Puppeteer:无法使用本地存储的图像渲染pdf

问题描述 投票:0回答:1

我无法将本地存储的图像渲染为使用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的结果:

enter image description here

test_html2的结果:

enter image description here

我的问题:

  • Puppeteer是否使用绝对img路径?
  • 如果是,我是否正确指定路径?或者还有别的我做错了吗?
pdf-generation puppeteer
1个回答
0
投票

解决了Puppeteer GitHub:https://github.com/GoogleChrome/puppeteer/issues/1643

基本上,它不起作用,因为作为针对恶意网站的安全措施,阻止了对本地图像的访问。

© www.soinside.com 2019 - 2024. All rights reserved.