Run JavaScript on a PDF from the document context menu or Command Palette. Use an included script or edit your own with the same API as sumatrapdf-tool run.
Available in pre-release 3.7 and SumatraPDF ng desktop builds.
Choose a script, check its arguments, then press Run Script.
- Arguments: each row becomes an entry in
scriptArgs, starting atscriptArgs[0]. Empty fields show hints. - Input and output: examples fill paths and defaults from their argument comments. Output PDFs use the first unused name such as
foo.1.pdforfoo.2.pdfbeside the input. - Example Scripts: show or hide the script list beside the editor. Clicking a name loads its script and rebuilds its argument rows; Reset puts the picked script back the way it came, without your edits.
- Results: stdout and stderr appear below the script after it finishes. A failed script also reports its exit code.
Run an included script #
- Open a PDF, press
Ctrl + K, and choose Run PDF Script… (CmdRunPDFScript). You can also choose it from the document context menu. - Click Example Scripts to expand the dialog, then click a script name in the list.
- Check the arguments shown for that script. Fill any required empty fields. Use Browse… to choose a file, Remove to delete a row, or Add argument to append one.
- Press Run Script (or
Ctrl + Enter). Open the output file when the script finishes.

Examples with additional inputs, such as an image or another PDF, show those fields automatically.
Ask AI to write a script #
Describe what you want the script to do:
Write your own script #
The runner supplies MuPDF classes such as
Document, PDFDocument, Image, and Buffer. Use print() to write output. See the JavaScript API reference.var doc = Document.openDocument(scriptArgs[0]);
print("Pages:", doc.countPages());
doc.save(scriptArgs[1], "compress");
You can also use
"<INPUT>" and "<OUTPUT>" inside the script. They expand to the first two argument values, escaped for a JavaScript string:var doc = Document.openDocument("<INPUT>");
doc.save("<OUTPUT>", "compress");
Define example arguments #
Each
// Arg: comment defines one argument, in order: // Arg: initial value : description.
Keep spaces around :. Initial values are plain text; omit JavaScript quotes. Leave the value empty when the user must supply it.// Arg: <INPUT> : Input PDF
// Arg: <OUTPUT> : Output PDF
// Arg: all : Pages to process
// Arg: : Optional search text
The dialog replaces these tokens when you select an example, using the PDF that opened the dialog:
| Token | Initial value |
|---|---|
<INPUT> |
Input PDF path |
<OUTPUT> |
Unused output PDF path, such as foo.1.pdf |
<DIR> |
Input file’s directory |
<BASE> |
Input filename without its extension, such as foo |
Combine tokens with text, for example
<DIR>/<BASE>.txt for a text export.Tips #
- Changes saved in the viewer are available to the script; unsaved annotations are not, because scripts read the file on disk.
- Choose an output extension that matches the example: Draw graphics with Device writes PNG; Convert PDF to images appends page numbers and
.pngto the output path. - The script and arguments remain editable after a run. Selecting another example replaces both.
- The command requires disk access and is unavailable in restricted mode without it or in WebAssembly.