Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate pdf from HTML in div using Javascript

I have the following html code:

<!DOCTYPE html> <html>     <body>         <p>don't print this to pdf</p>         <div id="pdf">             <p><font size="3" color="red">print this to pdf</font></p>         </div>     </body> </html> 

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've been using jspdf to do this, but the only function it has is "text" which accepts only string values. I want to submit HTML to jspdf, not text.

like image 882
John Crawford Avatar asked Aug 12 '13 16:08

John Crawford


People also ask

How do I export my HTML page as PDF using JavaScript?

Generate PDF using JavaScript The following example shows how to use the jsPDF library to generate PDF file using JavaScript. Specify the content in text() method of jsPDF object. Use the addPage() method to add new page to PDF. Use the save() method to generate and download PDF file.

How do I convert a div to PDF?

To download a div in a HTML page as PDF using JavaScript, we can use the jsPDF library. import { jsPDF } from "jspdf"; const doc = new jsPDF(); doc. text("Hello world!", 10, 10); doc.

How do I convert HTML elements to PDF?

Use the jsPDF Library to Convert HTML to PDF Check the code below. Copy var source = window. document. getElementsByTagName("body")[0]; var specialElementHandlers = { '#hidden-element': function (element, renderer) { return true; } }; var doc = new jsPDF({ orientation: 'landscape' }); doc.


2 Answers

jsPDF is able to use plugins. In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:

  1. Go to https://github.com/MrRio/jsPDF and download the latest Version.
  2. Include the following Scripts in your project:
    • jspdf.js
    • jspdf.plugin.from_html.js
    • jspdf.plugin.split_text_to_size.js
    • jspdf.plugin.standard_fonts_metrics.js

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF. Therefore your HTML should look like this:

<!DOCTYPE html> <html>   <body>     <p id="ignorePDF">don't print this to pdf</p>     <div>       <p><font size="3" color="red">print this to pdf</font></p>     </div>   </body> </html> 

Then you use the following JavaScript code to open the created PDF in a PopUp:

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"); 

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.

Please note that the special element handlers only deal with IDs in the current version, which is also stated in a GitHub Issue. It states:

Because the matching is done against every element in the node tree, my desire was to make it as fast as possible. In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported.

Therefore replacing '#ignorePDF' with class selectors like '.ignorePDF' did not work for me. Instead you will have to add the same handler for each and every element, which you want to ignore like:

var elementHandler = {   '#ignoreElement': function (element, renderer) {     return true;   },   '#anotherIdToBeIgnored': function (element, renderer) {     return true;   } }; 

From the examples it is also stated that it is possible to select tags like 'a' or 'li'. That might be a little bit to unrestrictive for the most usecases though:

We support special element handlers. Register them with jQuery-style ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) There is no support for any other type of selectors (class, of compound) at this time.

One very important thing to add is that you lose all your style information (CSS). Luckily jsPDF is able to nicely format h1, h2, h3 etc., which was enough for my purposes. Additionally it will only print text within text nodes, which means that it will not print the values of textareas and the like. Example:

<body>   <ul>     <!-- This is printed as the element contains a textnode -->             <li>Print me!</li>   </ul>   <div>     <!-- This is not printed because jsPDF doesn't deal with the value attribute -->     <input type="textarea" value="Please print me, too!">   </div> </body> 
like image 75
snrlx Avatar answered Oct 09 '22 04:10

snrlx


This is the simple solution. This works for me. You can use the javascript print concept and simple save this as pdf.

<html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title></title>     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>     <script type="text/javascript">         $("#btnPrint").live("click", function () {             var divContents = $("#dvContainer").html();             var printWindow = window.open('', '', 'height=400,width=800');             printWindow.document.write('<html><head><title>DIV Contents</title>');             printWindow.document.write('</head><body >');             printWindow.document.write(divContents);             printWindow.document.write('</body></html>');             printWindow.document.close();             printWindow.print();         });     </script> </head> <body>     <form id="form1">     <div id="dvContainer">         This content needs to be printed.     </div>     <input type="button" value="Print Div Contents" id="btnPrint" />     </form> </body> </html> 
like image 40
vaibhav kulkarni Avatar answered Oct 09 '22 03:10

vaibhav kulkarni