How to Export/Generate html to pdf in web application

In this article, We are going to see the tutorials and examples to convert HTML to PDF using javascript and angular. In Web applications, Normally we have a data/images render on a webpage in different format ways as below.

  • Dynamic data is populated in the table 
  • Images are rendered on the web page
  • Any element under HTML DOM elements example DIV

To Export the generated HTML on the webpage to PDF, we have to write code to handle this.

JsPDF opensource library is a popular javascript framework to handle pdf/HTML manipulation in client side.
We will see the below tutorial about converting HTML elements like div or tables  to PDF document and download to it your system

Using Javascript and JQuery

First configure jsPDF js file in script tag. Better use CDN package which is always the latest code, Also add JQuery in a script tag

<script src=""></script>  
<script crossorigin="anonymous" src=""></script>  

Html table example for pdf

<div id="print-container">  
         <h3>Employees List</h3>  
         <table style="width:100%">  
      <button id="exportButton">Export table To PDF</button>  

Table data is rendered on the page. To convert it to PDF, the first step is to convert this table to canvas using Html2Canass library, then copy this canvas/image file to pdf using jsPDF library
Steps for the conversion process
1. Create and initialize jspdf object
2. Add image to this document javascript code to generate pdf

<br />   $('#exportButton').click(function() {<br />             convertPdf();<br />         });<br />         var doc = new jsPDF();<br />         function convertPdf() {<br />             html2canvas($('#print-container')[0], {<br />                 onrendered: function(canvas) {<br />    var imgWidth = 208;   <br />            var pageHeight = 295;    <br />                   var imgHeight = canvas.height * imgWidth / canvas.width;  <br />                   var img = canvas.toDataURL("image/png");<br />     var doc = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF  <br />                   doc.addImage(img, 'PNG', 0, 0,imgWidth, imgHeight);<br />         'test.pdf');<br />                 }<br />             });<br />         }<br />         <br /> 

button export button is clicked, using jquery we called convertPDF javascript method. JsPDF is the class used and syntax is jsPDF(orientation, unit, format) Parameters are
orientation - p portrait -l landscape these are basically used in printing documents in windows.
format - pdf format like A4, A5
unit - coordinate points measurement - pt points, - cm centimeters using

typescript in Angular Applications

As we have seen an example using plain Javascript. The angular framework is based on typescript language. We have to do some configuration angular cli apart from html/typescript component code. jsPDF provides npm packages for jsPDF.
First install jsPDF and html2canvas packages
html2canvas - HTML element to canvas image
jspdf - canvas image to pdf document

npm install jspdf --save  
npm install html2canvas --save  

Install both dependencies in your angular project.
Angular cli configuration

 "scripts": [  

if you don’t configure this script element in angular cli json file, we used to get the following error: ERROR TypeError: html2canvas_1.default is not a function

html code for generating pdf single page

import * as jspdf from 'jspdf';    
import * as html2canvas from "html2canvas"  
// this generates single page  
    var data = document.getElementById('print-container');    
    html2canvas(data).then(canvas => {    
      // Few necessary setting options    
      var imgWidth = 208;     
      var pageHeight = 295;      
      var imgHeight = canvas.height * imgWidth / canvas.width;    
      var heightLeft = imgHeight;    
      const contentDataURL = canvas.toDataURL('image/png')    
      let pdf = new jspdf('p', 'mm', 'a4'); // A4 size page of PDF    
      var position = 0;    
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)    'report.pdf'); // Generated PDF     

Generating multiple pdf pages

multiple pages can be geneated with below script of code  
      heightLeft -= pageHeight;  
      while (heightLeft >= 0) {  
        position = heightLeft - imgHeight;  
        pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight);  
        heightLeft -= pageHeight;  

Component html code for export pdf

<button click="" converttopdf="" id="exportButton">Export table To PDF</button>  

Output html to pdf pages. Please see the below screenshot for the output of the code Export html to pdf using typescript javascript

Similar Posts