DEV Community

IronSoftware
IronSoftware

Posted on

Searchable PDFs From JavaScript: html2pdf.js and IronPDF

A booking confirmation page has a Download PDF button under it, and the customer clicks it to keep a copy of the order. When a site lets people download a web page as PDF in JavaScript, the first question is where the file is made, and the second is whether the order number inside it can be selected and searched later.

On the browser side, html2pdf.js, released under the MIT license, is built on html2canvas and jsPDF and converts a page or a single element into a PDF on the client. The sections below cover what its file contains, why it stays in the browser and how IronPDF for Node.js renders the same confirmation on a server.

What ends up inside the downloaded file?

A picture of the page. The whole integration is one script tag and one call inside a click handler.

<main id="order-confirmation">
  <h1>Order 48213 is booked</h1>
  <p>2 tickets, Harbor Lights Festival, Gate C</p>
</main>
<button id="download-pdf">Download PDF</button>

<script src="html2pdf.bundle.min.js"></script>
<script>
  // Capture the confirmation element in the tab on click, with no request to the server
  document.getElementById("download-pdf").onclick = function () {
    html2pdf(document.getElementById("order-confirmation"));
  };
</script>
Enter fullscreen mode Exit fullscreen mode

The library renders the content into an image and places that image into a PDF. A customer cannot select the order number to paste it into an email, a search inside the PDF for the event name returns nothing, and an accounting import or a document index that reads PDFs for their text sees a page with no words. The README adds that the approach causes large file sizes, so a year of confirmations attached to emails grows quickly.

Can html2pdf.js move to a Node.js server?

No. The README states that html2pdf.js runs only in a browser. It can be installed with npm and bundled into a front-end build, but the code still executes in the user's tab.

That matters once PDF generation is no longer triggered by a button. Teams move generation to a server for a familiar set of reasons, such as a nightly batch of statements, a PDF attached to a confirmation email or a copy kept for the record, and none of those jobs has a tab open when it runs. The browser carries one more limit that grows with the document. Canvases have a maximum height and width, and content that needs a larger canvas comes out as a blank PDF.

Where html2pdf.js fits

For a page the customer is already looking at, the library asks for almost no setup, and from the browser console with the library loaded, html2pdf(document.body) captures the entire page.

Page breaks are well supported. The library honors most CSS break properties in its default mode, offers avoid-all, css and legacy modes, and takes selectors for elements to break before, break after or keep whole. TypeScript types ship in the package, and version 0.14.0 fixed CVE-2026-22787, the only security advisory the project had published as of December 2, 2026, which concerned HTML passed as a text source. Anyone passing text as a source should be on 0.14.0.

Rendering the confirmation from Node.js with IronPDF

IronPDF renders the page from its address, as a PDF whose text stays text. IronPDF for Node.js is installed as @ironsoftware/ironpdf, renders HTML with an embedded Chromium engine and returns a document object.

import { PdfDocument } from "@ironsoftware/ironpdf";

// Open the confirmation page by its address on the server and render it as text and shapes
const confirmation = await PdfDocument.fromUrl("https://tickets.example.com/orders/48213/confirmation");

// Read the text back to show the order number can be found and copied
const text = await confirmation.extractText();
console.log(text.includes("48213"));

// Save the confirmation to attach to the email
await confirmation.saveAs("order-48213-confirmation.pdf");
Enter fullscreen mode Exit fullscreen mode

IronPDF's example on converting a URL to a PDF covers fromUrl, the Node.js PDF creation guide adds HTML strings and files as sources, and extractText reads the text back.

Often the server already holds the order data, and then there is no page to fetch. PdfDocument.fromHtml takes an HTML string or the path to an HTML file and returns the same kind of document, so the confirmation email can attach a PDF built straight from the booking record. Tickets for a group booking can be merged into one file so the customer downloads a single PDF for the whole party. Before it goes out, IronPDF can apply passwords and permissions to confirmations that carry personal details.

Sorting the PDF jobs by where they run

The deciding questions are whether a browser is open when a job runs and whether anyone needs the text afterward.

  • No browser present: Emailed confirmations, nightly statements and archive copies move first, because the browser library cannot serve them.
  • Text people reuse: Invoices and receipts headed for an accounting system follow, since a picture of an invoice means retyping it.
  • Long documents: Order histories and multi-page reports come next, before one of them exceeds the canvas limit.
  • The on-screen button: The ticket a customer saves and shows at the gate moves last, once the server route exists.

We suggest rendering one real confirmation through IronPDF for Node.js, dragging across the order number in the result and searching the file for the event name, as a check that the text survived. A trial key, set as IronPDF's license key guide explains, goes in before that first render.

html2pdf.js, html2canvas, jsPDF and Chromium are trademarks or works of their respective owners, we have no affiliation with any of them, and the facts above are based on the projects' repositories, README files and advisory, npm records and IronPDF for Node.js documentation.

Top comments (0)