jsPdf:使用 html() 插件设置边距

Suh*_*ttu 5 javascript css jspdf

我正在使用 jsPDF 将 HTML 转换为 PDF 文件。它工作正常,但我想在页面周围添加边距,以便在内容分成页面时不会剪切文本。但是我没有找到任何方法来为 PDF 文件添加边距。我正在使用以下代码添加文本。请注意,我使用的是新.html()插件, not .addHtml(),已弃用,如他们的文档所示。所以这不是这个问题的重复。

<!DOCTYPE html>
 <html lang="en">
<head>
    <script src="jsPDF-master/dist/jspdf.min.js"></script>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
</head>
<body>
    <div id="content">
       <label><span>Standard</span></label>
       <label><span>Planning item number:</span></label>
    </div>                    
    <button onclick="exportPDF()" style="float:right;">Download</button>
    <script>
        function exportPDF () {
            var pdf = new jsPDF('p', 'pt', 'a4');
            var margins = {
                top: 40, bottom: 60, left: 40, right: 200
            };
            pdf.html(document.getElementById("content"), {
                callback : function (pdf) {
                    pdf.save("a4.pdf");
                }
            });
        }
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何为 PDF 页面添加边距?

小智 0

保证金将作为选项对象的一部分传入,这是调用的第二个参数pdf.html(element, options)

margin 属性可接受的值为:数字、2 数组或 4 数组: margin: 40 OR margin: [40,60] OR margin: [40,200,60,40]

请参阅 jsPDF html 插件第 499 行的 Worker.prototype.setMargin: https://artskydj.github.io/jsPDF/docs/modules_html.js.html

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="jsPDF-master/dist/jspdf.min.js"></script>
    <script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>
</head>
<body>
    <div id="content">
       <label><span>Standard</span></label>
       <label><span>Planning item number:</span></label>
    </div>                    
    <button onclick="exportPDF()" style="float:right;">Download</button>
    <script>
        function exportPDF () {
            var pdf = new jsPDF('p', 'pt', 'a4');
            pdf.html(document.getElementById("content"), {
                callback : function (pdf) {
                    pdf.save("a4.pdf");
                },
                margin: [40, 200, 60, 40]
            });
        }
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)