A PDF document is made up of several sections:

  • A one-line header identifying the version of the PDF specification to which thefile conforms
  • A body containing the objects that make up the document contained in the file
  • A cross-reference table containing information about the indirect objects in the file
  • A trailer giving the location of the cross-reference table and of certain specialobjects within the body of the file

The following code is an actual Hello World PDF document from this tutorial. The full guide on Understanding the PDF File Format from Leon Atherton will provide a detailed and simple answer to all of your questions.

There is also a huge Portable Document Format reference book from Adobe, if you really want to understand how it works.

The official pdf specification has been an ISO norm since 2008, ISO 32000-1; Adobe provides a copy thereof on their web site with merely the official ISO headers replaced: PDF32000_2008.pdf. Meanwhile (2018) ISO has published an updated version, ISO 32000-2.

Create a .txt file, open it in your favourite editor, paste this code. Then, change the extension to .pdf. You will see a working PDF document.

%PDF-1.4
1 0 obj <</Type /Catalog /Pages 2 0 R>>
endobj
2 0 obj <</Type /Pages /Kids [3 0 R] /Count 1>>
endobj
3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents 6 0 R>>
endobj
4 0 obj<</Font <</F1 5 0 R>>>>
endobj
5 0 obj<</Type /Font /Subtype /Type1 /BaseFont /Helvetica>>
endobj
6 0 obj
<</Length 44>>
stream
BT /F1 24 Tf 175 720 Td (Hello World!)Tj ET
endstream
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000056 00000 n
0000000111 00000 n
0000000212 00000 n
0000000250 00000 n
0000000317 00000 n
trailer <</Size 7/Root 1 0 R>>
startxref
406
%%EOF
Answer from Daniele Molinari on Stack Overflow
Top answer
1 of 3
8

A PDF document is made up of several sections:

  • A one-line header identifying the version of the PDF specification to which thefile conforms
  • A body containing the objects that make up the document contained in the file
  • A cross-reference table containing information about the indirect objects in the file
  • A trailer giving the location of the cross-reference table and of certain specialobjects within the body of the file

The following code is an actual Hello World PDF document from this tutorial. The full guide on Understanding the PDF File Format from Leon Atherton will provide a detailed and simple answer to all of your questions.

There is also a huge Portable Document Format reference book from Adobe, if you really want to understand how it works.

The official pdf specification has been an ISO norm since 2008, ISO 32000-1; Adobe provides a copy thereof on their web site with merely the official ISO headers replaced: PDF32000_2008.pdf. Meanwhile (2018) ISO has published an updated version, ISO 32000-2.

Create a .txt file, open it in your favourite editor, paste this code. Then, change the extension to .pdf. You will see a working PDF document.

%PDF-1.4
1 0 obj <</Type /Catalog /Pages 2 0 R>>
endobj
2 0 obj <</Type /Pages /Kids [3 0 R] /Count 1>>
endobj
3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents 6 0 R>>
endobj
4 0 obj<</Font <</F1 5 0 R>>>>
endobj
5 0 obj<</Type /Font /Subtype /Type1 /BaseFont /Helvetica>>
endobj
6 0 obj
<</Length 44>>
stream
BT /F1 24 Tf 175 720 Td (Hello World!)Tj ET
endstream
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000056 00000 n
0000000111 00000 n
0000000212 00000 n
0000000250 00000 n
0000000317 00000 n
trailer <</Size 7/Root 1 0 R>>
startxref
406
%%EOF
2 of 3
0

If you don't need some advanced features, you can use this:

// Create a new window
const printWindow = window.open('', '', 'height=600,width=800');

// Get the content you want to export
const content = document.getElementById('content-to-export').outerHTML;

// Write the content to the new window
printWindow.document.write('<html><head><title>Export as PDF</title>');
printWindow.document.write('<style>body { font-family: Arial, sans-serif; }</style>');
printWindow.document.write('</head><body>');
printWindow.document.write(content);
printWindow.document.write('</body></html>');

// Close the document to finish writing
printWindow.document.close();

// Wait for the content to load and then print
printWindow.onload = function () {
    printWindow.print();
    printWindow.close();
};

Basically what it will do is:

  • Write the HTML content to a new window
  • Apply some basic styling using inline CSS
  • Trigger the print dialog of the browser and the user will be able to save the PDF file

I like this solution because everything is native. For example, if you try to export a big data, it will split it into multiple PDF pages and you don't have to manage it.

When you will export it as PDF, browsers default styling will be applied. If you are trying to export a table or other styled elements, you can customise your inline CSS like this:

printWindow.document.write(`<style>
    body { font-family: Arial, sans-serif; }
    table { width: 100%; border-collapse: collapse; margin: 20px 0; }
    th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
    th { background-color: #f2f2f2; }
</style>`);

