Generate Multipage PDF using Single Canvas of HTML Document using jsPDF

jsPDF is a nice library to convert HTML content into PDF. We can put the different type of elements in PDF from HTML like an icon, images, text, CSS styles.
Here we will discuss an example of jsPDF to convert long HTML page into PDF document which will be generated client-side and download.

Let’s start…

Step 1) Include jsPDF and html2canvas liberary URl in head section of your HTML.

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.js"></script>

 

Step 2) We will add JS code for HTML to PDF conversion

In this code block we have used html2canvas function which will give canvas of HTML section we specified, then after getting canvas object we will add PDF page using a jsPDF method and add break-up of canvas s image(JPG) in PDF page.

There are some variables defined to automate Canvas width and HTML width height blunders during conversion. Names of variables are self-explanatory. In "totalPDFPages" we are getting total PDF pages we need to display whole data in HTML.

	function getPDF(){

		var HTML_Width = $(".canvas_div_pdf").width();
		var HTML_Height = $(".canvas_div_pdf").height();
		var top_left_margin = 15;
		var PDF_Width = HTML_Width+(top_left_margin*2);
		var PDF_Height = (PDF_Width*1.5)+(top_left_margin*2);
		var canvas_image_width = HTML_Width;
		var canvas_image_height = HTML_Height;
		
		var totalPDFPages = Math.ceil(HTML_Height/PDF_Height)-1;
		

		html2canvas($(".canvas_div_pdf")[0],{allowTaint:true}).then(function(canvas) {
			canvas.getContext('2d');
			
			console.log(canvas.height+"  "+canvas.width);
			
			
			var imgData = canvas.toDataURL("image/jpeg", 1.0);
			var pdf = new jsPDF('p', 'pt',  [PDF_Width, PDF_Height]);
		    pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin,canvas_image_width,canvas_image_height);
			
			
			for (var i = 1; i <= totalPDFPages; i++) { 
				pdf.addPage(PDF_Width, PDF_Height);
				pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height*i)+(top_left_margin*4),canvas_image_width,canvas_image_height);
			}
			
		    pdf.save("HTML-Document.pdf");
        });
	};

 

Step 3) Following is HTML part having container selector(canvas_div_pdf) which we used in JS code in step 2.

<div class="canvas_div_pdf">

<!--Add HTML content you want to convert to PDF-->

</div>

Other Method: We can have multiple sections on the page and we can convert each section to a canvas to add to separate PDF page.

See working demo here.

Let me know if you have more suggestions.

Happy Coding 🙂

Subscribe
Notify of
26 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

THIS IS AMAZINGGG. GRACIASSSS

Thank you so much for this code, you saved me a great amount of time ! I’d like to add a little correction for the vertical margins : when you add the imgData to PDF, you may want to count 2 of them for each page before, and one for the current page, by changing : pdf.addImage(imgData, ‘JPG’, top_left_margin, -(PDF_Height*i)+ (top_left_margin*4), canvas_image_width, canvas_image_height); To : pdf.addImage(imgData, ‘JPG’, top_left_margin, -(PDF_Height*i)+(top_left_margin*(2*i+1)), canvas_image_width,canvas_image_height); For cleaner result I also added white rectangles to cover the margins (add this twice : for the first page and the following ones) : pdf.setFillColor(255,255,255); pdf.rect(0, 0, PDF_Width, top_left_margin,… Read more »

man, you made my day. I was struggling with this sh*t since yesterday!

[…] to create a multi page pdf. Currently all i am getting back is a blank pdf. I found this function @ https://www.freakyjolly.com/jspdf-multipage-example-generate-multipage-pdf-using-single-canvas-of-ht&#8230; and i changed a few things so it would run in angular 8 with out […]

Greate. works fine for me…Thanks a lot.

how to save the generated pdf in a folder instead of downloading it. Thanks in advance

Thank you so much! Helped me a lot!