Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate HTML string into the docx? #2632

Open
jurei733 opened this issue Mar 7, 2024 · 5 comments
Open

How to integrate HTML string into the docx? #2632

jurei733 opened this issue Mar 7, 2024 · 5 comments

Comments

@jurei733
Copy link

jurei733 commented Mar 7, 2024

Want to integrate Html strings in my docx file and don't see how to to do it right now ?

@0biWanKenobi
Copy link

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

@k6c8e4
Copy link

k6c8e4 commented Apr 25, 2024

Same question 🙏

@EvanPiro
Copy link

EvanPiro commented May 6, 2024

Same here!

@nastopendo
Copy link

I am also interested in this

@EvanPiro
Copy link

EvanPiro commented May 7, 2024

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",
                  }),
                ],
              }),
            ],
          }),
        },
      },
    ],
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants