IRONSOFTWAREHOME
Execute Custom JavaScript
from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()
 
# Example that change all to red style in JavaScript:
renderer.RenderingOptions.Javascript = "document.querySelectorAll('h1').forEach(function(el){el.style.color='red';})"

# Make sure to use the render options object that you created:
pdf = renderer.RenderHtmlAsPdf("<h1>Happy New Year!</h1>")
pdf.SaveAs("executed_js.pdf")
pip install ironpdf
Execute Custom JavaScript

Execute Custom JavaScript

IronPDF for Python can inject and execute custom JavaScript against a page before it is rendered to PDF, giving you programmatic control over the HTML content at render time.

Getting Started

Assign a JavaScript string to RenderingOptions.Javascript on a ChromePdfRenderer instance. IronPDF will execute that script after the page has loaded and before the PDF is captured.

Understanding the Code

  • RenderingOptions.Javascript: A string containing JavaScript code that IronPDF injects and runs after the page finishes loading. In the example, this script selects all <h1> elements and sets their color to red using querySelectorAll and a forEach loop.
  • RenderHtmlAsPdf(html): Renders the HTML with the injected JavaScript applied. The resulting PDF reflects the DOM state after the script has run.

Use Cases

Injecting custom JavaScript at render time is useful for:

  • Hiding UI elements not relevant to a print view (navigation bars, sidebars, modals).
  • Applying dynamic styles based on conditions evaluated at render time.
  • Populating data into a page template before capturing it as a PDF.
  • Triggering print-specific layout changes without modifying the original HTML source.
Learn to convert HTML to PDF with IronPDF for Python!

Ready to Get Started?

Version:2026.9just 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
Python Module Download for PDF
Install with pip

Version: 2026.9

  1. Download and install Python 3.7+.
  2. Install pip from pypi.org if it isn't installed already.
  3. Execute the above command in the terminal.
Python PDF Module
Download Module

Version: 2026.9

Manually install into your project

  1. Download the package
  2. Run this command from the terminal
    pip install ironpdf-2026.9-py37-none-win_amd64.whi

Licenses from $999