Generate PDF on client side using JSPDF
Solution 1
I 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.com/web-designing-development/6718-jspdf-generating-your-pdf-web-page-documents-using-javascript.html
Solution 2
You don't need to complicate 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 "";
};
Solution 3
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.
RAS
Updated on June 26, 2022Comments
-
RAS almost 2 years
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.