Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda