base64图像在尝试在headertemplate puppeteer中呈现图像时损坏

huangapple go评论52阅读模式
英文:

base64 image is broken when trying to render image in headertemplate puppeteer

问题

我想使用puppeteer/PDFOptions创建一个PDF。它包含在_headerTemplate_中的base64编码图像:

await page.pdf({
  displayHeaderFooter: true,
  printBackground: true,
  headerTemplate:
    '<img class="header-logo" src="data:image/png;base64,${headerimgtry}" style="height:100px; width:100px">',
  footerTemplate: '<h1">Footer</h1>',
  margin: "30px",
  format: "A4",
  path: "output.pdf",
});

(source)

但是,它只显示一个占位符而不是图像:

base64图像在尝试在headertemplate puppeteer中呈现图像时损坏

如何才能正确显示图像在PDF中?

英文:

I want to create a pdf using puppeteer/PDFOptions . It contains a base64 encoded image inside the headerTemplate:

await page.pdf({
  displayHeaderFooter: true,
  printBackground: true,
  headerTemplate:
    &#39;&lt;img class=&quot;header-logo&quot; src=&quot;data:image/png;base64,${headerimgtry}&quot; style=&quot;height:100px; width:100px&quot;&gt;&#39;,
  footerTemplate: &#39;&lt;h1&quot;&gt;Footer&lt;/h1&gt;&#39;,
  margin: &quot;30px&quot;,
  format: &quot;A4&quot;,
  path: &quot;output.pdf&quot;,
});

(source)

But instead of the image it only shows a placeholder:

base64图像在尝试在headertemplate puppeteer中呈现图像时损坏

How can I correctly display the image in the pdf?

2: https://i.stack.imgur.com/oBs0g.png "What it looks like, with a placeholder intead of the image"
3: https://i.stack.imgur.com/Ss50N.png "the code snippet"

答案1

得分: 1

使用不同的模板引擎,最近遇到了这个问题。将img/png中的正斜杠转义为img//png可以解决它。

英文:

I'm using a different templating engine and recently came across this issue. Escaping img/png with a forward-slash to img//png fixed it for me.

huangapple
  • 本文由 发表于 2023年3月12日 19:38:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定