How to Create Multipage PDF from HTML Using jsPDF and html2Canvas

Last Updated on 25 July 2018

There are a number of tools which can easily create PDF files from JSON or raw data provided. But in some situations, we have rich UI elements like images and data representation in graphs, so in that case, we may require PDF as it is.

jsPDF and html2canvas are really powerful tools which can help you to convert whole HTML page into multi-page PDF document, which you can show in iFrame or user can even download it.

See working demo here.

Let’s get started!

 

Step 1 – First we need to add the jQuery library into our project which is a basic dependency to get this work. Then we need to include latest jsPDF and Html2Canvas plugins.

 

Step 2 – As we have a long HTML page to get converted into multiple PDF pages, so will break whole HTML page into multiple chunks of pages with the help of selectors like we are using page1, page2 and page3 classes. Each selector or ID will be converted into

Each selector or ID will be converted into the canvas using Html2Canvas plugin and get pasted into PDF new page using the jsPDF plugin.

Here we have used a single function generatePDF() having html2canvas called three times to convert three sections of single HTML page into three pages of PDF file.

html2canvas function will create a canvas and add it as Image in PDF page. Using addPage function we are setting height and width of new PDF page. We can adjust it dynamically or as per application requirements.

In setTimeout we have added code to create download link and iFrame if we want to show PDF created in and in page iFrame. Which we will add in next step.

We will add a separate function to calculate Heights and Widths of HTML, Canvas and PDF page.

 

Step 3 – Here we will add HTML content which will be converted into a multipage PDF file.

 

After combining all sections above our final page will look like below:

 

That’s all folks!!! Let me know in the comments section if you face any issue in plugin this code sample into your app or if you have any suggestion always welcome.

Other Method: Create a Single canvas of the whole HTML section and break it up for different PDF pages.

Keep smiling 🙂

17
Leave a Reply

avatar
10 Comment threads
7 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
D HoppsarojJolly.exejayesh sharmajayesh Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Sarita
Guest
Sarita

My first page overlaps on last page

HK
Guest
HK

Hi,

I did try out the above mentioned code, but it seem some error on getting the content for page1, page2 and page3 on both iframe and generated PDF. The generated PDF only one blank page. is there anything went wrong with the code?

Yusuf
Guest
Yusuf

Hi, i have other question, but it still jsPDF problem, i use tab in my html code, and i need to render all, my question is : how to render all tab and merge them into one pdf file ?

Raf Van den Bosch
Guest
Raf Van den Bosch

Does this work correctly in firefox ? I have the impression that the order of the divs is printend randomly in the pdf.

Hemant
Guest
Hemant

thanks for this tutorial

Hemant
Guest
Hemant

if i want to stick the header and footer on every page then what will be the codes?

Sandeep
Guest

I have much content in my page it picks only some content as pdf .
Any suggestions please let me know

jayesh sharma
Guest
jayesh sharma

I have followed the same procedure but my first page would be blank by this.

saroj
Guest
saroj

Hi, .print-wrap { width: 700px; margin: auto; background: #1a577b; color: #fff; padding: 15px; margin-bottom: 30px; } when ever i make width 700px or 100% pdf download is not working but when width 500px it workes fine as per demo.Thanks for your great work.Please give me some suggestion how i fix it.

D Hopp
Guest
D Hopp

Please disregard my previous comment! I figured out that I messed up adding a new PDF instance instead of a page for pages 2 and up!!