生成PDF从HTML/CSS代码,带有所有页面的边距。

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

Generate PDF from HTML/CSS code with margins to all pages

问题

我需要使用JavaScript将一个长度可能变化的HTML页面转换为PDF。我尝试了多种方法,如Window.printjsPDFhtml-to-pdf-js。但我总是遇到相同的问题:当内容太大以至于不能适应一页时,会生成一个新的页面,但页面之间没有任何边距。

这是我一直在尝试的示例:

var doc = new jspdf.jsPDF("p", "mm", "a4");
doc.html(document.body, {
    callback: function (doc) {
        doc.save();
    },
});

不期望的结果如下:

生成PDF从HTML/CSS代码,带有所有页面的边距。

英文:

I need to convert an HTML page with a length that can vary to a PDF using JavaScript. I tried using multiple methods, like Window.print, jsPDF or html-to-pdf-js. But I always ended up having the same problem: When the content was too large to fit in a page, a new one would be generated without any margin between the pages.

Here's an example of what I have been trying:

var doc = new jspdf.jsPDF("p", "mm", "a4");
doc.html(document.body, {
    callback: function (doc) {
        doc.save();
    },
});

And the undesired result:

生成PDF从HTML/CSS代码,带有所有页面的边距。

答案1

得分: 0

I ended up giving up on using a JS framework and switched to using the TCPDF PHP framework, that has, between many other, the very useful option SetMargins():

require_once('TCPDF/tcpdf.php');
$pdf = new TCPDF();
$pdf->SetMargins(20, 20, 20);
$pdf->AddPage();
$pdf->writeHTML("<b>Bold text!</b>");
$pdf->Output('example.pdf', 'I');
英文:

I ended up giving up on using a JS framework and switched to using the TCPDF PHP framework, that has, between many other, the very useful option SetMargins():

require_once(&#39;TCPDF/tcpdf.php&#39;);
$pdf = new TCPDF();
$pdf-&gt;SetMargins(20, 20, 20);
$pdf-&gt;AddPage();
$pdf-&gt;writeHTML(&quot;&lt;b&gt;Bold text!&lt;/b&gt;&quot;);
$pdf-&gt;Output(&#39;example.pdf&#39;, &#39;I&#39;);

huangapple
  • 本文由 发表于 2023年3月8日 17:40:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671430.html
匿名

发表评论

匿名网友

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

确定