Create PDF file from HTML using jsPDF Javascript Library on Client-Side
There may be situations where it is necessary to produce PDF files while developing a web application. It may need to be printed especially in cases such as reporting and invoicing.
PDF file creation in the web application can be performed in two ways. On the client-side and on the server-side ( backend). In this post, I will explain how to create a PDF file from HTML template using javascript libraries on the client-side. If you want to generate PDF from HTML template on server side, you can use weasyprint library in Python and DOMPDF in PHP.
Before moving on to our example, make sure you have npm(node package manager) installed on your computer.
In our example, we will use 2 Javascript libraries.
1- jsPDF
jsPDF is a javascript library used to generate PDF documents on the client side. The documentation is pretty good. jsPDF is written according to Asynchronous module definition (AMD) standards. We will install the library into our project by running the following line of code.
npm install jspdf --save
If you want to include the library in the project as cdn, you can use the script tag below.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js"></script>
2- html2canvas
To install the html2canvas library, we will download it from github to our computer.
git clone git://github.com/niklasvh/html2canvas.git
Then, we open the terminal screen in the folder we have installed on our computer and install the dependencies.
npm install
Finally we build to create browser package.(unification, uglification, minification)
npm run build
html2canvas reads the different styles applied to the DOM and elements, rendering the current page as a canvas image.
Example
I have prepared the example in this post as a single page HTML file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="html-template"> <div style="margin-top: 50px;margin-left: 50px;"> <div style="min-width: 600px"> <header> <div class="row"> <div class="col"> <a target="_blank" href="https://etemkeskin.com"> <img style="max-width: 70px;" src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABlCAYAAAC7vkbxAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAniSURBVHhe7Z17jBtHHcdndn0Jl0fbS6kE4tG0ShGEh0ppCSRqkFKC2rS0SiEIhCiPiiiEXG3n7LWTtMWFkPOuL2dfLkmViJbyKIGmQERaFbW0PC99ENI/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eVyNj2tUCgUCoVCoVAoFAqFQqFQKBQK7zD2H1oPDEpzIUUFAAAAAElFTkSuQmCC'/> <span style="font-size: 26px;"> www.etemkeskin.com</span> </a> </div> <br> <div class="col company-details"> <div>455 Foggy Heights, AZ 85004, US</div> <div>(123) 456-789</div> <div>etemkeskin@example.com</div> </div> </div> </header> <main> <div> <div> <div>INVOICE TO:</div> <h2>John Doe</h2> <div>796 Silver Harbour, TX 79273, US</div> <div><a href="mailto:john@example.com">john@example.com</a></div> </div> <div class="col invoice-details"> <h1 style="color: darkcyan;">INVOICE 3-2-1</h1> <div >Date of Invoice: 01/10/2018</div> <div >Due Date: 30/10/2018</div> </div> </div> <table border="0" cellspacing="0" cellpadding="0"> <thead> <tr> <th>#</th> <th class="text-left">DESCRIPTION</th> <th class="text-right">HOUR PRICE</th> <th class="text-right">HOURS</th> <th class="text-right">TOTAL</th> </tr> </thead> <tbody> <tr> <td class="no">01</td> <td class="text-left"><h3>Website Design</h3>Creating a recognizable design solution based on the company's existing visual identity</td> <td class="unit">$40.00</td> <td class="qty">30</td> <td class="total">$1,200.00</td> </tr> </tbody> <tfoot> <tr> <td colspan="2"></td> <td colspan="2">SUBTOTAL</td> <td>$1,200.00</td> </tr> <tr> <td colspan="2"></td> <td colspan="2">TAX 25%</td> <td>$300.00</td> </tr> <tr> <td colspan="2"></td> <td colspan="2">GRAND TOTAL</td> <td>$1,500.00</td> </tr> </tfoot> </table> </main> <footer> Invoice was created on a computer and is valid without the signature and seal. </footer> </div> </div> </div> <br><br> <button type="button" onclick="convertHTMLToPDF()" style="margin-left: 350px;padding: 7px; color: white; background-color: darkslategrey;">PDF olarak Kaydet</button> </body> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js"></script> <script type="text/javascript" src="./html2canvas.js"></script> <script> function convertHTMLToPDF() { const { jsPDF } = window.jspdf; var doc = new jsPDF('l', 'mm', [1200, 1810]); var pdfjs = document.querySelector('#html-template'); doc.html(pdfjs, { callback: function(doc) { doc.save("output.pdf"); }, x: 10, y: 10 }); doc.output('dataurlnewwindow'); } </script> </html>
We run the HTML file and click the save as PDF button. The generated PDF file will be saved on our computer.
In this post, I explained how to export PDF file at web application quickly and simply.
Good luck…
Sources
- https://github.com/MrRio/jsPDF
- https://github.com/niklasvh/html2canvas
- https://codebeautify.org/image-to-base64-converter