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.11just released