Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puppeteer blank pdf generation

I'm using this simple code to generate a pdf document from http://example.com/ but I keep getting a blank pdf generated ...

Am I missing something ?

const puppeteer = require('puppeteer');

puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }).then(function (browser) {
  browser.newPage().then(function (page) {
    page
      .goto('http://example.com/', { waitUntil:['domcontentloaded', 'networkidle0','load'] })
      .then(page.pdf({ path: 'result.pdf', format: 'letter' }))
      .then(() => {
        browser.close();
      })
  })
})

I used the no-sandbox option because of kernel issues.

I'm using CentOS 7

like image 647
M. Gara Avatar asked Feb 07 '18 17:02

M. Gara


1 Answers

I had to wait for the promise in page.goto().then ...

const puppeteer = require('puppeteer');


puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] }).then(function (browser) {
  browser.newPage().then(function (page) {

    page
      .goto('https://www.example.com', { waitUntil: ['domcontentloaded', 'networkidle0', 'load'] }).then(function (response) {
    //    page.emulateMedia('screen')
        page.pdf({ path: 'result.pdf', format: 'letter' })
          .then(function (res) {
            browser.close();
          }).catch(function (e) {
            browser.close();
          })
      })
  })
})
like image 59
M. Gara Avatar answered Oct 19 '22 12:10

M. Gara