question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feeding Pdfkit generated file to pdf-lib.

See original GitHub issue

I am trying to send a pdfkit generated pdf file as input to pdflib for merging. I am using async function. My project is being developed using sails Js version:“^1.2.3”, “node”: “^12.16”, my pdf-kit version is: “^0.11.0”, “pdf-lib”: “^1.9.0”, This is the code:

const  textbytes=fs.readFileSync(textfile);
var bytes1 = new Uint8Array(textbytes);
 const textdoc = await PDFDocument.load(bytes1)

The error i am getting is:

UnhandledPromiseRejectionWarning: Error: Failed to parse PDF document (line:0 col:0 offset=0): No PDF header found

Please help me with this issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Hopdingcommented, Nov 24, 2020

@Patcher56 Looks like a very interesting project! I suspect that in many places the protected access modifier could be used instead of the private one. I don’t want to replace all usages of private, and relatively few instances should be changed to public. If something is marked as private then it is not meant to be public interfaces and is subject to breaking changes. If you’d like to open a PR changing the things you need to protected, I’d be happy to take a look.

1reaction
hdwongcommented, Aug 8, 2020

I also use pdfkit and pdf-lib at the same time, but I can embed the pdf files generated by pdfkit. I use embedPdf instead of PDFDocument.load.

const PDFDocument = require('pdf-lib');
const pdfDoc = await PDFDocument.create();
const [ sourcePage ] = await pdfDoc.embedPdf(fs.readFileSync(textfile));

I think the problem may be that the pdf generated by pdfkit is not saved completely, you can try to ensure that the pdf file has been written before load by pdf-lib.

const PDFDocument = require('pdfkit');
const doc = new PDFDocument;
doc.pipe(fs.createWriteStream('/path/to/file.pdf'));

// draw something...

(async () => {
  await new Promise(resolve => {
    // resolve after WriteStream is finished, 
    doc.stream.on('finish', resolve);
    // finalize the PDF and end the stream
    doc.end();
  });

  // open the file after here
})();
Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Feeding PDF generated from pdfkit as input to pdf-lib ...
I am trying to send a pdfkit generated pdf file as input to pdflib for merging. I am using async function. My project...
Read more >
Feeding PDFKit into pdf-lib · Issue #9 - GitHub
I'm using PDFKit/Svg-to_PDF to convert SVG maps to a GeoPDF aalong with the sajjad-shirazy/pdfkit.js fork of PDFKit to insert SPOT colors.
Read more >
PDFKit Guide
PDFKit normally flushes pages to the output file immediately when a new page is created, making it impossible to jump back and add...
Read more >
Working With PDFs in Node.js Using pdf-lib
Puppeteer is a great tool for generating PDFs from HTML, ... The PDF document will have 1 page with the Mastering JS logo...
Read more >
Microservices – Generating PDF with Node.js & PDFKit
Create the main function (GenInvoice.js) to provide the sample invoice data to be displayed in the generated PDF document. The sample invoice ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found