无法使用 puppeteer 打印我的 ejs 文件中的图像

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

当我尝试在 Puppeteer 和 ejs 模板的帮助下打印 pdf 时,它迫不及待地等待解析路径。所以它会打印样式路径之前的页面,图像被解析。结果,它将只打印没有 CSS 和图像的数据

index.ejs

<head>
  <link rel="stylesheet" href="/css/style.css">
</head>

<body>
  <h2 class="fourth-one">Testing</h2>
  <div class="logo-img">
    <img style="margin-left: 200px;" src="/img/paybooks.jpg" width=300px height=250px>
  </div>
</body>

脚本文件代码

(async () => {
  const browser = await puppeteer.launch();
  const [page] = await browser.pages();
  const html = await ejs.renderFile('views/index.ejs', {
    data: response[i],
  });
  await page.setContent(html, { waitUntil: 'domcontentloaded' });
  const pdf = await page.pdf({
    path: `result-1.pdf`,
    printBackground: true,
    format: 'A4',
  });
  await browser.close();
})();

我尝试从互联网复制图片地址,它适用于该图片,但无法加载本地存储在同一文件夹中的图片

My code structure

记住我在 app.js 中使用了

app.use(express.static(path.join(__dirname, 'public')));
所以不需要写完整的路径

node.js puppeteer ejs
© www.soinside.com 2019 - 2024. All rights reserved.