Javascript 如何通过在html5中单击按钮将网页转换为pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13448584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert a web page into pdf by button click in html5
提问by user1808433
I have web page with containing data I want that when I click generate pdf button it should create pdf of that page. And Save Locally .
我有包含我想要的数据的网页,当我点击生成 pdf 按钮时,它应该创建该页面的 pdf。并在本地保存。
I have searched alot but I am getting script to only create pdf by entering data is there any other way
我已经搜索了很多,但我得到的脚本只能通过输入数据来创建 pdf 有没有其他方法
Below is the method which I tried but it is not suitable I want to create whole page into pdf file.
以下是我尝试过的方法,但它不适合我想将整个页面创建为 pdf 文件。
jspdf.com
jspdf.com
I also tried another code but it also does not create the file
我也尝试了另一个代码,但它也没有创建文件
<script>
function generatePDF(){}
var conv = new ActiveXObject("pdfServMachine.converter");
conv.convert("http://www.google.com", "c:\google.pdf", false);
WScript.Echo("finished conversion");
}
</script>
<body onload="generatePDF()">
</body>
</html>
回答by RRikesh
You didn't declare the function correctly. It should be generatePDF(){instead of generatePDF(){}. Your }should be at the end of the function only.
您没有正确声明该函数。它应该generatePDF(){代替generatePDF(){}. 你}应该只在函数的末尾。
回答by user2929270
I am a bit late but let me tell you that, the code you are using is only compatible with old versions of Microsoft browser(i.e) because it contains WScript & ActiveX Component.
我有点晚了,但让我告诉您,您使用的代码仅与旧版本的 Microsoft 浏览器(即)兼容,因为它包含 WScript 和 ActiveX 组件。
Yes, you can generate it with jsPDF.js. Just search this javascript file in google and download it locally then include it like shown below.
是的,您可以使用jsPDF.js. 只需在谷歌中搜索此 javascript 文件并在本地下载它,然后将其包含如下所示。
<script src="~/js/jspdf.js"></script>
<script>
var doc = new jsPDF('p', 'pt', 'letter');
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
$('#btn_Pdfprint').click(function () {
doc.fromHTML($('#myTabContent').html(), 25, 25, {
'width': 790,
'elementHandlers': specialElementHandlers
});
doc.save('mywebpagee.pdf');
window.location.reload();
});
</script>

