Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to capture image while generating pdf using puppeteer API

Node- v8.11.1 Headless Chrome
Im trying to generate PDF but somehow the background image is not captured in the PDF. Below is the code. Any help is appreciated

const puppeteer = require('puppeteer'); 
(async () => {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto('http://54.201.139.151/', {waitUntil : 'networkidle0'});
await page.pdf({path: 'hn40.pdf', printBackground: true, width: '1024px' , height: '768px'});
await browser.close();
})();
like image 200
Suchitra Avatar asked Dec 11 '22 07:12

Suchitra


2 Answers

Update: page.emulateMedia() is dropped in favor of page.emulateMediaType()

like image 121
Jovan Balaban Avatar answered May 08 '23 09:05

Jovan Balaban


As Rippo mentioned, you require page.emulateMedia("screen") for this to work properly. I have updated your script below, but I changed the page to google for testing.

const puppeteer = require('puppeteer'); 
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('http://google.ca/', {waitUntil : 'networkidle2'});
    await page.emulateMedia('screen');
    await page.pdf({path: 'hn40.pdf', printBackground: true, width: '1024px' , height: '768px'});
    await browser.close();
})();
like image 42
pmkro Avatar answered May 08 '23 11:05

pmkro