【问题标题】:Puppeteer: cannot render pdf with images stored locallyPuppeteer:无法使用本地存储的图像渲染 pdf
【发布时间】: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 路径?
  • 如果是,我是否正确指定了路径?还是我做错了什么?

【问题讨论】:

    标签: pdf-generation puppeteer


    【解决方案1】:

    已在 Puppeteer GitHub 上解决:https://github.com/GoogleChrome/puppeteer/issues/1643

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2021-06-19
      • 2018-03-02
      • 2015-10-12
      • 2022-01-18
      • 2015-07-03
      • 2018-12-25
      相关资源
      最近更新 更多