`;\n\n // Create rendering options object\n const renderOptions = {\n enableJavaScript: true,\n };\n\n // Render HTML content to a PDF\n const pdf = await PdfDocument.fromHtml(htmlWithJavaScript, { renderOptions });\n\n // Save the PDF with the executed JavaScript\n await pdf.saveAs(\"javascript-in-html.pdf\");\n})();","about":{"@id":"https://ironpdf.com/nodejs/examples/javascript-html-to-pdf/#product"},"targetProduct":{"@id":"https://ironpdf.com/nodejs/examples/javascript-html-to-pdf/#product"},"keywords":"javascript html to pdf,IronPDF for Node.js,HTML to PDF conversion,enable JavaScript in IronPDF,HTML to PDF JavaScript example,render PDF from HTML with JavaScript","datePublished":"2018-08-29","dateModified":"2026-04-24"}]}
IRONSOFTWAREHOME
JavaScript in HTML To PDF
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    // Define the JavaScript code to change text color to red
    const htmlWithJavaScript = `<h1>This is HTML</h1>
    <script>
        document.write('<h1>This is JavaScript</h1>');
        window.ironpdf.notifyRender();
    </script>`;

    // Create rendering options object
    const renderOptions = {
        enableJavaScript: true,
    };

    // Render HTML content to a PDF
    const pdf = await PdfDocument.fromHtml(htmlWithJavaScript, { renderOptions });

    // Save the PDF with the executed JavaScript
    await pdf.saveAs("javascript-in-html.pdf");
})();
npm i @ironsoftware/ironpdf
JavaScript in HTML To PDF

JavaScript in HTML To PDF

Any JavaScript code within the HTML will be executed during the rendering process, whether it's from an HTML string, file, or URL to a PDF document.

To enable JavaScript execution, set the enableJavaScript property to true. In some cases, such as when loading charts and graphs with JavaScript, you may also need to set a waitFor property. This ensures that there is sufficient time for the JavaScript code to execute properly.

For more detailed instructions on enabling JavaScript in IronPDF, visit the JavaScript Code Examples page.

Example of Enabling JavaScript in IronPDF

Here is a concise example of how you can enable JavaScript execution in IronPDF:

  • EnableJavaScript: This ensures that JavaScript within your HTML content is executed, which is essential for rendering dynamic content such as charts and graphs.
  • RenderDelay: The RenderDelay option lets you specify a wait time (in milliseconds) to allow the JavaScript to fully execute before the page is rendered as a PDF. This is particularly useful when the JavaScript involves asynchronous operations or animations.

Additionally, check out the JavaScript Code Examples page for further examples and detailed information on handling different types of JavaScript within IronPDF.

Explore JavaScript to PDF Examples on GitHub Now!
View on GitHub

Ready to Get Started?

Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Node.js Module Download for PDF
Install with npm

Version: 2026.8

  1. Download and install Node.js 12+.
  2. Execute the above command in the terminal.

Licenses from $999