IRONSOFTWAREHOME
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.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