最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Generate PDF on client side using JSPDF - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 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./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.

发布评论

评论列表(0)

  1. 暂无评论