Or instead, as it is a basic HTML, you can import your own CSS file with link tag.

๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ how do you convert html to pdf without a third-party package/library?
r/javascript on Reddit: How do you convert HTML To PDF without a third-party package/library?
May 17, 2018 - It's impossible (not really, but highly impractical) the most vanilla way to do it without extensions plugins, or third party services is to use PDF.js, a pure JavaScript PDF library
๐ŸŒ
Quora
quora.com โ€บ How-do-I-export-my-HTML-page-as-a-PDF-in-JavaScript-without-a-third-party-tool
How to export my HTML page as a PDF in JavaScript without a third party tool - Quora
Answer (1 of 2): You can do it using jsPDF Html page JavaScrip file To run this code you need to include jspdf.debug.js in the head section of your html code. In this code i call jQuery function when button is click and inside this function ...
๐ŸŒ
Medium
sam-ngu.medium.com โ€บ how-i-turned-html-elements-into-pdf-using-plain-javascript-with-css-8aa2dba6e098
HTML elements to PDF using Plain Javascript (without library)
July 24, 2020 - HTML elements to PDF using Plain Javascript (without library) I found myself needing to programmatically convert HTML pages to PDF quite frequently for my projects. I know there are a lot of โ€ฆ
๐ŸŒ
QA With Experts
qawithexperts.com โ€บ article โ€บ javascript โ€บ easy-way-to-convert-html-to-pdf-using-javascript โ€บ 335
Easy way to convert HTML to PDF using Javascript - QA With Experts
May 22, 2022 - In this article, I have provided sample code with output to convert HTML to PDF using Javascript, using jsPDF library or without using any library.
๐ŸŒ
EncodedNA
encodedna.com โ€บ javascript โ€บ convert-html-table-to-pdf-using-javascript-without-a-plugin.htm
Convert HTML Table to PDF using JavaScript without a Plug-in
Here Iโ€™ll show you how convert an HTML table, along with table style, to a PDF document without using a plug-in.
Top answer
1 of 3
2

If all you need is the CDN then simply add it after the </body>

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

    function generatePDF() {
    
    const element = document.getElementById("pageprint");
    document.getElementById("reportbox").style.display = "block";
    document.getElementById("reportbox").style.marginTop = "0px"; 
    document.getElementById("pageprint").style.border = "1px solid black";
    html2pdf().from(element).save('download.pdf'); 
    }
    
    function downloadCode(){
    var x = document.getElementById("reportbox");  
    generatePDF();
    setTimeout(function() { window.location=window.location;},3000);}
    
<div id="pageprint">
   <div id="reportbox">Hello World!!</div>
</div> 
<button type="button" onclick="downloadCode();">Download HTML</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>

However seems a very odd way to ask a user to download a pdf page since the option disappears after the download is attempted, so change of mind does not keep it user visible to try differently on fail.

So for example, I say open the download on current page, I see

but if I say open in PDF Viewer I see

It's much simpler to layout the printable HTML page as text not image, and suggest the user prints or saves exactly as their browser is configured and their desire, best result for all, especially as no libraries are needed. Nor will the page be cluttered by buttons.

2 of 3
0

There isn't an easy way to do this. The best thing you could do is to open an empty page, fill it with your html data and print it to pdf. Or look for some external libary like jsPDF.

example for print to pdf:

 var wnd = window.open('about:blank', '', '_blank');
 wnd.document.write("<p> Some HTML-Content </p> ");
 wnd.print();
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ [askjs] some alternatives to generate pdf from html in 2023?
r/javascript on Reddit: [AskJS] Some alternatives to generate PDF from HTML in 2023?
May 4, 2023 -

Hey everyone! I have an app that frequently needs to generate PDFs that look very similar to the original pages. Currently, we use html2pdf on the client-side, which works well for most things, but we've noticed that tables are a weak spot for this library.

I'm now exploring a server-side approach using Puppeteer, but I'm worried about the waiting time, particularly because many of the pages I need to print require authentication and may take a few seconds to load. This is causing the PDF generation process to take longer than we'd like.

Do you think that Puppeteer is still the best solution, and I just need to make some adjustments, or is there another way to tackle this problem?

PS: I already looked on some of the past posts on this topic, but all of them seem to be all 3+ years old, so some things might have changed since.

Thanks in advance for any suggestions or advice!

