title: Export HTML page to PDF on user click using JavaScript
body: <p>When user click on GeneratePDF button, I need to export the html page into a PDF file. I am successfully exporting HTML page into PDF file but for the first click only I am able to download data into PDF but from the second click I am unable to download the data to PDF file. I am not sure where I went wrong in the code.</p>
  
  <p>Please check the code here:  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() {
    var doc = new jsPDF();
    var specialElementHandlers = {
      '#editor': function(element, renderer) {
        return true;
      }
    };
    $('#cmd').click(function() {
      doc.fromHTML($('#target').html(), 15, 15, {
        'width': 170,
        'elementHandlers': specialElementHandlers
      });
      doc.save('sample-file.pdf');
    });
  });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="https://code.jquery.com/ui/1.12.0-beta.1/jquery-ui.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.1.135/jspdf.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://cdn.uriit.ru/jsPDF/libs/adler32cs.js/adler32cs.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2014-11-29/FileSaver.min.js
  "&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="libs/Blob.js/BlobBuilder.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://cdn.immex1.com/js/jspdf/plugins/jspdf.plugin.addimage.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://cdn.immex1.com/js/jspdf/plugins/jspdf.plugin.standard_fonts_metrics.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://cdn.immex1.com/js/jspdf/plugins/jspdf.plugin.split_text_to_size.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="http://cdn.immex1.com/js/jspdf/plugins/jspdf.plugin.from_html.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/basic.js"&gt;&lt;/script&gt;
  
  &lt;body id="target"&gt;
    &lt;div id="content"&gt;
      &lt;h3&gt;Hello, this is a H3 tag&lt;/h3&gt;
      &lt;a class="upload"&gt;Upload to Imgur&lt;/a&gt; 
      &lt;h2&gt;this is &lt;b&gt;bold&lt;/b&gt; &lt;span style="color:red"&gt;red&lt;/span&gt;&lt;/h2&gt; 
      &lt;p&gt;Feedback form with screenshot This script allows you to create feedback forms which include a screenshot, created on the clients browser, along with the form. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation
        as it does not make an actual screenshot, but builds the screenshot based on the information available on the page. How does it work? The script is based on the html2canvas library, which renders the current page as a canvas image, by reading the
        DOM and the different styles applied to the elements. This script adds the options for the user to draw elements on top of that image, such as mark points of interest on the image along with the feedback they send. It does not require any rendering
        from the server, as the whole image is created on the clients browser. No plugins, no flash, no interaction needed from the server, just pure JavaScript! Browser compatibility Firefox 3.5+ Newer versions of Google Chrome, Safari &amp; Opera IE9
      &lt;/p&gt;
  
    &lt;/div&gt;
    &lt;button id="cmd"&gt;generate PDF&lt;/button&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>