admin管理员组文章数量:1356753
In my application I need to download pdf by parsing HTML on client side itself. For generating pdf on client side I am using jsPdf. Following is my code.
PdfGenerator.java
public static native String createPDF() /*-{
$wnd.createPDF();
}-*/;
entrypoint.html
function createPDF(){
try {
var doc = new jsPDF();
doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a crea');
doc.output('datauri');
var out = doc.output();
var url = 'data:application/pdf;base64,' + Base64.encode(out);
document.location.href = url;
} catch (e) {
return e.message;
}
return "";
};
I have added all the js in my project and defined script as well. But whenever I call this method then it is giving output "sprintf is not defined.". Please let me know if I am missing out something.
In my application I need to download pdf by parsing HTML on client side itself. For generating pdf on client side I am using jsPdf. Following is my code.
PdfGenerator.java
public static native String createPDF() /*-{
$wnd.createPDF();
}-*/;
entrypoint.html
function createPDF(){
try {
var doc = new jsPDF();
doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a crea');
doc.output('datauri');
var out = doc.output();
var url = 'data:application/pdf;base64,' + Base64.encode(out);
document.location.href = url;
} catch (e) {
return e.message;
}
return "";
};
I have added all the js in my project and defined script as well. But whenever I call this method then it is giving output "sprintf is not defined.". Please let me know if I am missing out something.
Share Improve this question edited Nov 23, 2018 at 3:41 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Aug 15, 2012 at 10:32 RASRAS 8,15017 gold badges66 silver badges86 bronze badges4 Answers
Reset to default 1I guess you forget to add the scripts for sprintf.js
and base64 js
. As jsPdf.js
internally uses both of these js.
entrypoint.html
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="sprintf.js"></script>
<script type="text/javascript" src="jspdf.js"></script>
please refer this link http://forums.webhosting.uk./web-designing-development/6718-jspdf-generating-your-pdf-web-page-documents-using-javascript.html
In the latest builds of jsPDF, you don't need base64 or sprintf, just jspdf.min.js found in the 'dist' folder, includes all the plugins (except downloadify/swfobject).
Just updating an old ticket if someone runs across it when trying to figure out jsPDF due to it's not so good documentation.
You don't need to plicate your code by using window.location
.
JsPDF has method .save()
to handle it.
function createPDF(){
try {
var doc = new jsPDF();
doc.text(20, 20, 'This PDF has a title, subject, author, keywords and a crea');
doc.save('file_name.pdf');
} catch (e) {
return e.message;
}
return "";
};
Depending on what browser you are supporting (all browser versions, and IE 10+), you don't even need to include Base64.encode()
. Just call btoa()
instead.
本文标签: javaGenerate PDF on client side using JSPDFStack Overflow
版权声明:本文标题:java - Generate PDF on client side using JSPDF - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744061293a2584171.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论