The PDF file format is a very convenient format to download a lot of data in the web application. So, it is advantageous for users to download dynamic content in file format for offline use. Firstly, with export to PDF functionality, the HTML content is converted to PDF document and downloaded as a PDF file. Secondly, in the dynamic web application, you can use a server-side script to convert HTML to PDF and generate PDF files using PHP.
If you want a solution to create a PDF document, then JavaScript is the easiest way to convert HTML to PDF. So, there is a lot of JavaScript library available for making PDF from HTML. But we prefer jsPDF because it is one of the best libraries to convert HTML to PDF with the help of JavaScript. With the help of the following tutorial, we will explain to you how to create a PDF document and convert HTML to PDF using jQuery and jsPDF library.
Include jQuery and jsPDF Library
Include the jQuery and jsPDF library files to use the jsPDF class.
<!-- jQuery library --> <script src="js/jquery.min.js"></script> <!-- jsPDF library --> <script src="js/jsPDF/dist/jspdf.min.js"></script>
Note: In this, there is no need to download the jsPDF library separately; There are all the essential files contain in our source code package.
Instantiate jsPDF Class
Use the following line of code to instantiate and use the jsPDF object in JavaScript.
var doc = new jsPDF();
Generate PDF using JavaScript
The example given below shows you how you can use the jsPDF library to create a PDF file using JavaScript.
- Firstly, specify the content in text() method of jsPDF object.
- Secondly, you have to use the addPage() method to add a new page to PDF.
- Thirdly, use the save() method to create and download PDF files.
var doc = new jsPDF(); doc.text(20, 20, 'Hello world!'); doc.text(20, 30, 'This is client-side Javascript to generate a PDF.'); // Add new page doc.addPage(); doc.text(20, 20, 'Visit CodexWorld.com'); // Save the PDF doc.save('document.pdf');
SEE ALSO: Upload and Add Watermark to Image using PHP
Convert HTML Content to PDF using JavaScript
The following example shows how to use the jsPDF library to convert HTML to PDF and generate PDF files from HTML content using JavaScript.
- Retrieve the HTML content from the specific element by ID or class.
- Convert HTML content of the specific part of the web page and generate PDF.
- Save and download the HTML content as a PDF file.
HTML Code:
<div id="content"> <!-- HTML contnet goes here --> </div> <div id="elementH"></div>
JavaScript Code:
var doc = new jsPDF(); var elementHTML = $('#contnet').html(); var specialElementHandlers = { '#elementH': function (element, renderer) { return true; } }; doc.fromHTML(elementHTML, 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }); // Save the PDF doc.save('sample-document.pdf');
SEE ALSO: Multiple Image Upload with View, Edit and Delete in PHP
Useful Configurations
The jsPDF library offers a lot of methods to configure PDF creation. In which, some useful methods of jsPDF class are given below which we can use to export HTML to PDF with the help of jQuery.
Change Paper Orientation:
Use the orientation
option to set the paper orientation of the PDF.
var doc = new jsPDF({ orientation: 'landscape' }); doc.text(20, 20, 'Hello world!'); doc.text(20, 30, 'This is client-side Javascript to generate a PDF.'); // Add new page doc.addPage(); doc.text(20, 20, 'Visit CodexWorld.com'); // Save the PDF doc.save('document.pdf');
Change Text Font:
Use setFont() and setFontType() methods to set text font and font-style in the PDF.
var doc = new jsPDF(); doc.text(20, 20, 'This is the default font.'); doc.setFont("courier"); doc.setFontType("normal"); doc.text(20, 30, 'This is courier normal.'); doc.setFont("times"); doc.setFontType("italic"); doc.text(20, 40, 'This is times italic.'); doc.setFont("helvetica"); doc.setFontType("bold"); doc.text(20, 50, 'This is helvetica bold.'); doc.setFont("courier"); doc.setFontType("bolditalic"); doc.text(20, 60, 'This is courier bolditalic.'); // Save the PDF doc.save('document.pdf');
Change Font Size:
Use setFontSize() method to set font size of the text in the PDF.
var doc = new jsPDF(); doc.setFontSize(24); doc.text(20, 20, 'This is a title'); doc.setFontSize(16); doc.text(20, 30, 'This is some normal sized text underneath.'); // Save the PDF doc.save('document.pdf');
Change Text Color:
Use setTextColor() method to set the color of the text in the PDF.
var doc = new jsPDF(); doc.setTextColor(100); doc.text(20, 20, 'This is gray.'); doc.setTextColor(150); doc.text(20, 30, 'This is light gray.'); doc.setTextColor(255,0,0); doc.text(20, 40, 'This is red.'); doc.setTextColor(0,255,0); doc.text(20, 50, 'This is green.'); doc.setTextColor(0,0,255); doc.text(20, 60, 'This is blue.'); // Save the PDF doc.save('document.pdf');
Conclusion
Firstly, our example code will help you to convert HTML to PDF and make PDF files with the help of JavaScript. Secondly, you can add the Export to PDF functionality on the web page without liable on the server-side script. You can enhance the PDF creation functionality with jsPDF configuration options as per your needs. At last, download our source code package to get all the required files, including the jsPDF JavaScript library.
Also, read our previous blog- Convert HTML to MS Word Document using PHP