Skip to main content

PDFSyntax

A Python library to inspect and transform the internal structure of PDF files

Introduction

The project is focused on chapter 7 ("Syntax") of the Portable Document Format (PDF) Specification. It implements all the detailed document structure management down to the byte level for inspection and transformation use cases (access to metadata, rotation,...).

  • Internal functions are being exposed as an API toolkit for PDF read/write operations,
  • Some specific functions are additionally exposed as a command line interface for use in a terminal or a browser.

PDFSyntax is lightweight (no dependencies) and written from scratch in pure Python, with a focus on simplicity and immutability.

It favors non-destructive edits allowed by the PDF Specification: by default incremental updates are added at the end of the original file (you may rewind or squash all revisions into a single one).

Project status

WORK IN PROGRESS! This is BETA quality software. The API may change anytime. Next on TO-DO list:

  • Cut & append pages
  • Lossless compression
  • More filters
  • Improve text extraction
  • Augment text extraction with layout detection

Installation

You can install from PyPI:

pip install pdfsyntax

CLI overview

Please refer to the CLI README for details.

The general form of the CLI usage is:

pdfsyntax COMMAND FILE

Or this longer form if you installed from source:

python3 -m pdfsyntax COMMAND FILE

You can get quick insights on a PDF file with these commands:

  • overview outputs text data about the structure and the metadata.
  • disasm outputs a dump of the file structure on the terminal.
  • text outputs extracted text spatially, as if it was a kind of scan.
  • fonts outputs list of fonts used.
  • browse outputs static html data that lets you browse the internal structure of the PDF file: the PDF source is pretty-printed and augmented with hyperlinks.

API overview

Please refer to the API README for details.

PDFSyntax is mostly made of simple functions. Example:

>>> from pdfsyntax import readfile, metadata
>>> doc = readfile("samples/simple_text_string.pdf")
>>> metadata(doc) #returns a Python dict whose keys are 'Title', 'Author', etc...

The Doc object is probably the only dedicated class you will need to handle. It is a black box that stores all the internal states of a document:

  • content that is cached/memoized from an original file,
  • modifications that add/modifiy/delete content and that are tracked as incremental updates.
>>> doc
<PDF Doc in revision 1 with 0 modified object(s)>

This object exposes as a method the same metadata function, therefore you can get the same result with:

>>> doc.metadata() #returns a Python dict whose keys are 'Title', 'Author', etc...

Low-level functions like get_object or update_object allow you to directly access and manipulate the inner objects of the document structure. You may also use higher-level functions like rotate:

>>> from pdfsyntax import rotate, writefile
>>> doc180 = rotate(doc, 180) #rotate pages by 180°

The original object is unchanged and a new object is created with an incremental update (revision 2) that encloses the ongoing orientation modification:

>>> doc180
<PDF Doc in revision 1 with 1 modified object(s)>

You then can write the modified PDF to disk. Note that the resulting file contains a new section appended to the original content. You may cut this section to revert the change.

>>> writefile(doc180, "rotated_doc.pdf")

Open-Source, not Open-Contribution yet

PDFSyntax is MIT licensed but is currently closed to contributions.

Personal note: this is a pet projet of mine and my time is limited. First I need to focus on my roadmap (new features and refactoring) and then I will happily accept contributions when everything is a little more stabilised.

Release files for pdfsyntax 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdfsyntax 0.1.6
File Size Uploaded
pdfsyntax-0.1.6.tar.gz 45.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pdfsyntax 0.1.6
File Interpreter ABI Platform
pdfsyntax-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 90.1 kB

Release files / pdfsyntax-0.1.6.tar.gz

Download URL pdfsyntax-0.1.6.tar.gz
Size 45.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b4d8823a83e77c4617bbcc96caabd1157e1ab8ae73c4af085c4397fb9a29f320
BLAKE2b-256 checksum
How to use checksums
675eab7a10970258b28f569de8981b4ca511936ece713b6b3f495d51e34c76ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2025.

Transparency log

Release files / pdfsyntax-0.1.6-py3-none-any.whl

Download URL pdfsyntax-0.1.6-py3-none-any.whl
Size 45.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f99cccd0cf3ace8400cd6046743fc0db83ebcd286bc59cd69c81d95d8944de2
BLAKE2b-256 checksum
How to use checksums
c5e46291d8c552bb49c5473f346651b596478a20cc5126f748172cfcf1bdae89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 1, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page