Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

372
Views
Merge multiple puppeteer pdf buffers from urls into single pdf file and return to user

I am trying to merge multiple, infinite amount, of pdf buffers from puppeteer to a single file. I suspect it has something to do with the buffer, but I have yet to find a solution that seems to work. This got me the closest, How to output a PDF buffer to browser using NodeJS?, but the output still says it's unable to load. Adobe, Chrome, and Fox-IT all say it's corrupt or decoded incorrectly..

Puppeteer code:

async function generateBulkPDFFromUrl(urlString) {
  // launch a new chrome instance
  const browser = await puppeteer.launch({
      headless: true,
      args: ['--font-render-hinting=none']
  });

  // create a new page
  const page = await browser.newPage();
  await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) 
       AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36');

  // set your html as the pages content
  const url = new URL(`${urlString}`);

  await page.goto(url, { waitUntil: 'domcontentloaded' });

  // create a pdf buffer
  const pdfBuffer = await page.pdf({
       format: 'A4'
  });

  // close the browser
  await browser.close();

  return pdfBuffer;
}

Buffer from Puppetteer incase you wanna see it:

<Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 52960 more bytes>

Array of Buffers sent to Merge Code:

[
  <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 52960 more bytes>,
  <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 6d 69 75 6d 29 0a 2f 50 72 6f 64 ... 93378 more bytes>
]

Merge Code (PDF-LIB):

async function mergePdfs(pdfsToMerges) {
    const mergedPdf = await PDFDocument.create();
    const actions = pdfsToMerges.map(async pdfBuffer => {

    const pdf = await PDFDocument.load(pdfBuffer);
    const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());

        copiedPages.forEach((page) => {
           mergedPdf.addPage(page);
        });
    });

 await Promise.all(actions);

  return await mergedPdf.save();
}

I can get a single pdf to download fine, it's the merge that seems to be the issue. Any insight would be helpful. Thank you.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So I was correct in saying it was a buffer issue. I'm not sure PDF-LIB can handle the buffers from puppeteer correctly... if anyone has any other solution I'm all ears.. er fingers, well eyes..

Here is what I got to work, found here https://npm.io/package/merge-pdf-buffers

Seems a bit simplistic, and I'm not sure that this person is still supporting it.. so I may pull it down and see what it's doing. Maybe I'll support it. lol

However, at this time this solution is working.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!