A-PDF
A-PDF is a fast, command-line tool designed to replace Unicode and Hex text directly within PDF content streams.
Features
- Direct Stream Replacement: Modifies the underlying PDF streams directly for fast and precise text replacement.
- Unicode & Hex Support: Text is automatically encoded to the document's fonts.
- Layout Preservation: Only the matched text is rewritten, so
TJspacing, justified gaps and column gaps survive (width compensation can be disabled). - Reflow & Paragraph Wrap: When a replacement is longer or shorter, the following words on the line move with it. If the line then runs past the right margin, the overflowing words wrap onto the next lines of the same paragraph (works on PDFs that position each word or glyph separately, e.g. Chrome / Skia / Google Docs). Disable with
-nopa. - Regex & Rules Files: Regex with groups (
\1), ignore-case, whole-word, and rules from.json/.csv/.tsv. - RTL Support:
--rtlreverses each OLD string to match the left-to-right glyph order that RTL producers (e.g. Google Docs / Skia) write into content streams. - Dry Run, Reports & Verify: Preview every match, get a JSON report, and re-check the output.
- Safe Output: Atomic write with integrity check before saving. Output if not specified with
-ois always saved asInput-Edited.pdf, keeping the original intact. - Wider Coverage: Form XObjects, annotations/form fields, split text, multi-part
/Contents. - Batch, Encrypted & Damaged PDFs: Whole folders in parallel,
--password, automatic repair. - Strict Mode / Fallback Font / Page Selection: As before.
Installation
uv add apdf # or As a tool: uv tool install apdf
uv run apdf --help
Or with pip:
pip install apdf # core
pip install "apdf[crypto]" # + AES-encrypted PDF support (--password)
Usage As Library
from apdf import Replacement, replace_pdf_text_detailed
rep = replace_pdf_text_detailed(
"Test1.pdf",
"Test1-Edited.pdf",
[
Replacement("Old Text", "New Text"),
Replacement(r"INV-(\d+)", r"BILL-\1", regex=True),
Replacement("draft", "final", ignore_case=True, whole_word=True),
], # a plain {"Old": "New"} dict still works
# Optional parameters:
# pages={2, 3, 4}, # one-based; int, set or range; None = all pages
# dry_run=True, # analyse only, write nothing (output_pdf may be None)
# verify=True, # re-read the output and confirm the replacements
# password="secret", # encrypted input
# strict=True,
# wrap=True, # fit long text within detected rectangular cells
# fallback_font="FONT.ttf", font_fallback=True,
)
print("Replacements:", rep.replacements)
print("Missing:", rep.missing)
print("Encoding failures:", rep.encoding_failures)
print("Warnings:", rep.warnings)
for m in rep.matches: # every occurrence with context
print(m.page, m.before, "->", m.after)
# rep.to_dict() is JSON-serialisable
The library is silent; attach a handler to the apdf logger for progress.
Usage As CLI Tool
apdf [options] input # output saved as input-Edited.pdf
apdf --batch FILES/DIRS/GLOBS --out-dir DIR [options]
Examples
# Basic / multiple replacements (saves as input-Edited.pdf)
apdf input.pdf -r "OldText=NewText" -r "2023=2024"
# Preview first (nothing is written)
apdf input.pdf -n -r "foo=bar"
# Regex with a group, case-insensitive, verified, with a JSON report
apdf input.pdf -x -i -r 'INV-(\d+)=BILL-\1' --verify --report report.json
# Arabic / RTL PDF (Google Docs, Skia) — glyphs are stored LTR in the stream
apdf input.pdf -r "جين=جولي" --rtl -i -ff ReadexPro.ttf
# Rules from a file
apdf input.pdf --rules rules.json
# Wrap a long replacement inside rectangular table cells
apdf input.pdf -r "Emil Tester=This is very long text to see what will happen" --wrap
# A whole folder, 4 processes, originals untouched (saves as *-Edited.pdf)
apdf --batch ./pdfs --out-dir ./edited --rules rules.json -j 4
Rules file
rules.json (per-rule options; backslashes doubled in JSON):
[
{"old": "Abodx\\d", "new": "Tester", "regex": true, "ignore_case": true},
{"old": "INV-(\\d+)", "new": "BILL-\\1", "regex": true},
{"old": "12.50", "new": "99.00"}
]
A plain {"old": "new"} object also works. rules.csv / .tsv:
old,new,regex
Abodx9,Tester,
INV-(\d+),BILL-\1,regex
Arguments
input: Source PDF. (the default output file isinput-Edited.pdf).-o FILE: Output file for edited PDF.-r OLD=NEW: Replacement pair, repeatable. Use\=for a literal=in OLD.--rules FILE: Load rules from.json,.csvor.tsv, repeatable.-x/-i/-w: Regex / ignore case / whole word, applied to all rules.--rtl: Reverse both OLD and NEW strings before matching/replacing — for RTL PDFs (Arabic, Hebrew) where a producer like Google Docs writes glyphs left-to-right in the content stream.-p 2,-p 2,3,4,-p 1-5: Pages to edit (default: all).-n,--dry-run: Analyse and list every match, write nothing.--verify: Re-read the output and check the replacements are present.--report FILE,--json: JSON report to a file / on stdout.-vlists matches,-qis quiet.--password PW: Encrypted PDF (output is re-encrypted with the same password).--no-annotations: Skip annotation / form-field appearance streams.--batch,--out-dir DIR,--suffix TEXT,-j N: Batch mode — inputs are never overwritten (default suffix is-Edited).--extract: Print the text apdf sees on the selected pages.-nopa: No position adjustment. Keeps every original position: noTJwidth compensation, and the following text is not shifted or wrapped (a longer replacement may overlap the next word).--wrap: Wrap overflowing replacements inside detected rectangular cells and reduce the font size to fit the row.-s: Strict. Fail if text is missing, cannot be encoded, or verification fails.-ff FONT.ttf/-no-ff: Fallback font to embed for missing glyphs / refuse missing glyphs.
All rules are matched against the original text at once, so A=B plus B=C never turns A into C. Overlaps resolve leftmost, then longest.
Limitations
- Edits with different font using a fallback font might not look the same as the surrounding text.
- Text stored as images or outlines can't be found; Type3 and variable-width CJK fonts are untested.
- Paragraph wrap moves whole words or text runs; it cannot split a run that holds a full line in one
Tj/TJ(common in Word exports). If the paragraph's last line has no free space below it, the line stays long and a warning is reported. - Use
--wrapto wrap long replacements in cells with explicit rectangular bounds. It measures each line and shrinks the font when needed to fit the cell height. PDFs without detectable rectangular bounds keep the original layout behavior.
Metadata
Release files for apdf 2.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| apdf-2.4.tar.gz | 224.6 kB | Without provenance | Details for apdf-2.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| apdf-2.4-py3-none-any.whl | Python 3 | none | any | Details for apdf-2.4-py3-none-any.whl |
Total release size: 476.2 kB
Release files / apdf-2.4.tar.gz
| Download URL | apdf-2.4.tar.gz |
|---|---|
| Size | 224.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0fa6c0de2a31c31d3c9b3083326607134ac458fb74b4f7a9e2463f6082e937d0
|
|
BLAKE2b-256 checksum How to use checksums |
1b2cb373d7f04ea5964cc073e777c3e8336ec5f12cb9de050aa6ce25180ccf22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / apdf-2.4-py3-none-any.whl
| Download URL | apdf-2.4-py3-none-any.whl |
|---|---|
| Size | 251.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86eb4f656043116bf4709e7c88705527e7dfb4da66723fd128ee27499003a6b8
|
|
BLAKE2b-256 checksum How to use checksums |
06b5bccc6954efe7d54903418380da3a9c40c226dee3f30fc1462cd1441d5efc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for apdf 2.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.