Aymkdn / html-to-pdfmake

This module permits to convert HTML to the PDFMake format
https://aymkdn.github.io/html-to-pdfmake/index.html
MIT License
545 stars 88 forks source link

Cannot load images in the Node.js environment #194

Closed grizlizli closed 11 months ago

grizlizli commented 11 months ago

Hi,

Hope you are doing well.

Recently, we started using your library in our project.

However, when we tested generating a document with images, we got this error:

\node_modules\pdfmake\build\pdfmake.js:66743
[1]             var xhr = new XMLHttpRequest();
[1]                       ^
[1]
[1] ReferenceError: XMLHttpRequest is not defined

Here's our setup:

const html = htmlToPdfMake(HTML, { window, imagesByReference: true });

    const docDefinition = {
      // @ts-ignore
      content: html["content"],
      // @ts-ignore
      images: html["images"],
      // pageOrientation: "landscape",
      styles: {
        "html-h1": {
          color: "red",
          background: "white",
        },
        "di-paragraph": {
          color: "green",
        },
      },
    };

    const pdfDocGenerator = pdfMake.createPdf(docDefinition);
    // pdfDocGenerator.download();
    pdfDocGenerator.getBuffer(function (buffer) {
      //fs.writeFileSync("companies.pdf", buffer);

      res.setHeader("Response-Type", "blob");
      res.setHeader("Content-Length", buffer.length);
      res.setHeader("Content-Type", "application/pdf");
      res.setHeader("Content-Disposition", "attachment; filename=quote.pdf");
      res.write(buffer, "binary");
      res.end(null, "binary");
    });

Thanks in advance!

Aymkdn commented 11 months ago

Images generated from a Node process must be provided as a base64, as explained in the documentation: image

I agree that my documentation is not super clear…

grizlizli commented 11 months ago

Thanks for the clarification.

Keep up the awesome work!