0
votes

I am trying to save a div as a PDF using jsPDF. Here is my fiddle: https://jsfiddle.net/waleedbinahmed/bfj6qktv/11/ and here is a fiddle that someone else has uploaded: https://jsfiddle.net/waleedbinahmed/8s4qvu27/

The second fiddle is working fine.

I have tried to mimic the second fiddle i.e. including the same js and Jquery libraries. But on my fiddle, I am getting this error "Uncaught ReferenceError: jsPDF is not defined".

I don't know what I am missing on my fiddle that is causing the issue. Please help me out with what needs to be updated with respect to the second fiddle so that I can save a div a PDF.

JS library: https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.debug.js

Here is my code: HTML:

<div id="content">
  <h3>Hello, this is a H3 tag</h3>

  <p>A paragraph</p>
</div>
<div id="editor"></div>
<button onclick="javascript:createPdf()" id="cmd">generate PDF</button>

Javascript:

function createPdf() {
     var doc = new jsPDF();
    
      source = $('#content')[0];
    
      specialElementHandlers = {
        '#editor': function(element, renderer) {
          return true
        }
      };
    
      doc.fromHTML(
        source,
        15,
        15, {
          'width': 170,
          'elementHandlers': specialElementHandlers
        }
      );
      doc.save('sample-file.pdf')
    }

thanks.

1

1 Answers

0
votes

i did watch to your fiddle (the first one that was not working correctly), and just paste js library from the correct fiddle

and its working. sounds like your imported library doesn't work. and also the working fiddle has a jquery 3.4.1 implemented