Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Puppeteer optimalization

I have a web app that generates quite large PDF documents, possibly 100 pages and more.

The workflow is this:

  1. Generate html using nunjucks templates
  2. Open a puppeteer browser
  3. Create PDF front page (see code below)
  4. Create PDF pages
  5. Merge the pages to a document and create a buffer
import { PDFDocument } from 'pdf-lib';

const pdfHtml = await nunjucks.render(...);

const theBrowser = await puppeteer.launch({
  args: [
    '--disable-dev-shm-usage',
    '--no-first-run',
    '--no-sandbox',
    '--no-zygote',
    '--single-process',
  ],
  headless: true
});

const page = await theBrowser.newPage();

awaut page.setContent(`${pdfHtml}`, { waitUntil: 'networkidle0' });

const frontPage: Buffer = await page.pdf({
  ... someOptions,
  pageRanges: '1'
});

const pdfPages: Buffer = await page.pdf({
  ... someOptions,
  pageRanges: '2-',
  footerTemplate: ...,
});

const pdfDoc = await PDFDocument.create();
const coverDoc = await PDFDocument.load(frontPage);
const [coverPage] = await pdfDoc.copyPages(coverDoc, [0]);
pdfDoc.addPage(coverPage);

const mainDoc = await PDFDocument.load(reportPages);
for (let i = 0; i < mainDoc.getPageCount(); i++) {
    const [aMainPage] = await pdfDoc.copyPages(mainDoc, [i]);
    pdfDoc.addPage(aMainPage);
}

pdfBytes = Buffer.from(await pdfDoc.save());
// handle the bytes here

When the PDF gets really big, this operation will take quite a while and use lots of memory doing so, stalling the API until its completion. What can I do to optimalize this? Or are there other tools I can use to avoid stalling the API?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda