An explicit object model of PDF 1.7 and PDF 2.0, including ISO 32000-2:2020 EC3 and its companion specifications, for 64-bit PHP 8.3 or later.
This library represents the format's syntax and structures, down to its names, numbers, references, dictionary entries and operators. It does not aim to provide a layout API. A higher-level library can construct these objects while retaining control of their composition.
The project belongs to the PDFToolkit organization. The PDFToolkit/pdf repository is the core: it contains the objects and mechanisms described by the Adobe/ISO standard, including writing. Reading external PDFs, converting PNG/JPEG files and analyzing TrueType/OpenType fonts belong to three optional packages. Each module depends on the core; installing the core to generate a PDF installs none of these modules.
The online documentation brings together guides, a reference for every component and tutorials with their code and viewable PDFs. The PDFToolkit/pdftoolkit.github.io repository retains the text, assets and build tools. This documentation presents the published core and module versions selected in the source manifest. The documentation commit is identified separately from package versions.
The 0.x series is intended for developers who want to construct and explore this model. Before 1.0, a new minor version may change names and signatures; patch releases preserve their series' API. The implemented scope and limits appear in specification coverage.
Definitions are based on the Adobe/ISO documents PDF32000_2008.pdf and adobe_supplement_iso32000.pdf, retained in the core repository. The supplement guide explains Adobe PDF 1.7 level 3 extensions, their provenance and mechanisms. The Adobe reference guide also introduces the 2006 reference, its errata, the redaction addendum and their portfolio. Adobe references archived in the core retain their original names and bytes; the four archived references are excluded from Composer archives.
Core 0.8.0 extends PDF 2.0 writing with the companion specifications: R=6 AES-CBC, R=7 AES-GCM, modern digests/signatures, PDF MAC, XMP/declarations, metadata ownership, associated files, document parts and qualified structure roles. The eleven bundle originals retain their supplied names, bytes and source fingerprints in the core; they are excluded from Composer archives. The PDF Association supplies the sponsored standards. WTPDF offers a partial audit, not certification; complete PDF 2.0 or PDF/A, PDF/X and PDF/UA conformance is not inferred from a definition catalogue.
Installation
Core 0.12.4 requires 64-bit PHP 8.3 or later and the ctype, iconv and zlib extensions. AES and CMS backends are optional and require openssl. The pdftoolkit/pdf package is available on Packagist. From the consuming project:
composer require "pdftoolkit/pdf:^0.12.4"
API changes appear in the changelog and the
0.12 migration guide. Keep the consuming project's
composer.lock to pin installed versions.
Package name migration
Since 0.12.2, the Composer package is pdftoolkit/pdf. The PHP namespace remains
PDF\ and its API is unchanged. To migrate an application that directly
requires the former name:
composer remove roukmoute/pdf --no-update
composer require 'pdftoolkit/pdf:^0.12.2' --with-all-dependencies
Review and commit the resulting lock file. The new package replaces the old name
at self.version, so compatible older module constraints can resolve to one core
installation. It does not impersonate older exact versions: a constraint pinned
to 0.12.1, for example, must be updated. Previously published tags and lock
files retain their original package identity.
Optional modules
| Package | Responsibility |
|---|---|
pdftoolkit/pdf-reader |
Read objects and revisions from an external PDF, check pages and their inheritance, inspect their streams. |
pdftoolkit/pdf-images |
Inspect JPEG files and decode PNG files into core image objects. |
pdftoolkit/pdf-fonts |
Select a static TrueType/CFF1 face, prepare metrics and mappings, and apply explicit OpenType layout to PDF text. |
Reader 0.8.1, Images 0.2.3 and Fonts 0.2.1 require the canonical core package pdftoolkit/pdf:^0.12.2. This documentation selects core 0.12.4; older module releases retain their historical constraints. Reader accepts PDF 1.0–2.0 syntax, explicit password/public-key authentication, source MAC verification and bounded object imports. Its image adapter selects a compatible native engine first, then the optional PHP codecs. Images handles static PNG depths, Adam7 and JPEG colour metadata. Fonts supports static TrueType/CFF1 faces and explicit OpenType layout; variable CFF2 embedding remains excluded. These packages are available on Packagist: reader, images and fonts. For example, to import image files with core 0.12.4:
composer require "pdftoolkit/pdf:^0.12.4" "pdftoolkit/pdf-images:^0.2.3"
The module installs the core with its stable constraint. The package guide explains boundaries, installation and reader behavior. Keep the application's composer.lock to pin the versions and commits used.
The independent OpenType model, pdftoolkit/opentype, represents font tables, Unicode mappings, GSUB/GPOS layout, variable metrics and unhinted TrueType/CFF/CFF2 outlines. It depends on no PDF package. Its coverage details the implemented mechanisms and work remaining.
composer require "pdftoolkit/opentype:^0.4.1"
The independent pdftoolkit/pdf-codecs library decodes JPEG, JPEG 2000 and JBIG2 in PHP without depending on a PDF package. Reader detects it when installed. If no compatible native engine or PHP decoder is available, it reports the installation command; it never installs dependencies itself or hides malformed input and exhausted budgets. The codec guide documents selection, precision and supported profiles.
composer require "pdftoolkit/pdf-codecs:^0.1"
Development and checks
From a clone of the PDFToolkit/pdf repository:
export COMPOSER_ROOT_VERSION=0.12.x-dev
composer install
composer check
php examples/hello-world.php hello-world.pdf
php examples/object-stream.php object-stream.pdf
Production code depends only on PHP and these three extensions. PHPUnit 12, PHPStan, PHP-CS-Fixer and the modules used by examples and documentation are development dependencies locked in composer.lock. Their presence in this checkout adds no production dependency to applications using the core. Core CI runs tests and validates example PDFs on PHP 8.3, 8.4 and 8.5. Tests also fail on PHP deprecations and warnings. The changelog describes every published release.
To validate this checkout, including from its tag, set
COMPOSER_ROOT_VERSION=0.12.x-dev before composer install. This variable
identifies the current core as belonging to the 0.12 series accepted by the
modules. Development dependencies use released PDF modules, whose exact versions
and sources are locked in composer.lock. CI also defines this variable for
branches, tags and pull requests.
Construction object by object
$helvetica = new Type1Font(StandardFont::Helvetica);
$resources = new ResourceDictionary(
new Font(new DictionaryType(
new Entry(new NameType('F1'), new IndirectReference($helvetica)),
)),
);
$contents = new StreamObject(new ContentStreams(
new BeginTextObject(),
new SetTextFontAndSize(new NameType('F1'), new NumericType(18)),
new MoveTextPosition(new NumericType(72), new NumericType(720)),
new ShowText(new LiteralString('Super !')),
new EndTextObject(),
));
$page = new PageObject(
new Rectangle(new NumericType(0), new NumericType(0), new NumericType(612), new NumericType(792)),
$resources,
$contents,
);
$pages = (new PageTreeNode())->addKid($page);
$catalog = new DocumentCatalog($pages);
$file = new FileStructure(new FileHeader('1.7'), new FileBody($catalog));
$file->writeTo('hello-world.pdf');
The complete example, with imports and metadata, is in examples/hello-world.php.
Tutorials and recipes
The 37 tutorials accompany each script with an explanation, a preview and variants to try. Code comments connect the objects to PDF instructions and structures.
| Topic | What the example explains |
|---|---|
| First document | Font, resources, content, page, catalog and document information. |
| Pages and text | Resource inheritance, multiple pages, positions and leading. |
| Drawing | Colors, paths, curves and graphics-state transformations. |
| Navigation | Hierarchical bookmarks and page destinations. |
| Links | Internal clickable annotations and URI actions. |
| Images | Image XObject, inline image, RGB bytes and filters. |
| Shading | PDF type 2 function and axial shading. |
| Compressed objects | Content stream, object container and cross-references. |
| TrueType font and Unicode | Embedded program, CID, widths and ToUnicode mapping. |
| JPEG and PNG images | Colors, PNG decoding, preserved JPEG and transparency mask. |
| Incremental revisions | Explicit replacements, preserved historical bytes and xref chaining. |
The PDF 2.0 sequence adds UTF-8 and associated files, metadata, declarations and document parts, encrypted payload wrappers, qualified structure roles and WTPDF reports, modern signatures and integrity, and timestamps and DSS/VRI evidence. Geospatial measurements and structure destinations complete the path. The bundle guide connects them to their retained specifications and independent checks.
The table recipe assembles these concepts into a concrete presentation. The recipe collection also explains how to contribute new examples.
Identity and responsibilities
- PDF values are objects. Strings represent bytes;
TextStringexplicitly encodes Unicode text. - An
IndirectReferencetargets an instance. ASerializationContextassigns identifiers while writing a single file. There is no singleton or global counter. FileStructuredescribes the file;FileWriterwrites it. Each write creates a fresh context and derives offsets and lengths from actually emitted bytes.- Dictionaries use
Entryobjects.with()andwithout()construct copies and preserve specialized models' constraints. - Page trees and outlines use mutable graph builders with a single parent and explicit references. Their structure calculates links and counters; standard definition objects are immutable.
- Filters are applied in the reverse of their decoding order. Streams are always referenced as indirect objects.
See the architecture for construction choices and specification coverage for traceability and limits.
The component guide explains every object family. The API reference gives signatures and links to code and the standard. The site build instructions describe generation and local checks.
Coverage and limits
The ISO 32000-1 catalogue inventories the reference's 392 tables, 823 indexed sections and 73 operator tokens. Dictionary tables produce named classes for dictionaries, entries and immutable definitions. PDF names retain their exact case, which is significant in the format.
Executable implementations cover fundamental objects, file writing, cross-reference tables and streams, compressed objects, content operators, page/name/number trees, destinations and outlines, standard fonts and all four PDF function representations.
A definition's presence is not a declaration of full compliance. The catalogue validates types, required references, unconditional requirements and some value forms. Conditions, relationships between dictionaries and version rules without specialized validation remain in their definitions and the coverage document.
The core composes font objects, CMaps, images and their masks, forms, tagged structure and incremental revisions. It also writes filters, explicitly selected encryption, byte-range signatures, hybrid references and linearization. Optional modules provide TrueType/CFF1 analysis, explicit OpenType layout, JPEG/PNG conversion, external-PDF reading and bounded graph imports. Verified mechanisms and remaining rules appear in the coverage inventory. Font subsetting, automatic bidirectional/script itemization and general editing of arbitrary signed PDFs remain separate concerns. EncodedData explicitly declares already-encoded bytes. Unsupported security obligations are rejected.
The writer currently assembles the file in memory. The reader is supplied by pdftoolkit/pdf-reader. A rendering engine and a layout API are outside the core.
Security
The security policy describes private reporting and trust boundaries. The security review explains serialization protections and application responsibilities for paths, PDF actions and resource limits.
Generating definitions
Structured resources and generated classes are versioned in the core repository. From that repository, generation is reproducible from those resources; the PDF does not need to be read again at library runtime.
python tools/generate_specification.py
The tools/extract_spec.py extractor retains the source and PDF locations. Using it requires the Python dependencies listed in the specification documentation. Normative descriptions trace constraints still to implement; class generation does not replace algorithms.
Derive HTML
For semantic HTML from tagged PDF, install pdftoolkit/pdf-to-html:^0.8.4 and follow tutorial 35. This optional conversion remains outside the core.
Version 0.8.4 requires Reader ^0.8.1, the canonical core pdftoolkit/pdf:^0.12.2 and 64-bit PHP 8.3+ with DOM and mbstring. Its requirements ledger links rules to tests. Profiles distinguish semantic content, optional form actions and PDF scripts; none of those active operations is enabled by default. Graphics rendering and authentication remain capabilities supplied explicitly by the application. Keep returned assets and read diagnostics before publication.
Packagist: pdftoolkit/pdf-to-html.
Create PDF from HTML
For HTML/CSS conversion instead of assembling PDF objects manually, install the optional pdftoolkit/html-to-pdf module:
composer require "pdftoolkit/html-to-pdf:^0.4"
The converter guide describes explicitly supplied fonts and resources, limits and diagnostics. See the actual invoice PDFs and CSS progression by level. The core remains independent from these HTML/CSS parsers.
The historical W3C CSS1 sample shows the unchanged test5526c.htm source converted with version 0.3 bounded floats, with its PDF, preview and validation evidence. The border example remains on the same page. The registry has 52 partial entries and 10 unsupported entries: CSS1 is not complete; check the coverage before converting an existing document.
The CSS1 text example adds signed word/letter spacing and first-line indentation in version 0.4. Its three native PDF pages, inert HTML source, PHP generator and independent PDF/browser receipts show the implemented behavior, including page continuation.