Coder Social home page Coder Social logo

Comments (5)

EvanPiro avatar EvanPiro commented on June 6, 2024 1

Here is a minimal implementation that works for very simple HTML (WYSIWYG outputs).

import { parseFromString } from "dom-parser";
import {
  Document,
  Footer,
  PageNumber,
  Paragraph,
  TextRun,
} from "docx";
import * as he from "he";

export const htmlStrToParagraph = (str: string): Paragraph[] => {
  const dom = parseFromString(`<body>${str}</body>`)
  
  const sectionChildren = [];

  dom.getElementsByTagName("body")[0].childNodes.forEach((node) => {
    if (["h1", "h2", "h3"].includes(node.nodeName)) {
      sectionChildren.push(
        new Paragraph({
          children: [
            new TextRun({
              text: he.decode(node.textContent),
              bold: true,
            }),
          ],
          spacing: { after: 0 },
        })
      );
    } else if (["p"].includes(node.nodeName)) {
      sectionChildren.push(
        new Paragraph({
          children: [
            new TextRun({
              text: he.decode(node.textContent),
            }),
          ],
          spacing: { after: 0 },
        })
      );
    } else if (node.nodeName === "ul") {
      node.childNodes.forEach((bullet) => {
        if (bullet.nodeName === "li") {
          sectionChildren.push(
            new Paragraph({
              text: he.decode(bullet.textContent),
              bullet: {
                level: 0,
              },
              spacing: {
                before: 0,
                after: 0,
              },
            })
          );
        }
      });
    }

    sectionChildren.push(
      new Paragraph({
        text: "",
        spacing: {
          before: 0,
          after: 0,
        },
      })
    );
  });

  return sectionChildren;
};

You can then run it to construct a document:

const htmlStrToDocx = async (str: string): Promise<Document> => {
  return new Document({
    creator: "Me",
    sections: [
      {
        children: htmlStrToParagraph(str),
        footers: {
          default: new Footer({
            children: [
              new Paragraph({
                alignment: AlignmentType.CENTER,
                children: [
                  new TextRun({
                    children: [PageNumber.CURRENT],
                    color: "000000",
                  }),
                ],
              }),
            ],
          }),
        },
      },
    ],
  });
};

from docx.

0biWanKenobi avatar 0biWanKenobi commented on June 6, 2024

I'm also interested in this, would it be possible to have this feature?

from docx.

k6c8e4 avatar k6c8e4 commented on June 6, 2024

Same question 🙏

from docx.

EvanPiro avatar EvanPiro commented on June 6, 2024

Same here!

from docx.

nastopendo avatar nastopendo commented on June 6, 2024

I am also interested in this

from docx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.