
The image is an important parameter of the opt object. Margin, of course, the page margin we need on each PDF page, the filename is the name of the exported PDF file. The opt variable holds all the values which are defining the configuration of a pdf file. The image above is explaining things itself, I will explain the html2pdf() function call and opt variable. we are calling a JavaScript function here on the click event of this button generatePDF() function is the crux of this blog and the core of our example, let’s check the code.

Line 44: Export to PDF ButtonĬheck this line 44, I know a simple HTML button with some Bootstrap classes on it, but rather more important is the function we are calling here, yes onclick=”generatePDF()” parameter. you can code anything in this div but the most important part is the div ID or I would say element ID, which will be a reference to the PDF generation code for the html2pdf library. In our code, from line 40–82 is the element code, where we have to design the grey-bordered central element which has to be exported to pdf.

Line 40–82: The Div(Content) to be exported Line 12 and 13 are important, here we are integrating CDNs of jsPDF and html2pdf libraries. Note : Use of Bootstrap is purely optional. Line 7–13: Integration of all important CDNs įirstly integrate a stylesheet file style.css, where you want some look and feel tweaks in CSS, then I added Bootstrap CSS bundle and Bootstrap JS library CDNs, just for better webpage creation. Here, we are going to use jsPDF and html2pdf. There are many JavaScript libraries we could use here, including:

Here we create an HTML table with some design by CSS and also create a button where the user can click and download the HTML page as a PDF.Īlso Read: Export MySQL Data to Excel in PHP Using Ajax Instantiate jsPDF Class Download html page as pdf using JavaScript JS Create an HTML Content Div With CSS ĭemo of how to convert and Download HTML page to PDF file with CSS, using JavaScript and jQuery.
