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

最佳答案

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

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

关于javascript - 使用puppeteer API生成pdf时无法捕获图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49743757/

10-15 08:40