How to Generate a PDF with jQuery, PDF Generator Tips - ByteScout
  • Home
  • /
  • Blog
  • /
  • How to Generate a PDF with JQuery

How to Generate a PDF with JQuery

Saving web page content is a useful function to be able to integrate into your site and a very common requirement in web development. PDF documents are the easiest and most portable way to save this content. Many people have the misconception that Javascript cannot be used for generating PDFs and that PDFs can only be generated from the server-side; this is not true, and jQuery can allow you to place this power in the hands of your users, by allowing you to create relatively simple code to embed this ability on your site. In this tutorial, we will show you how to generate a PDF with a jQuery PDF generator.

The task of saving content as a PDF file requires the use of some third party JS APIs. In this example, we have used the client-side JSPDF library to generate a PDF. This library is limited to converting only simple HTML code to PDF

Also consider ByteScout: Web API for full-featured HTML to PDF and PDF transformations

If you are looking to convert full-featured HTML with CSS and images from code or URL into rich PDF documents then consider service with REST API and Zapier support.

Generate PDF Files

Our code

Creade Files pdf

The HTML page we’ll be outputting as a PDF…

Create a PDF File

…and our final generated PDF file!

The steps we’ve taken to generate a PDF using this approach are:

  1. Download jQuery and the JSPDF open-source library from the appropriate Content Delivery Network:
    1. You can download jquery here:
    2. You can download JSPDF here:

(Note: CDNs are preferred as a source of these libraries rather than downloading *.js files because libraries are kept updated. In addition, if the libraries are downloaded from the same CDN, your browser can have the library saved already through some other website, reducing time on multiple projects).

  1. Create a new HTML file, including the JS libraries in the <Script> tag. Here in the sample code, we have used the CDN URLs:
<script src=””></script>

<script src=””></script>

  1. Define the HTML body to contain the two-division tags, displaying different information on the screen. The button we’re making here is labeled “Generate PDF”, and will be used to trigger the PDF generation process.
<div id=”PDFcontent“>

<h3>First PDF</h3>


<p>Content to be written in PDF can be placed in this DIV!</p>


<div id=”ignoreContent“>

<p>Only for display and not in pdf</p>


<button id=”gpdf”>Generate PDF</button>

  1. Initialize the JSPDF object in the script tag, and define its properties. When the “Generate PDF” button is pressed, the function will be triggered.  This function is included inside the document-ready function, which will prevent the function listed within the block from being invoked when the complete page is not loaded.

$(“#gpdf”) is the jQuery selector that helps to monitor clicks on the button with id=”gpdf”. Once clicked, it will invoke the code written within the click block. The function triggered will define the dimensions of the pdf file to be generated, and the object “specialElementHandlers”, which will define any additional properties to be taken under consideration while writing the data into the PDF. In this example, the code will bypass the text defined under the “ignoreContent” div, and will only display the content defined under the “PDFcontent” div.


var pdfdoc = new jsPDF();

var specialElementHandlers = {

‘#ignoreContent’: function (element, renderer) {

return true;






pdfdoc.fromHTML($(‘#PDFcontent’).html(), 10, 10, {

‘width’: 110,

‘elementHandlers’: specialElementHandlers




In the generated file, only the contents in “PDFcontent“ will be displayed. And this means we’re done – our file will be generated with the filename First.pdf!

See also:

FREE Bytescout PDF Generator SDK for Javascript library that you can use to generate PDFs invoices, reports, statements, with text with font formatting, links, tables, and images.
Powerful REST Web API (with Zapier and other integrations supported) – Web API with a full set of functions for HTML to PDF, documents to pdf, images to pdf, websites to pdf transformations.



About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.