🌐
CodePen
codepen.io › REMCOOLE › pen › xxOGoKo
html to pdf using jsPDF
$('.cmd').click(function () { var pdf = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; var $addr = $(this).closest('.res').find('.content'); var $temp = $('.content-template'); $temp.find('h3').text($addr.find('h3').text()); pdf.fromHTML($temp.html(), 15, 15, { 'width': 170, 'elementHandlers':specialElementHandlers } ); pdf.save('sample-file.pdf'); }); !
🌐
CodePen
codepen.io › amkid › pen › qKYwXo
jsPDF from html
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.4/jspdf.plugin.autotable.min.js"></script> <div class="zima"> <h1> Header </h1> <p> content </p> </div> <button id="getPDF" onclick="getPDF()">Download PDF</button> ... function getPDF() { var doc = new jsPDF(); // We'll make our own renderer to skip this editor var specialElementHandlers = { '#getPDF': function(element, renderer){ return true; }
🌐
CodePen
codepen.io › phpcodertech › pen › yLMrJvv
Generate PDF From HTML JavaScript
PDF</button> </p> Ref:<a ... }; //margins.left, // x coord margins.top, { // y coord $('#generatePDF').click(function () { doc.fromHTML($('#htmlContent').html(), ......
🌐
CodePen
codepen.io › shoma › pen › oELgQb
HTML to PDF
$(function () { var specialEle... } }; $('#cmd').click(function () { var doc = new jsPDF(); doc.fromHTML($('#target').html(), 15, 15, { 'width': 170,'elementHandlers': specialElementHandlers }); doc.save('sample-file.pdf'); }); }); ...
🌐
PDF Generation API
pdfgeneratorapi.com › home › 3 ways to generate pdf from html with javascript
3 Ways to Generate PDF from HTML with JavaScript - PDF Generator API
May 8, 2025 - Set it as the src parameter to an iframe in Codepen to display the generated PDF. You can find an example here. After you have your unique URL from the Open Editor endpoint, you can display the PDF in an iframe with simple HTML on your dashboard.
🌐
CodePen
codepen.io › avnishjayaswal › pen › xxWZRmZ
jspdf html to pdf Example
$(document).ready(function () { ... { return true; } }; pdf.fromHTML($('#main').html(), 15, 15, { 'width': 500, 'elementHandlers': specialElementHandlers } ); pdf.save('sample-file.pdf'); }); }); !...
🌐
CodePen
codepen.io › ianaya89 › pen › JoRNyK
jsPDF Demo
Minimize HTML Editor · Fold All · Unfold All · <h1>jsPDF Demo</h1> <p>This is a sample of how to use <a href="https://parall.ax/products/jspdf">jsPDF</a>.</p> <p> Write some text and donwload a pdf file with your content</p> <textarea id="txtContent" cols="60" rows="15"></textarea> <br /> <button id="btnDownload"> Download PDF </button> !
🌐
CodePen
codepen.io › v213 › pen › wLpMzv
HTML to PDF via Javascript
<div id="content"> <h1>The title goes here</h1> <p>The pararaph goes here</p> </div> <div id="page"></div> <button id="submit">Export to PDF</button> <!-- This code example was found here ... https://www.quora.com/How-do-I-export-my-HTML-page-as-PDF-using-JavaScript/answer/Jitendraamishra --> ... var doc = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#submit').click(function () { doc.fromHTML($('#content').html(), 15, 15, { 'width': 190, 'elementHandlers': specialElementHandlers }); doc.save('sample-page.pdf'); });
🌐
CodePen
codepen.io › plentzo › pen › ZbMgrq
JsPDF Playground
<button onclick="generate()">Generate PDF</button> <div id="output" style="font: 14pt sans-serif"> <h1 style="font-family: Arial; color: red">Hello world</h1> <p> This is a test paragraph. <strong>It's pretty cool</strong> </p> <table> <thead> <tr> <th>Column 1</th> <th>Column 2</th> </tr> </thead> <tbody> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </tbody> </table> </div> ... function generate() { var doc = new jsPDF(); doc.fromHTML(document.querySelector('#output'), 15, 15, { 'width': 170, 'elementHandlers': function() { return true; } }); doc.save('test.pdf'); }
Find elsewhere
🌐
CodePen
codepen.io › muneebul › pen › yLyYBvE
Javascript pdf Example (jsPDF)
Minimize HTML Editor · Fold All · Unfold All · <div id="content"> <h3>Generate PDF files in client-side JavaScript</h3> <p>href="https://parall.ax/products/jspdf#"</p> <p>Or refer to the YouTube video: https://www.youtube.com/watch?v=CnprxD_sJFE<p> </div> <div id="editor"></div> <button id="cmd">Generate PDF</button> !
🌐
CodePen
codepen.io › brownsugar › pen › rNyOjaM
html2canvas with jsPDF
} ], pageTotal: 5, working: false }; }, methods: { async download() { this.working = true; await this.$nextTick(); // A4 in 96dpi // 29.7cm x 21cm const pageWidth = 1123; const pageHeight = 794; const doc = new jsPDF({ orientation: "l", unit: "px", hotfixes: ["px_scaling"], format: [pageWidth, pageHeight], compressPdf: true }); // Slower, Larger, But maintain the html elements // doc.html(this.$refs.pdf, { // callback: async doc => { // const pageCount = doc.internal.getNumberOfPages(); // doc.deletePage(pageCount); // await doc.save("favorites.pdf"); // this.working = false; // }, // }); // F
🌐
CodePen
codepen.io › esbenjuul › pen › XRPQxG
jsPDF
* http://opensource.org/licenses/mit-license */ function html2pdf (html,pdf,callback) { var canvas = pdf.canvas; if (!canvas) { alert('jsPDF canvas plugin not installed'); return; } canvas.pdf = pdf; pdf.annotations = { _nameMap : [], ...
🌐
CodePen
codepen.io › ahmadaar › pen › yreEpa
Export PDF from html table with JS
var specialElementHandlers = { // element with id of "bypass" - jQuery style selector '.no-export': function (element, renderer) { // true = "handled elsewhere, bypass text extraction" return true; } }; function exportPDF(id) { var doc = new jsPDF('p', 'pt', 'a4'); //A4 - 595x842 pts //https://www.gnu.org/software/gv/manual/html_node/Paper-Keywords-and-paper-size-in-points.html //Html source var source = document.getElementById(id); console.log(source); var margins = { top: 10, bottom: 10, left: 10, width: 595 }; doc.fromHTML( source, // HTML string or DOM elem ref.
🌐
CodePen
codepen.io › connectpritam › pen › wvKqwLG
Save HTML page to PDF document via Javascript
var doc = new jsPDF(); var ... true; } }; $('#submit').click(function () { doc.fromHTML($('#content').html(), 15, 15, { 'width': 190, 'elementHandlers': specialElementHandlers }); doc.save('sample-page.pdf'); }); ...
🌐
SitePoint
sitepoint.com › blog › javascript › generating pdfs from web pages on the fly with jspdf
Generating PDFs from Web Pages on the Fly with jsPDF — SitePoint
November 7, 2024 - Massimo Cassandro demonstrates how to make use of jsPDF, a JavaScript library for generating PDF documents from web pages.
Top answer
1 of 16
325

jsPDF is able to use plugins. In order to enable it to print HTML, you have to include certain plugins and therefore have to do the following:

  1. Go to https://github.com/MrRio/jsPDF and download the latest Version.
  2. Include the following Scripts in your project:
  • jspdf.js
  • jspdf.plugin.from_html.js
  • jspdf.plugin.split_text_to_size.js
  • jspdf.plugin.standard_fonts_metrics.js

If you want to ignore certain elements, you have to mark them with an ID, which you can then ignore in a special element handler of jsPDF. Therefore your HTML should look like this:

<!DOCTYPE html>
<html>
  <body>
    <p id="ignorePDF">don't print this to pdf</p>
    <div>
      <p><font size="3" color="red">print this to pdf</font></p>
    </div>
  </body>
</html>

Then you use the following JavaScript code to open the created PDF in a PopUp:

var doc = new jsPDF();          
var elementHandler = {
  '#ignorePDF': function (element, renderer) {
    return true;
  }
};
var source = window.document.getElementsByTagName("body")[0];
doc.fromHTML(
    source,
    15,
    15,
    {
      'width': 180,'elementHandlers': elementHandler
    });

doc.output("dataurlnewwindow");

For me this created a nice and tidy PDF that only included the line 'print this to pdf'.

Please note that the special element handlers only deal with IDs in the current version, which is also stated in a GitHub Issue. It states:

Because the matching is done against every element in the node tree, my desire was to make it as fast as possible. In that case, it meant "Only element IDs are matched" The element IDs are still done in jQuery style "#id", but it does not mean that all jQuery selectors are supported.

Therefore replacing '#ignorePDF' with class selectors like '.ignorePDF' did not work for me. Instead you will have to add the same handler for each and every element, which you want to ignore like:

var elementHandler = {
  '#ignoreElement': function (element, renderer) {
    return true;
  },
  '#anotherIdToBeIgnored': function (element, renderer) {
    return true;
  }
};

From the examples it is also stated that it is possible to select tags like 'a' or 'li'. That might be a little bit to unrestrictive for the most usecases though:

We support special element handlers. Register them with jQuery-style ID selector for either ID or node name. ("#iAmID", "div", "span" etc.) There is no support for any other type of selectors (class, of compound) at this time.

One very important thing to add is that you lose all your style information (CSS). Luckily jsPDF is able to nicely format h1, h2, h3 etc., which was enough for my purposes. Additionally it will only print text within text nodes, which means that it will not print the values of textareas and the like. Example:

<body>
  <ul>
    <!-- This is printed as the element contains a textnode -->        
    <li>Print me!</li>
  </ul>
  <div>
    <!-- This is not printed because jsPDF doesn't deal with the value attribute -->
    <input type="text" value="Please print me, too!">
  </div>
</body>
2 of 16
94

This is the simple solution. This works for me. You can use the javascript print concept and simple save this as pdf.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $("#btnPrint").live("click", function () {
            var divContents = $("#dvContainer").html();
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(divContents);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        });
    </script>
</head>
<body>
    <form id="form1">
    <div id="dvContainer">
        This content needs to be printed.
    </div>
    <input type="button" value="Print Div Contents" id="btnPrint" />
    </form>
</body>
</html>
🌐
Phppot
phppot.com › javascript › html-to-pdf-in-javascript-using-jspdf
HTML to PDF in Javascript using jsPDF with Example Download - Phppot
This example is for simply converting HTML to PDF in JavaScript with few lines of code. It uses the jsPDF library to build a custom PDF generator tool on the client-side.
🌐
GitHub
gist.github.com › harunpehlivan › d26f42422b6b5ce37af76fb7fa0d3934
How To Convert HTML To PDF using JavaScript · GitHub
How To Convert HTML To PDF using JavaScript. GitHub Gist: instantly share code, notes, and snippets.
🌐
CodePen
codepen.io › tag › jspdf
Pens tagged 'jspdf' on CodePen
CodePen doesn't work very well without JavaScript · We're all for progressive enhancement, but CodePen is a bit unique in that it's all about writing and showing front end code, including JavaScript. It's required to use most of the features of CodePen · Need to know how to enable it?