Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:使用jsPDF将html窗口另存为pdf_Javascript_Html_Pdf_Printing_Jspdf - Fatal编程技术网

Javascript:使用jsPDF将html窗口另存为pdf

Javascript:使用jsPDF将html窗口另存为pdf,javascript,html,pdf,printing,jspdf,Javascript,Html,Pdf,Printing,Jspdf,我正在使用这段javascript打印一些html内容: function printHTML(htmlToPrint) { var mywindow = window.open('', 'PRINT', 'height=400, width=600'); mywindow.document.write("<html><head><title>My Doc Title</title>"); mywindow.documen

我正在使用这段javascript打印一些html内容:

function printHTML(htmlToPrint) {
    var mywindow = window.open('', 'PRINT', 'height=400, width=600');

    mywindow.document.write("<html><head><title>My Doc Title</title>");
    mywindow.document.write("</head><body>");
    mywindow.document.write(htmlToPrint);
    mywindow.document.write("</body></html>");

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10*/

    mywindow.print();
    mywindow.close();

    return true;
}
函数printHTML(htmlToPrint){
var mywindow=window.open(“”,'PRINT','height=400,width=600');
mywindow.document.write(“我的文档标题”);
mywindow.document.write(“”);
mywindow.document.write(htmlToPrint);
mywindow.document.write(“”);
mywindow.document.close();//对于IE>=10是必需的
mywindow.focus();//IE>=10所必需的*/
mywindow.print();
mywindow.close();
返回true;
}
这项工作非常好

它打开“打印”对话框,然后您可以“打印”或“另存为pdf”

我想做的是跳过打印对话框,并在函数结束时将其保存(下载)为pdf格式。我找到了很多关于如何使用jsPDF库的建议,但在我的案例中找不到任何简单的例子

非常感谢您的任何建议:)

我发现了一个很好的演示,展示了您所需要的

该页面底部还有一个代码笔链接,您可以在其中使用html标记查看完整的代码

简而言之,如果您的HTML标记如下所示

<div id="content">
    <h3>Sample h3 tag</h3>
    <p>Sample pararaph</p>
</div>
<button id="cmd">Generate PDF</button>
这可能有助于。。。
var doc = new jsPDF();

$('#cmd').click(function () {   
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
        'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});