使用puppeteer API生成pdf时无法捕获图像

时间:2018-04-10 01:06:25

标签: javascript node.js selenium-chromedriver puppeteer google-chrome-headless

Node- v8.11.1无头Chrome
我试图生成PDF,但不知何故,背景图像没有在PDF中捕获。 下面是代码。任何帮助表示赞赏

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();
})();

2 个答案:

答案 0 :(得分:2)

正如Rippo所说,you require page.emulateMedia("screen")可以使其正常运作。我已在下面更新了您的脚本,但我将页面更改为谷歌进行测试。

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();
})();

答案 1 :(得分:0)

更新:page.emulateMedia()page.emulateMediaType()取代