Embed PDF JavaScript⚓︎
Warning
Do NOT trust user input; always sanitize it. Although PDF JavaScript runs in a sandbox, arbitrary execution is dangerous and can lead to remote code execution vulnerabilities.
These examples use sample_template.pdf.
PDFs can execute JavaScript during interactions if supported by the viewer. PyPDFForm provides Python APIs and CLI commands to embed scripts into both the PDF document and its form fields.
In CLI examples, field-level scripts are configured in data.json and applied with update field, unless the section says otherwise.
The examples below embed a script that displays an alert when the pointer hovers over the test field:
Tip
For supported Acrobat JavaScript APIs, see the Adobe JavaScript API reference.
Execute JavaScript on hover⚓︎
Set on_hovered_over_javascript to run code when the pointer hovers over a field:
Execute JavaScript when hover ends⚓︎
Set on_hovered_off_javascript to run code when the pointer leaves a field:
Execute JavaScript on mouse press⚓︎
Set on_mouse_pressed_javascript to run code when a mouse button is pressed inside a field:
Execute JavaScript on mouse release⚓︎
Set on_mouse_released_javascript to run code when a mouse button is released inside a field:
Execute JavaScript on focus⚓︎
Set on_focused_javascript to run code when a field gains focus:
Execute JavaScript on blur⚓︎
Set on_blurred_javascript to run code when a field loses focus:
Execute JavaScript on PDF open⚓︎
Use PdfWrapper.on_open_javascript to set or read the script that runs when the PDF opens: