Error while create pdf stream using html-pdf npm package on Ubuntu Docker

766 Views Asked by At

I am getting below error while converting html to pdf using html-pdf npm package on Ubuntu Docker (however it's working fine in my local system Windows and Mac both) -

node:events:491

      throw er; // Unhandled 'error' event

      ^

Error: write EPIPE

    at afterWriteDispatched (node:internal/stream_base_commons:160:15)

    at writeGeneric (node:internal/stream_base_commons:151:3)

    at Socket._writeGeneric (node:net:905:11)

    at Socket._write (node:net:917:8)

    at writeOrBuffer (node:internal/streams/writable:391:12)

    at _write (node:internal/streams/writable:332:10)

    at Socket.Writable.write (node:internal/streams/writable:336:10)

    at PDF.PdfExec [as exec] (/usr/src/node-app/node_modules/html-pdf/lib/pdf.js:156:15)

    at PDF.PdfToStream [as toStream] (/usr/src/node-app/node_modules/html-pdf/lib/pdf.js:59:8)

    at /usr/src/node-app/server.js:335:75

Emitted 'error' event on Socket instance at:

    at emitErrorNT (node:internal/streams/destroy:157:8)

    at emitErrorCloseNT (node:internal/streams/destroy:122:3)

    at processTicksAndRejections (node:internal/process/task_queues:83:21) {

  errno: -32,

  code: 'EPIPE',

  syscall: 'write'

**Docker OS - **

uname -a
[1670850844595]     Linux 693e0a6af7a3 4.14.281-212.502.amzn2.x86_64 #1 SMP Thu May 26 09:52:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

**Code - ** I tried both APIs but getting same error in both -

1.

htmlPdf.create(htmlContent, htmlToPdfOptions).toStream(function(err: any, stream: { pipe: (arg0: fs.WriteStream) => void; }){
        stream.pipe(fs.createWriteStream('./outputDir/chatTranscript.pdf'));
       });
htmlPdf.create(htmlContent, htmlToPdfOptions)
      .toFile("newPdf.pdf", function(err: any, result: any) {
        if (err) { console.log("Error while generating pdf file - ",err); }
      //     console.log("pdf file generated - ",result);
      //     return; 
     });

**Full Method - **

async function generatePdf() {
  try {
    console.log("Inside generatePdf() ...")
      let  HTMLFilePath = __dirname+"/pdfTemplate.html";

      if (!fs.existsSync(HTMLFilePath)) {
          console.log("file doesn't exists");
      } else {
        console.log("file exists: ",HTMLFilePath);
      }

      const htmlContent = fs.readFileSync(HTMLFilePath, 'utf8');
      const htmlToPdfOptions = {
          "type": ".pdf",
          "height": "650px",
          "width": "850px",
          "renderDelay": 2000,
      };

       const a = await htmlPdf.create(htmlContent, htmlToPdfOptions).toStream(function(err: any, stream: { pipe: (arg0: fs.WriteStream) => void; }){
        stream.pipe(fs.createWriteStream('./outputDir/chatTranscript.pdf'));
       });

       console.log("printing output of html stream");
       console.log("a: ",a);
       return a;

      // htmlPdf.create(htmlContent, htmlToPdfOptions)
      // .toFile("newPdf.pdf", function(err: any, result: any) {
      //     if (err) {
      //       console.log("Error while generating pdf file - ",err);
      //     } 
      //     console.log("pdf file generated - ",result);
      //     return; 
      // });
  } catch (error) {
      console.log("error while converting html-to-pdf", error);
  }
}

Earlier Docker container (Ubuntu) was giving "libfontconfig" library error so I added these two lines in Dockerfile -

apt-get update
apt-get install -y libfontconfig

I also checked the permission on the directory where file is getting generated, user has write permission there.

0

There are 0 best solutions below