简体   繁体   English

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

[英]Using jsPDF to render an HTML string into a PDF document

I really need your help, 我真的需要你的帮助,

I am trying to use the jsPDF library to render an HTML string into a PDF document. 我正在尝试使用jsPDF库将HTML字符串呈现为PDF文档。 When the function below is called at the click of a button it does nothing, what am I doing wrong here? 单击按钮时调用以下功能时,它什么都不做,这是我在做什么错?

<!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>

It seems you have an extra comma in the line - 似乎您在行中还有一个逗号-

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

Try removing it? 尝试删除它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM