【发布时间】:2017-12-21 12:34:12
【问题描述】:
我无法使用 Puppeteer 获取本地存储的图像以在生成的 pdf 中呈现,但我为其指定 url 的外部图像工作。
特别是,在下面的示例代码中,在 test_html1 中渲染页面有效,而在 test_html2 中渲染页面无效。
(async () => {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
const test_html1 = `<html><h3>Hello world!</h3><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1024px-Google_2015_logo.svg.png"></html>`;
// const test_html2 = `<html><h3>Hello world!</h3><img src="file:///home/cristina/Documents/logo.jpg"></html>`;
await page.goto(`data:text/html,${test_html}`, { waitUntil: 'networkidle0' });
await page.pdf({ path: `${this.outputPath}/test-puppeteer.pdf`,
format: 'A4', landscape: !data.isPortrait,
margin: { top: '0.5cm', right: '1cm', bottom: '0.8cm', left: '1cm' }, printBackground: true });
await browser.close();
})();
test_html1的结果:
test_html2的结果:
我的问题:
- Puppeteer 是否使用绝对 img 路径?
- 如果是,我是否正确指定了路径?还是我做错了什么?
【问题讨论】: