英文:
Generate PDF from HTML/CSS code with margins to all pages
问题
我需要使用JavaScript将一个长度可能变化的HTML页面转换为PDF。我尝试了多种方法,如Window.print、jsPDF或html-to-pdf-js。但我总是遇到相同的问题:当内容太大以至于不能适应一页时,会生成一个新的页面,但页面之间没有任何边距。
这是我一直在尝试的示例:
var doc = new jspdf.jsPDF("p", "mm", "a4");
doc.html(document.body, {
callback: function (doc) {
doc.save();
},
});
不期望的结果如下:
英文:
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:
答案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('TCPDF/tcpdf.php');
$pdf = new TCPDF();
$pdf->SetMargins(20, 20, 20);
$pdf->AddPage();
$pdf->writeHTML("<b>Bold text!</b>");
$pdf->Output('example.pdf', 'I');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论