Html2pdf – 将文本添加到已生成的PDF的新页面不起作用

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

Html2pdf - Adding text to new page in already generated PDF not working

问题

这确实添加了一个新页面,但是该页面上的文本不可见。我在这里做错了什么?

英文:

I am trying to add a footer to each page in my pdf file I am creating using Html2pdf.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

content = document.querySelector(&quot;.pdf&quot;);
const config = {
   filename:  &#39;document.pdf&#39;,
   image: {type: &#39;jpeg&#39;,quality: 1.0},
   html2canvas: {dpi: 300, letterRendering: true},
   jsPDF: {orientation: &#39;portrait&#39;, unit: &#39;in&#39;, format: &#39;a4&#39;},
   // pdfCallback: pdfCallback
}
$(&quot;button&quot;).on(&#39;click&#39;, function() {
  html2pdf().from(content).set(config).toPdf().get(&#39;pdf&#39;).then((pdf) =&gt; {
      var totalPages = pdf.internal.getNumberOfPages();

      pdf.addPage();
      pdf.setFontSize(22);
      pdf.text(10,10,&quot;Hello World&quot;);
      pdf.save(&#39;document.pdf&#39;);

  })
})

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js&quot;&gt;&lt;/script&gt;
   &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
   &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.3/html2pdf.bundle.min.js&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;div classs=&quot;pdf&quot;&gt;
  Original Page
&lt;/div&gt;

&lt;button&gt;Get PDF&lt;/button&gt;

<!-- end snippet -->

This does add a new page, but the text is not visible on that page. What am I doing wrong here?

答案1

得分: 1

你在文本中有一些小错误导致文本未显示:

  • 首先,text 的语法是 text(string|array, x, y),因此你的文本必须首先出现。
  • 其次要注意你的单位,A4 纸张是 21 厘米 / 29.7 厘米(或 210 毫米 / 297 毫米)。你设置了英寸作为单位,1 英寸 = 2.54 厘米。因此,通过将 x = 10,你实际上是在页面左边距离 25.4 厘米的地方写字,超出了页面范围。我建议你使用 &#39;mm&#39;&#39;pt&#39; 而不是 &#39;in&#39;
  • 额外提示:据我记得,这个坐标是文本的左下角,所以要从顶部和左边获得均匀的边距,你需要通过额外的行高进行补偿。行高有点棘手,你可以使用这个公式来计算(在设置新的字体大小后进行计算,1.15 是硬编码的 "lineHeightProportion" 值):
    (pdf.internal.getFontSize() / pdf.internal.scaleFactor) * 1.15

这是 jsfiddle,因为它不能在 SO 的代码片段中运行:
https://jsfiddle.net/gm8p9a3c/

    content = document.querySelector(&quot;.pdf&quot;);
    const config = {
       filename:  &#39;document.pdf&#39;,
       image: {type: &#39;jpeg&#39;,quality: 1.0},
       html2canvas: {dpi: 300, letterRendering: true},
       jsPDF: {orientation: &#39;portrait&#39;, unit: &#39;in&#39;, format: &#39;a4&#39;},
       // pdfCallback: pdfCallback
    }
    $(&quot;button&quot;).on(&#39;click&#39;, function() {
      html2pdf().from(content).set(config).toPdf().get(&#39;pdf&#39;).then((pdf) =&gt; {
          var totalPages = pdf.internal.getNumberOfPages();

          pdf.addPage();
          pdf.setFontSize(22);
          pdf.text(&quot;Hello World&quot;, 1, 1);
          pdf.save(&#39;document.pdf&#39;);

      })
    })
英文:

You had several small mistakes that added up to your text not showing:

  • first the syntax for text is text(string|array, x, y) so your text must be first
  • second be careful of your units, a portrait A4 page is 21cm / 29.7cm (or 210mm / 297mm). You set your units as inches, and 1 inch = 2.54cm. So by setting your x = 10, you were actually writing at 25.4cm from the left of the page, so outside of the page. IMO you would better work in &#39;mm&#39; or &#39;pt&#39; rather than &#39;in&#39;.
  • extra tip: IIRC this coordinate is for the bottom left of your text, so to have an even margin from the top and left you'll have to compensate with an extra line height. Line heights are a bit tricky, you should be able to get it with this formula (verify what it gives before using it). Calculate it after you set a new font size (the 1.15 is a line hardcoded "lineHeightProportion" value) :
    (pdf.internal.getFontSize() / pdf.internal.scaleFactor) * 1.15

Here is the fiddle, as it doesn't work in SO's snippets
https://jsfiddle.net/gm8p9a3c/

    content = document.querySelector(&quot;.pdf&quot;);
    const config = {
       filename:  &#39;document.pdf&#39;,
       image: {type: &#39;jpeg&#39;,quality: 1.0},
       html2canvas: {dpi: 300, letterRendering: true},
       jsPDF: {orientation: &#39;portrait&#39;, unit: &#39;in&#39;, format: &#39;a4&#39;},
       // pdfCallback: pdfCallback
    }
    $(&quot;button&quot;).on(&#39;click&#39;, function() {
      html2pdf().from(content).set(config).toPdf().get(&#39;pdf&#39;).then((pdf) =&gt; {
          var totalPages = pdf.internal.getNumberOfPages();

          pdf.addPage();
          pdf.setFontSize(22);
          pdf.text(&quot;Hello World&quot;, 1, 1);
          pdf.save(&#39;document.pdf&#39;);

      })
    })

huangapple
  • 本文由 发表于 2023年5月25日 16:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76330296.html
匿名

发表评论

匿名网友

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

确定