使用jsPDF将HTML字符串呈现为PDF文档

Using jsPDF to render an HTML string into a PDF document

本文关键字:PDF 文档 字符串 jsPDF HTML 使用      更新时间:2023-09-26

我真的需要你的帮助,

我正在尝试使用jsPDF库将HTML字符串呈现为PDF文档。当下面的函数在点击按钮时被调用,它什么也不做,我在这里做错了什么?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jspdf.debug.js"></script>
<style type="text/css">
</style>
<script type="text/javascript">
function demoLandscape() {
    var doc = new jsPDF('landscape');
    doc.text(10, 10, 'Hello landscape world!'nThis is client-side Javascript, pumping out a PDF.');
    // Save the PDF
    doc.save('Test.pdf');
}

function demoHTML() {
                var pdf = new jsPDF('p', 'pt', 'portrait');
                source = '<html><body><p>test HTML string</p></body></html>'
                margins = {
                    top : 80,
                    bottom : 60,
                    left : 60,
                    width : 522
                };
                // all coords and widths are in jsPDF instance's declared units
                // 'inches' in this case
                pdf.fromHTML(source, // HTML string or DOM elem ref.
                margins.left, // x coord
                margins.top, { // y coord
                    'width' : margins.width, // max width of content on PDF
                },
                function(dispose) {
                    // dispose: object with X, Y of the last line add to the PDF 
                    //          this allow the insertion of new lines after html
                    pdf.save('fileNameOfGeneretedPdf.pdf');
                }, margins);
            });
        });

}
</script>
</head>
<body>
<input type="button" value="test2" onclick="demoHTML()">
</body>
</html>

你好像多了一个逗号-

margins.top, { // y coord
                'width' : margins.width,

尝试删除它?