Skip to content

Embed field JavaScript (beta)⚓︎

Info

These beta features and their documentation are subject to change. Use with caution; they may be modified, rolled back, or fail in some PDF forms.

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.

This documentation uses this PDF as an example.

PDF form fields can execute JavaScript during interactions if supported by the viewer. PyPDFForm provides APIs to embed scripts into form fields.

For example, this snippet embeds a script that triggers an alert when the test field is hovered:

app.alert("Hello World!");
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_hovered_over_javascript = "./alert.js"

form.write("output.pdf")
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_hovered_over_javascript = open("./alert.js")

form.write("output.pdf")
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_hovered_over_javascript = open("./alert.js").read()

form.write("output.pdf")
Tip

Please refer to this link for JavaScript that can be executed in PDF forms.

Execute JavaScript on hover⚓︎

Set the on_hovered_over_javascript attribute to run code when a field is hovered over:

this.getField("test").value = "hovered over";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_hovered_over_javascript = "./script.js"

form.write("output.pdf")

Execute JavaScript on hover off⚓︎

Set the on_hovered_off_javascript attribute to run code when the mouse moves away from a field:

this.getField("test").value = "hovered off";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_hovered_off_javascript = "./script.js"

form.write("output.pdf")

Execute JavaScript on mouse pressed⚓︎

Set the on_mouse_pressed_javascript attribute to run code when a mouse button is pressed within a field:

this.getField("test").value = "mouse pressed";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_mouse_pressed_javascript = "./script.js"

form.write("output.pdf")

Execute JavaScript on mouse released⚓︎

Set the on_mouse_released_javascript attribute to run code when a mouse button is released within a field:

this.getField("test").value = "mouse released";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_mouse_released_javascript = "./script.js"

form.write("output.pdf")

Execute JavaScript on focus⚓︎

Set the on_focused_javascript attribute to run code when a field gains focus:

this.getField("test").value = "focused";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].on_focused_javascript = "./script.js"

form.write("output.pdf")

Execute JavaScript on blur⚓︎

Set the off_focused_javascript attribute to run code when a field loses focus:

this.getField("test").value = "not focused";
from PyPDFForm import PdfWrapper

form = PdfWrapper("sample_template.pdf")
form.widgets["test"].off_focused_javascript = "./script.js"

form.write("output.pdf")