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.

Corrupted pdf (inconsistent)

See original GitHub issue

Hi @rkusa,

I’m having some intermittent problems that appear related to closing the pdf document. Here is a reduced snippet demonstrating te issue:

export const testPDF = async function () {

  const readFile = function (f) {
    return new Promise(function (resolve, reject) {
      fs.readFile(f, (err, data) => {
        err ? reject(err) : resolve(data);
      });
    });
  }

  const tempFile = `${Random.id()}.pdf`
  const tempPath = `${os.tmpdir()}${tempFile}`;

  const doc = new pdf.Document();
  doc.pipe(fs.createWriteStream(tempPath))

  const sampleCell = doc.cell();
  sampleCell.text({fontSize: 14 }).add('headline')
  sampleCell.text({fontSize: 12 }).add('Line one')
  sampleCell.text().add('Line two')
  
  await doc.end();

  const pdfData1 = await readFile(tempPath);
  // const pdfData2 = await readFile(tempPath);

  console.log(`pdfData1 ${tempFile} length: ${pdfData1.length}`);
  // console.log(`pdfData2 ${tempFile} length: ${pdfData2.length}`);

  return true;

};

invoking the above function like

    const res = await testPDF();

sometimes (maybe half the time) results in pdfData1 having corrupted (truncated?) data. example of invoking it six times:

    const res1 = await testPDF();
    const res2 = await testPDF();
    const res3 = await testPDF();
    const res4 = await testPDF();
    const res5 = await testPDF();
    const res6 = await testPDF();

results in following console log:

I20190104-09:29:53.937(11)? pdfData1 6EXKAv5E2nACnDrff.pdf length: 5258
I20190104-09:29:53.941(11)? pdfData1 jfzRdCmNpC7XDkjNT.pdf length: 16
I20190104-09:29:53.944(11)? pdfData1 fyEZg7XyEg9CanpoR.pdf length: 5258
I20190104-09:29:53.946(11)? pdfData1 m2atPJS4v7B7g9iBi.pdf length: 16
I20190104-09:29:53.949(11)? pdfData1 eBoKr5zHuXyfCTc2L.pdf length: 16
I20190104-09:29:53.953(11)? pdfData1 w2pwKQAJEQwtLHvGu.pdf length: 16

Wen reading the same file again (as when adding the commented out lines) will always result in pdfData2 having correct data. Sample output with commented out lines added:

I20190104-09:29:17.490(11)? pdfData1 JAzWC7KhfprnQqAs7.pdf length: 16
I20190104-09:29:17.491(11)? pdfData2 JAzWC7KhfprnQqAs7.pdf length: 5258
I20190104-09:29:17.496(11)? pdfData1 YT9Rt97BNwW7PuhuH.pdf length: 16
I20190104-09:29:17.497(11)? pdfData2 YT9Rt97BNwW7PuhuH.pdf length: 5258
I20190104-09:29:17.501(11)? pdfData1 Hrf9H5tjEt2cTv8qo.pdf length: 5258
I20190104-09:29:17.502(11)? pdfData2 Hrf9H5tjEt2cTv8qo.pdf length: 5258
I20190104-09:29:17.505(11)? pdfData1 cx9axS5guzmh8WeMn.pdf length: 16
I20190104-09:29:17.505(11)? pdfData2 cx9axS5guzmh8WeMn.pdf length: 5258
I20190104-09:29:17.508(11)? pdfData1 fi3FeGKdnNhCi5YJv.pdf length: 5258
I20190104-09:29:17.509(11)? pdfData2 fi3FeGKdnNhCi5YJv.pdf length: 5258
I20190104-09:29:17.511(11)? pdfData1 x5jsesFbKnLMSCJHC.pdf length: 5258
I20190104-09:29:17.512(11)? pdfData2 x5jsesFbKnLMSCJHC.pdf length: 5258

A second question: Is there a way of getting the generated data into a variable (eg, pdfData1 above) without having to write the file into disk first?

Thanks for looking into this.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
hluzcommented, Feb 14, 2019

Works great. Thanks!

0reactions
rkusacommented, Feb 13, 2019

Updated snippet with the commit above:

const doc = new pdf.Document();

// start reading here
const buff = doc.asBuffer({end: false})

// MERGE documents here

doc.end();
const pdfData = await buff;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Repair Corrupted or Damaged PDF File with 4 Ways
Learn the different ways to repair corrupted PDF easily and quickly here. ... Incompatible programs: Using incompatible programs to open the PDF can...
Read more >
How to Repair a Corrupted or Damaged PDF file? - PDF2Go
This PDF repair tool helps users to recover and repair the corrupted PDF files, by performing complete PDF file recovery with no loss...
Read more >
2 Methods to Repair Corrupted PDF Files - SysTools
Learn how to repair corrupted PDF files on Windows & Mac. Know the complete step by step process & recover corrupted PDF documents....
Read more >
Effortless Method to Repair a Corrupted PDF File - UPDF
If you attempt to open or generate a PDF file using incompatible software, the file may be altered, hence becoming corrupted.
Read more >
A Simple Way to Repair Adobe PDF Files
Repair Corrupt or Damaged PDF Files · Try other PDF Reader utilities · Restore the previous file version · Convert the PDF file...
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