node.jsexpresspdfpdfkitnode.js-fs

Cannot open PDFKit generated file


I want to create a pdf file in my NodeJs server and then stream to the client, so it can download it, but I am not able to open the pdf file.

I created a pdf file using PDFKit, using the code in the documentation:

const doc = new PDFDocument({});
doc.text("Test");

const writeStream = fs.createWriteStream("MyTest.pdf");
writeStream.on("close", () => {
    console.log("done");
});
doc.pipe(writeStream);
doc.end();

The pdf file gets created, but when I try to open it 2 things can happen:

  1. Adobe Acrobat does not open at all
  2. Adobe Acrobat opens after serveral minutes

I have the same behaviour if I open the file with Google Chrome.

I already looked into previous StackOverflow questions, like:

  1. error while opening pdf file created by pdfkit in node
  2. PDFKit, Not able to open the pdf generated

But these solutions seem not working for me.

Any ideas on how I have to proceed?


Solution

  • Found the issue, I was creating the file in Windows WSL and opening from WSL's fileSystem with Windows' Adobe Acrobat. This operation is very slow.

    A quick workaround to see the result of the pdf operation is to copy the .pdf file to Windows' desktop and open it from there.