Generate a PDF in AWS Lambda with NodeJS and Puppeteer

Aki Rautio on July 22, 2019

Recently I have needed to solve a problem that involves generating a PDF file based on database content. Since these PDFs are not generated too oft... [Read Full]
markdown guide

Thanks for the article.
I have an issue where the pdf comes empty randomly, to solve it setContent should wait for everything to be loaded.

await page.setContent(html, { waitUntil: ['load', 'domcontentloaded', 'networkidle0'] });

Thanks :) Very good point and interesting find. I haven't seen this when loading html as string but this very well can happen.

WaitUntil is very good to use and even necessary if page itself loads external content.


I have read about named @page rule in css, but It is not working, any idea? Why? i want to make mixture of landscape and portrait pages.


Any chance you could share your CSS? I haven't tried exactly this kind of a scenario but it could be that puppeteer has some limitation regarding css.

      @page :first {
          display: none;
      @page { size : portrait }
      @page rotated { size : landscape }
      h3 { page : rotated }

      p, h3 {
        page-break-after: always;
    <p>First Page.</p>
    <h3>Hello world</h3>
  <p>Second Page.</p>


Error: Chromium revision is not downloaded. Run "npm install" which, doesn't work


Is this happening on aws or when running locally?


Locally. Works fine on lambda, how do I run it locally?

Also thank you very much for this code, saved me 5 days of work

The original package that we are using in here is suggesting following: github.com/alixaxel/chrome-aws-lam...


Hi! Thanks for the article, it helped me a lot, I didn't know chrome-aws-lambda and it was frustrating me horrors, I couldn't use serverless puppeteer at all and I finally got it now.


"errorMessage": "Cannot find module 'iltorb'"


This article was very helpful to me.
The pdf that you created, I wanted to store that pdf at S3 bucket because it's in base64, Can you guide me how can I do that?


I haven't done this with PDF but I have another lambda function save files which I save this way. Though you could also put the PDF variable straight to body before turning it to base64 and that should work.

const s3 = new AWS.S3()

Body: Buffer.from(pdfBase64, 'base64'),
Bucket: BUCKET,
Key: 'path for the file'
(err, data) => {
if (err) {
callback(err, null)
} else {
callback(null, {
statusCode: 200,
body: true,
isBase64Encoded: false

code of conduct - report abuse