Programmatic PDF generation on worker

Hi everyone,

I've been looking for some way to generate PDFs within a worker script.
Currently I use the browser rendering api to create PDFs of a page but according to the dashboard a single invocation takes 6 seconds...
There must be a better and faster way right?

I looked into the javascript PDF libraries (pdfkit, jspdf, pdf-lib, pdfme, etc.) but these are very limited in their features (I cannot even use a bold font?).
I have no experience with Rust but I'm considering this more and more as it seems the only solution?
But before I plunge myself into the deep with learning a bit of Rust, I'm curious if there are any other solutions?
Will I run into the same problems as javascript (limited features)?
Can I improve the speed of puppeteer rendering the PDF?

This function renders the pdf on the worker:
export async function generateFromPage(browser: Browser, ticketId: string, lang: string) {
  const page = await browser.newPage();
  await page.setCookie({ name: 'locale', value: lang, domain: '<redacted>' });
  page.on('error', err => console.log(`Browser DO: Error: ${JSON.stringify(err)}`));
  page.on('pageerror', err => console.log(`Browser DO: Page Error: ${JSON.stringify(err)}`));

  // create a pdf of the ticket page
  await page.emulateMediaType('screen');
  await page.emulateTimezone('Europe/Amsterdam');
  await page.setViewport({ width: 1920, height: 1080 });
  console.log(`Creating PDF of ticket page: https://<redacted>/ticket/${ticketId}`);
  await page.goto(`https://<redacted>/ticket/${ticketId}`, { waitUntil: ['domcontentloaded', 'load', 'networkidle0'] });
  const pdf = await page.pdf({ format: 'A4' });

  // Close tab when there is no more work to be done on the page
  await page.close();

  return {
    name: `ticket-${ticketId}.pdf`,
    data: pdf,
  };
}
Was this page helpful?