Registry / serialization / pypdftk

pypdftk

JSON →
library0.5pypypi✓ verified 25d ago

PyPDFtk is a Python wrapper for the powerful command-line tool PDFtk (PDF Toolkit). It provides a Pythonic interface to manipulate PDF documents, enabling operations such as filling forms with data, concatenating multiple PDFs, splitting a single PDF into individual pages, extracting specific page ranges, replacing pages, generating XFDF data, stamping, and adding backgrounds. The library is currently at version 0.5, with its last release in April 2021, and primarily follows a maintenance release cadence.

pip install pypdftk
INSTALL
IMPORT
SIG · PYPDFTK
P
pypdftk
serializationpythonv0.5
Install
2.4s avg
Import
38ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.036s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pypdftk
import pypdftk

This quickstart demonstrates basic operations like getting the number of pages and concatenating PDFs using `pypdftk`. It highlights the necessity of the `pdftk` binary and provides comments on how to set `PDFTK_PATH`. A form-filling example is commented out as it requires a specific fillable PDF and proper `pdftk` configuration.

import pypdftk import os import tempfile # NOTE: You must have the 'pdftk' binary installed on your system. # You may also need to set the PDFTK_PATH environment variable if pdftk is not in your system's PATH. # For example: os.environ['PDFTK_PATH'] = '/usr/local/bin/pdftk' on macOS or '/usr/bin/pdftk' on Linux. # For Windows, it might be 'C:/Program Files (x86)/PDFtk/bin/pdftk.exe' # Create a dummy PDF file for demonstration (in a real scenario, this would be an existing PDF form) # This example can't create a fillable PDF, so it will demonstrate other functions. # We'll use get_num_pages and concat for a runnable example. # Create a dummy PDF file (simplified for quickstart, typically this would be a pre-existing PDF) # In a real scenario, you'd use an actual PDF path. dummy_pdf_content = b'%PDF-1.4\n1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj 2 0 obj<</Type/Pages/Count 1/Kids[3 0 R]>>endobj 3 0 obj<</Type/Page/Parent 2 0 R/MediaBox[0 0 612 792]/Contents 4 0 R>>endobj 4 0 obj<</Length 16>>stream\nBT /F1 12 Tf 100 700 Td (Hello PyPDFtk!) Tj ET\nendstream\nendobj\nxref\n0 5\n0000000000 65535 f\n0000000009 00000 n\n0000000055 00000 n\n0000000107 00000 n\n0000000194 00000 n\ntrailer<</Size 5/Root 1 0 R>>startxref\n240\n%%EOF' with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f1: f1.write(dummy_pdf_content) pdf_path1 = f1.name with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as f2: f2.write(dummy_pdf_content) pdf_path2 = f2.name try: # Get number of pages num_pages = pypdftk.get_num_pages(pdf_path1) print(f"Number of pages in {pdf_path1}: {num_pages}") # Concatenate two dummy PDFs output_concat_pdf = pypdftk.concat([pdf_path1, pdf_path2]) print(f"Concatenated PDFs to: {output_concat_pdf}") # Example of filling a form (requires a real fillable PDF and pdftk to be correctly configured) # To run this, replace 'path/to/model.pdf' with a path to an actual fillable PDF # and ensure PDFTK_PATH is correctly set. # datas = {'firstname': 'John', 'lastname': 'Doe'} # try: # filled_pdf = pypdftk.fill_form('path/to/model.pdf', datas) # print(f"Filled form saved to: {filled_pdf}") # except Exception as e: # print(f"Could not fill form (requires a real fillable PDF and pdftk setup): {e}") finally: # Clean up temporary files os.remove(pdf_path1) os.remove(pdf_path2) if 'output_concat_pdf' in locals() and os.path.exists(output_concat_pdf): os.remove(output_concat_pdf) # if 'filled_pdf' in locals() and os.path.exists(filled_pdf): # os.remove(filled_pdf)
Debug
Known issues
breakingThe 'pdftk' binary is a mandatory external dependency and must be installed separately on your operating system. PyPDFtk is merely a Python wrapper and will not function without 'pdftk' being accessible in the system's PATH or explicitly configured via `PDFTK_PATH`.
fix
Install 'pdftk' (e.g., `sudo apt-get install pdftk` on Debian/Ubuntu, or download from pdflabs.com for other OSes). Ensure it's in your system's PATH, or set `os.environ['PDFTK_PATH'] = '/path/to/pdftk'` in your Python code.
affects: All versions
gotchaPyPDFtk might not automatically find the 'pdftk' executable on all systems, especially Windows or when 'pdftk' is installed in a non-standard location. This often leads to `FileNotFoundError` or `subprocess.CalledProcessError`.
fix
Explicitly set the `PDFTK_PATH` environment variable in your script or shell to point to the `pdftk` executable, e.g., `os.environ['PDFTK_PATH'] = '/usr/local/bin/pdftk'`.
affects: All versions
gotchaThe `pypdftk.split()` function, if provided with an `out_dir` parameter, warns that it 'may destroy your files and return incorrect results' if the specified output directory is not empty.
fix
Always ensure the `out_dir` argument passed to `pypdftk.split()` is an empty directory or let the function use a temporary directory by omitting `out_dir`.
affects: All versions
gotchaPyPDFtk currently does not support handling or inserting image fields into PDF forms. Its form-filling capabilities are limited to text, checkboxes, and radio buttons.
fix
For operations involving image fields, consider alternative PDF manipulation libraries or tools that explicitly support image embedding, or pre-process PDFs with images before using PyPDFtk for form filling.
affects: All versions
Errors
Common errors & fixes
FileNotFoundError: [Errno 2] No such file or directory: 'pdftk'
The underlying command-line tool 'pdftk' is not installed on the system, or its executable path is not included in the system's PATH environment variable, preventing pypdftk from finding it.
fix
Install 'pdftk' on your operating system (e.g., `sudo apt-get install pdftk` on Debian/Ubuntu, `brew install pdftk-server` on macOS) and ensure it's in your system's PATH, or explicitly configure pypdftk with `pypdftk.configure(pdftk_path='/path/to/pdftk')`.
ModuleNotFoundError: No module named 'pypdftk'
The `pypdftk` Python library has not been installed in the current Python environment or is not accessible to the interpreter running the script.
fix
Install the library using pip: `pip install pypdftk`.
FileNotFoundError: [Errno 2] No such file or directory: '/path/to/your_input.pdf'
The input PDF file, data file (e.g., XFDF for form filling), or output directory specified in a `pypdftk` function call does not exist at the given path or is inaccessible.
fix
Verify that the file path is correct, the file exists, and your Python script has read/write permissions for the specified files and directories; use absolute paths for robustness.
AttributeError: module 'pypdftk' has no attribute 'some_nonexistent_function'
You are attempting to call a function or access an attribute that does not exist within the `pypdftk` module, often due to a typo or misunderstanding of the library's API.
fix
Consult the `pypdftk` documentation for the correct function names and their usage, e.g., `pypdftk.fill_form`, `pypdftk.concat`, `pypdftk.split`.
Upgrade
Version history
0.5latest on PyPI · released Apr 11, 2021
Audit
Dependencies
pdftkrequiredPyPDFtk is a wrapper for the external 'pdftk' binary, which must be installed separately on your system.
Agent activity
11 hits · last 30 days
node
10
Resources
pypdftk — pip install pypdftk · libregistry