Execute Custom JavaScript
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    // Define the JavaScript code to change text color to red
    const javascriptCode = "document.querySelectorAll('h1').forEach(function(el){el.style.color='red';})";

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

    // HTML content to be rendered
    const htmlContent = "<h1>Happy New Year!</h1>";

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

    // Save the PDF with the executed JavaScript
    await pdf.saveAs("executed_js.pdf");
})();
npm i @ironsoftware/ironpdf
Execute Custom JavaScript

Execute Custom JavaScript

Effortlessly modify HTML content appearance with JavaScript code using IronPDF. IronPDF is a part of Iron Software's product libraries, known for its robust capabilities in converting HTML to PDF while supporting JavaScript, CSS, and more.

To achieve this, start by defining the JavaScript code. In our example, we'll change the text color of all H1 tags to red.

Next, create a rendering options object and set the enableJavaScript property to true. Assign the previously defined JavaScript code to the javascript property within this object.

Utilize the PdfDocument.FromHtml method to render the HTML content into a PDF document. Pass the renderOptions object as the second parameter.

Finally, save the resulting PDF, which incorporates the effects of the executed JavaScript code (changing text color to red), as 'executed_js.pdf'.

To learn more about the capabilities of IronPDF or other Iron Software libraries such as IronBarcode, IronOCR, and more, visit the Iron Software official website.

Explore the Execute Custom JavaScript Code Example Now!
View on GitHub

Ready to Get Started?

Version:2026.11just released