Find elsewhere
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ t โ€บ html-to-pdf-in-pure-js โ€บ 367269
HTML TO PDF in pure JS - The freeCodeCamp Forum
February 8, 2020 - In this blog, I will demonstrate how to generate a PDF file of your HTML page with CSS using JavaScript and Jquery. Convert HTML To PDF, generate PDF from html ยท You could probably, if you know JS well, just take what @JeremyLT linked, and translate the jQuery into pure JS.
๐ŸŒ
DocRaptor
docraptor.com โ€บ node-html-to-pdf
Compare Top Node.js HTML to PDF Libraries - Open-Source and Commercial
Creating PDFs from HTML can be surprisingly different than creating standard web pages. Our goal is to speed up your research and save you pain. We haven't tried all these libraries in a production environment, so please do further testing and evaluation on your documentโ€”but we believe our shared PDF knowledge provides useful insight. We'll review the most popular Node.js and JavaScript HTML to PDF libraries, as well as a few non-HTML-based and even non-JavaScript-based options.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ [askjs] is it possible to generate a customized pdf file from a html page using js?
r/javascript on Reddit: [AskJS] Is it possible to generate a customized PDF file from a HTML page using JS?
June 19, 2024 -

So, I'm trying to generate a PDF file from a Microsoft SharePoint list. The usual method of using CTRL+P doesn't work properly because the information is spread across multiple pages. My idea is to create a "1-page model file" where I have tables for all the user information. This way, I can fill in the blank spaces and then extract it to a PDF. Is that even possible?

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>
๐ŸŒ
IronPDF
ironpdf.com โ€บ ironpdf blog โ€บ using ironpdf for node.js โ€บ html to pdf nodejs without puppeteer
Convert HTML to PDF in Node.js Without Puppeteer (Tutorial)
This article will explore how to convert HTML content into a PDF file using the IronPDF JavaScript package, offering a practical guide for developers.
Published ย  June 23, 2025
๐ŸŒ
HTML2PDF
html2pdf.com
HTML to PDF โ€“ Convert HTML files to PDF
A PDF would show the client what the designer has in mind for the web page without requiring the client to go through the complication of opening a raw HTML file. Our tool here can convert an HTML file into a PDF. You can upload a simple HTML file or a ZIP file containing the HTML code along with its corresponding images and style sheets. The first thing you need to do is decide how youโ€™ll want your HTML rendered. There are four options on the page: Grayscale, Landscape, No Background, and No JavaScript.
๐ŸŒ
APITemplate.io
apitemplate.io โ€บ home โ€บ generate pdfs in javascript(browser) with 4 popular methods
Generate PDFs in JavaScript(Browser) with 4 Popular Methods - APITemplate.io
June 24, 2024 - This is valuable in applications where the layout and content need to be customized according to user preferences. There are different libraries and methods available for generating PDFs with JavaScript. In the following sections, we will discuss the html2pdf library, the jsPDF library, the pdfmake library, and the built-in window.print() method, along with examples of how to use each one.
๐ŸŒ
Reddit
reddit.com โ€บ r/node โ€บ is node the right choice for html to pdf conversion?
r/node on Reddit: Is node the right choice for HTML to PDF conversion?
January 13, 2023 -

We are implementing a service that should generate PDF invoices for our clients.

Initally we are thinking about doing it using Node with puppeteer, but the first version of the service is really slow (1 second to generate a simple PDF with a header, footer and a table with product details). So I'm worried because PDF generation seems like a very CPU intensive task so I don't know if the service will handle well the concurrent request it will receive (aprox. 50 per second).

Does anyone uses node and puppeteer for this in a prod enviroment?

๐ŸŒ
Medium
weihui-guo.medium.com โ€บ which-javascript-library-should-i-use-for-client-side-html-to-pdf-conversion-77f25e40f782
Which JavaScript Library Should I Use for Client-side HTML-to-PDF Conversion? | by Weihui Guo | Medium
September 10, 2020 - But the new release of jsPDF leveraged pdf.js to convert HTML page to PDF and allows user-defined file name for download. As you can tell, there is no one-size-fits-all solution. The purpose of this article is to brief the pros and cons of these libraries so you can choose the right one for ...
๐ŸŒ
PDFCrowd
pdfcrowd.com โ€บ html-to-pdf
Convert HTML to PDF Online - PDFCrowd
For quicker conversion, utilize our Save as PDF Browser Addon or bookmarklet. These tools allows you to convert web pages with just a click. To install the bookmarklet, drag the button below to your bookmark bar (video tutorial).
๐ŸŒ
DocRaptor
docraptor.com
HTML to PDF API - High-Quality PDF Conversion with PrinceXML
WeasyPrint is the only major HTML-to-PDF library not based on a browser. While it lacks JavaScript support, it does offer more PDF-focused functionality than Headless Chrome.
๐ŸŒ
html2pdf.js
ekoopmans.github.io โ€บ html2pdf.js
html2pdf.js | Client-side HTML-to-PDF rendering using pure JS.
The following command will generate a PDF of #element-to-print and prompt the user to save the result: var element = document.getElementById('element-to-print'); html2pdf(element); Every step of html2pdf.js is configurable, using its new Promise-based API. If html2pdf.js is called without arguments, it will return a Worker object: