Now days, exporting data to PDF is common. The PDF files can contain links, button, tables, formatted data like doc file and can be electronically signed as actual documents.
Before starting, you need to download library file for jsPDF or you can use below CDN:
Let's take example, to create PDF from current webpage on click of button. This webpage will contain simple heading, paragraph, table and button. When user click on button it will convert current webpage into PDF and download it.
Create new file with any name and .html extension and update it as below:
In above example, we have created simple webpage and included jsPDF library file. When use click on button then our PDF generator function is called.
In this function, we will set PDF size as per pepper size and set height and width of PDF. Then we will convert body part of webpage and get HTML of it. At last, we will call save method of jsPDF and give provide one parameter which will be file name of new PDF file.
So whenever user click on download button, it will automatically convert webpage into PDF file and download it to user's system.
There are plenty of methods which can be used while generating PDF file like text(), addPage() or addImage() function. Please consider below example to use those functions :
As per above example, it will add custom data to PDF file like paragraph, page or image to our PDF. Here, we can append data using those function into our HTML based PDF file too.
Ask anything about this examples