0

I am using code suggested in following link. Following is the function for pdf conversion which i have used.

function pdfGenerator(){
    var doc = new jsPDF();    
    var elementHandler = {
              '#ignorePDF': function (element, renderer) {
                return true;
              }
            };
    var source = window.document.getElementsByTagName("body")[0];
    doc.fromHTML(
        source,
        15,
        15,
        {
          'width': 180,'elementHandlers': elementHandler
        });

    doc.output("dataurlnewwindow");
}

I have downloaded latest version mentioned in the following link and added it as external jar. Can u pleases suggest how to include scripts of

  • jspdf.js
  • jspdf.plugin.from_html.js
  • jspdf.plugin.split_text_to_size.js
  • jspdf.plugin.standard_fonts_metrics.js

in my project? I am getting blank pdf as output.

Community
  • 1
  • 1
Radhika Kulkarni
  • 298
  • 1
  • 4
  • 19

2 Answers2

0

You can use defer attribute of script tag.

<script defer src="jspdf.js"></script>
<script defer src="jspdf.plugin.from_html.js"></script>
<script defer src="jspdf.plugin.split_text_to_size.js"></script>
<script defer src="jspdf.plugin.standard_fonts_metrics.js"></script>
<script defer src="/* YOUR SCRIPT HERE */"></script>

This way all scripts will be loaded synchroniously, one after one. More info @ MDN

Ivan Shmidt
  • 163
  • 1
  • 7
  • Hey! I have added scripts as u have suggested and removed removed the zip file of jspdf which i had added externally. But, now no pdf is generated and i am getting following error in chrome console jspdf.plugin.from_html.js:424 Uncaught ReferenceError: $ is not definedGetCSS @ jspdf.plugin.from_html.js:424DrillForContent @ jspdf.plugin.from_html.js:492process @ jspdf.plugin.from_html.js:550jsPDFAPI.fromHTML @ jspdf.plugin.from_html.js:591pdfGenerator @ prefilledform.js:37onclick @ DayCare.jsp:111 – Radhika Kulkarni Aug 22 '16 at 08:55
  • It seems this code is using **jquery** inside itself, so simply add `` in the beginning – Ivan Shmidt Aug 23 '16 at 09:40
0

It is better to use the .save function instead of doc.output to make it as a downloadable pdf, Ensure that you have placed this inside a settimeout statement as well. Here is the refined code.

function pdfGenerator(){
    var doc = new jsPDF();    
    var elementHandler = {
              '#ignorePDF': function (element, renderer) {
                return true;
              }
            };
    var source = window.document.getElementsByTagName("body")[0];
    doc.fromHTML(
        source,
        15,
        15,
        {
          'width': 180,'elementHandlers': elementHandler
        });

       setTimeout(function(){
          doc.save('test');
       },2000);
}

Hope this helps!

David R
  • 14,711
  • 7
  • 54
  • 72