title: Download a div in a HTML page as pdf using jsPDF or javascript
body: <p>All I want to do is to print to pdf whatever is found in the div with an id of "pdf". This must be done using JavaScript. The "pdf" document should then be automatically downloaded with a filename of "foobar.pdf"
  I am using jsPDF script.It throws "Uncaught TypeError: Cannot read property 'name' of undefined"</p>
  
  <p>exception[enter link description here][1]</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> function demoFromHTML() {
          debugger;
          var pdf = new jsPDF('p', 'pt', 'letter');
          source = $('#divInvoice')[0];
          var Name = $("#spnorderno").text();
          specialElementHandlers = {
              '#editor': function (element, renderer) {
                  return true
              }
          };
          margins = {
              top: 80,
              bottom: 60,
              left: 40,
              width: 522
          };
          pdf.fromHTML(
          source,
          margins.left, 
          margins.top, { 
              'width': margins.width,
              'elementHandlers': specialElementHandlers
          },
          function (dispose) {
              pdf.save('Invoice'+Name+'.pdf');
          }, margins);
      }</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 src="https://code.jquery.com/jquery-2.1.4.min.js"&gt;&lt;/script&gt;
  &lt;script src="http://mrrio.github.io/jsPDF/dist/jspdf.debug.js"&gt;&lt;/script&gt;
  &lt;div style="width:800px; border-top:5px solid #1dbbe3; margin:0px auto; padding:25px; border-left:1px solid #ccc;  border-bottom:1px solid #ccc;  border-right:1px solid #ccc; font-family: Arial; font-size:13px; font-weight:normal; text-decoration:none; line-height:19px;"&gt;
  &lt;div class="printBox"&gt;
    &lt;table width="100%" style="margin-bottom:30px;"&gt;
      &lt;tr&gt;
        &lt;td align="left" valign="middle"&gt;LOGO&lt;/td&gt;
        &lt;td align="right" valign="middle" style="font-size:20px;"&gt;LAB&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align="left" valign="middle" height="60"&gt;&lt;span style="color:#757575;"&gt;Address:&lt;/span&gt;&lt;br&gt;
          &lt;span&gt;INDIA&lt;/span&gt;&lt;/td&gt;
        &lt;td align="right" valign="top"&gt;&lt;span style="color:#757575;"&gt;Address:&lt;/span&gt;&lt;br&gt;
          &lt;span&gt;INDIA&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td align="left" valign="middle"&gt;&lt;span style="color:#757575;"&gt;Contact No.:&lt;/span&gt; &lt;br&gt;
          &lt;span&gt;+000000000000&lt;/span&gt;&lt;/td&gt;
        &lt;td align="right" valign="middle"&gt;&lt;span style="color:#757575;"&gt;Contact No.:&lt;/span&gt; &lt;br&gt;
          &lt;span&gt;+000000000&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;table width="100%" style="border-top:3px solid #ccc; margin-top:15px;"&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;"&gt;&lt;span style="color:#757575;"&gt;Patient name:&lt;/span&gt; &lt;span style="color:#1dbae3; font-weight:bold;"&gt;Rahul Kumar Sharma&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;"&gt;&lt;span style="color:#757575;"&gt;Age:&lt;/span&gt; &lt;span&gt;25 year&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="padding: 7px 0 20px;"&gt;&lt;span style="color:#757575;"&gt;Gender: &lt;/span&gt;&lt;span&gt;male&lt;/span&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;table width="100%" style="border-top:3px solid #ccc; margin-top:15px;" cellpadding="0" cellspacing="0"&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 15px 0 12px; font-size:16px;" colspan="3" align="left"&gt;Order No: &lt;span style="color:#fe8534;"&gt;O1500002200&lt;/span&gt;&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 15px 0 12px;" colspan="2" align="right"&gt;&lt;span style="color:#757575;"&gt;Order Date:&lt;/span&gt; 28 October 2015 11:07AM&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px; font-weight:bold;" width="7%" align="left"&gt;S. No.&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px; font-weight:bold;" colspan="3" align="left"&gt;Investigations&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px; font-weight:bold;" width="19%" align="right" valign="middle"&gt;Amount&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="left" valign="middle"&gt;1&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" colspan="3" align="left" valign="middle"&gt;Senior Citizen Package Male&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 2000&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="left" valign="middle"&gt;2&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" colspan="3" align="left" valign="middle"&gt;Senior Citizen Package Male&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 2000&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="left" valign="middle"&gt;3&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" colspan="3" align="left" valign="middle"&gt;Senior Citizen Package Male&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 2000&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="left" valign="middle"&gt;4&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" colspan="3" align="left" valign="middle"&gt;Senior Citizen Package Male&lt;/td&gt;
        &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 2000&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td colspan="5"&gt;&lt;table width="100%" cellpadding="0" cellspacing="0"&gt;
            &lt;tr&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px; font-weight:bold;" width="82%" align="right" valign="middle"&gt;Sub Total:&lt;/td&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px; font-weight:bold;" width="18%" align="right" valign="middle"&gt;Rs. 12000&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Discount:&lt;/td&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 12000&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Promo Discount:&lt;/td&gt;
              &lt;td style="border-bottom:1px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Rs. 12000&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td style="border-bottom:3px solid #ccc;padding: 6px 0 8px;" align="right" valign="middle"&gt;Your Price:&lt;/td&gt;
              &lt;td style="border-bottom:3px solid #ccc;padding: 10px 0 10px; font-size:19px;" align="right" valign="middle"&gt;&lt;b&gt;Rs. 12000&lt;/b&gt;&lt;/td&gt;
            &lt;/tr&gt;
          &lt;/table&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/div&gt;
    &lt;div class="printBtnBox" style="margin-top: 25px;"&gt;
    &lt;a href="#" style="background:#1dbae3; padding: 8px 20px; border-radius:3px; color:#fff; text-decoration:none; font-size:16px;" onclick='demoFromHTML()'&gt;Download&lt;/a&gt;
    &lt;/div&gt;
    
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>