npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@awryhant/pdfmath

v0.1.0

Published

PDF arithmetic from the command line and as an MCP server: merge, cut, slice (Python-style), rotate and compress PDFs, with a Python-like scripting language (.pm). Fully offline.

Readme

pdfmath (pdfm)

PDF arithmetic on the command line. You merge, cut, slice, rotate and compress PDFs with operators and Python slicing. A Python-like scripting language and an interactive >>> prompt are included. It is a single static binary that runs fully offline.

pdfm operators, rendered from real output

pdfm -e 'p1 - p2' -o merged.pdf a.pdf b.pdf     # a then b
pdfm -e 'p1[::-1] - 1' -o out.pdf a.pdf         # reversed, without its last page
pdfm -e 'p1 -- 2mb' -o small.pdf scan.pdf       # compress to <= 2 MiB

Install

npm install -g @awryhant/pdfmath
# or build from source (Rust 1.88+):
cargo install --locked --git https://github.com/arihant1602/pdfmath

npm ships prebuilt binaries for Linux, macOS and Windows on x64 and arm64. You don't need Node or Rust at runtime.

Use it from AI agents (MCP)

pdfm mcp is a built-in MCP server. Once it is registered, your agent edits PDFs through tool calls (pdf_info, pdf_eval, pdf_script) whenever a task needs it:

claude mcp add --scope user pdfmath -- pdfm mcp     # Claude Code
codex mcp add pdfmath -- pdfm mcp                   # Codex CLI

Claude Code editing PDFs through the pdfmath MCP server

Setup for Cursor, Claude Desktop, Gemini CLI and other clients is in docs/mcp.md.

Let your AI agent install it

Paste the prompt from docs/AGENT_PROMPT.md into Claude Code, Codex, Cursor or a similar agent. It installs pdfm, registers the MCP server, checks that it works, and learns the syntax. After that you can ask for things like "merge these, drop the cover, and get it under 5 MB".

Operators

Pages are 0-indexed, as in Python.

| Expression | Result | |---|---| | a - b | merge: a's pages, then b's | | a + b | merge: b's pages, then a's | | p - n | drop the last n pages | | p + n | drop the first n pages | | p * n | repeat p n times | | p -- 2mb | compress until the file is ≤ 2mb (units b, kb, mb, gb; 1kb = 1024 bytes) | | p[i], p[-1] | a single page | | p[a:b:c] | Python slicing: p[::-1] reverses, p[::2] keeps every other page, p[1:-1] trims both ends | | p[i] = q, p[a:b] = q, del p[::2] | Python list assignment and deletion | | len(p) | page count | | q in p | True if all of q's pages are in p |

How -- works:

  1. It first tries lossless steps: recompressing streams, removing duplicate objects, and packing objects into object streams.
  2. If that is not enough, it re-encodes the images as JPEG. It searches for the mildest quality and scale setting that fits the target.
  3. The size is measured on the real output, so the file it saves is never larger than the target.
  4. If the target cannot be reached, for example with a PDF that is all text, it fails with an error that names the smallest size it managed.

Write p - -2 with a space if you really mean minus negative two.

Command line

pdfm                          interactive >>> prompt (runs stdin as a script when piped)
pdfm script.pm [ARGS...]      run a script; ARGS are in the list `args`
pdfm -c CODE [ARGS...]        run code from a string
pdfm -e EXPR [-o OUT] FILES   evaluate one expression; FILES are bound to p1, p2, ...
                              (or NAME=path.pdf binds NAME); -o saves the resulting pdf
pdfm -i script.pm             run a script, then stay in the prompt
pdfm -                        read a script from stdin
pdfm --help-lang              language cheat-sheet

one-liners

pdfm -e 'p1 + p2' -o out.pdf a.pdf b.pdf             # b then a
pdfm -e 'p1[::-1]' -o reversed.pdf a.pdf
pdfm -e 'p1 + 1 - 1' -o trimmed.pdf a.pdf            # drop first and last page
pdfm -e 'cover - body[2:10]' -o out.pdf cover=c.pdf body=b.pdf
pdfm -e 'p1 -- 500kb' -o small.pdf scan.pdf
pdfm -e 'len(p1)' a.pdf                              # prints 12

Interactive prompt

the >>> prompt

>>> a = open("report.pdf")
>>> a
<pdf: 12 pages>
>>> for i in range(0, len(a), 4):
...     save(a[i:i+4], f"part{i // 4}.pdf")
...
>>> info(a[::-1] - 2)

A line that ends in :, or that leaves a bracket open, continues on a ... line. A blank line runs the block. History is kept in ~/.pdfm_history.

Scripts (.pm)

.pm files use Python syntax and semantics:

  • control flow: if/elif/else, while, for, break, continue
  • functions: def with default and keyword arguments, return, lambda
  • global, list comprehensions, tuple unpacking, f-strings with format specs, and % formatting
  • the types int, float, str, bool, list, tuple and None, with the common string and list methods
  • builtins: print, len, range, sorted, enumerate, zip, min, max, sum, map, filter, any, all, round, abs, isinstance, input, exit

Dicts, classes, import and exceptions (try) are not supported.

PDF builtins:

| Function | | |---|---| | open(path, password=None) | load a pdf | | save(p, path) / p.save(path) | write it; returns the number of bytes written | | rotate(p, 90) / p.rotate(90) | rotate every page clockwise (any multiple of 90) | | reverse(p) | reverse the page order | | blank(n=1, width=595, height=842) | n empty pages (A4, in points) | | info(p) | print the page count, source files and page sizes | | size(p) | the size in bytes it would have when saved | | pages(p) / list(p) | a list of single-page pdfs | | compress(p, size) | same as p -- size | | glob("dir/*.pdf"), exists(path) | find files (sorted) | | run("other.pm") | run another script in the same session |

# merge every pdf in a folder, then compress the result
files = glob(args[0] + "/*.pdf")
out = open(files[0])
for f in files[1:]:
    out = out - open(f)
save(out -- 5mb, "all.pdf")

PDF values are cheap: slicing and merging only shuffle page references, and the file is built only when you save. Pages keep their content, fonts, images, annotations and links. Document-level data (outlines/bookmarks, forms, the structure tree) is not carried over.

For AI agents and scripts

  • Prefer the MCP server (pdfm mcp, see docs/mcp.md). Otherwise you don't need a prompt: use pdfm -e, pdfm -c, pdfm file.pm or echo '...' | pdfm -.
  • The exit code is 0 on success and 1 on error. exit(n) sets it.
  • Errors go to stderr as origin:line:col: Kind: message, for example job.pm:3:9: NameError: name 'x' is not defined.
  • -e … -o out.pdf prints one line: wrote out.pdf (N pages, SIZE).
  • Use pdfm -e 'len(p1)' file.pdf to get a page count, and pdfm -e 'info(p1)' file.pdf for details.
  • Nothing touches the network.
  • Agent-facing notes are in AGENTS.md. The full language reference is in docs/language.md.

Building from source

cargo build --release     # target/release/pdfm
cargo test
cargo clippy --all-targets -- -D warnings

npm packaging: npm/pdfmath is a small Node launcher, published as @awryhant/pdfmath. For each platform there is an optional package (pdfmath-linux-x64, pdfmath-darwin-arm64, pdfmath-windows-x64, …) that holds the binary. .github/workflows/release.yml builds all of them and publishes them when a v* tag is pushed. It needs the NPM_TOKEN secret.

License

MIT. See LICENSE.