dragonofmercy / phppdf
Modern PHP library for PDF generation.
Requires
- php: ^8.4
- ext-mbstring: *
- ext-openssl: *
- ext-zlib: *
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.0
- symfony/process: ^7.0
Suggests
- ext-gd: Decode WebP images (fallback) and JPEGs inside an SVG <filter> region
- ext-imagick: Decode WebP images (preferred backend)
Provides
None
Conflicts
None
Replaces
None
README
phppdf
Modern PHP 8.4 library for PDF generation. Pure PHP, no external runtime dependencies beyond the standard mbstring, openssl, and zlib extensions.
Status: stable (1.0). The public API follows semantic versioning; breaking changes are reserved for the next major version. See CHANGELOG.md.
What works today
A quick tour - each feature has a full guide in the wiki.
Create
- ๐ Documents - 19 standard sizes (ISO A0-A7, B4 / B5, C-series envelopes, Letter / Legal / Tabloid / Executive / Half-Letter) plus custom sizes, portrait or landscape, multi-page, metadata, AES-256 password protection, viewer hints.
- ๐ Coordinates - millimetres by default (origin top-left), or PDF points via
Unit::PT. - โ๏ธ Graphics - lines, rectangles, circles, paths, fill / stroke, dashes, caps / joins, transforms.
- ๐ค Text - the 12 standard fonts, multi-line, configurable leading, full Western Latin, exact width measurement.
- ๐ก Custom fonts - register TrueType / OpenType families with full Unicode (Latin / Greek / Cyrillic / CJK), selectable text, automatic glyph subsetting.
- ๐ฆ Cells - text boxes with borders, fill, padding, alignment (left / center / right / justify), word-wrap, three width-fit modes.
- ๐ผ๏ธ Images - JPEG, PNG (all color types, transparency), and WebP (lossy / lossless / alpha, decoded via ext-imagick or ext-gd), embedded once and placed anywhere.
- ๐จ SVG - fully vector: shapes, paths, gradients, patterns, clipping, masks, CSS styling, selectable
<text>/<textPath>, pure-PHP<filter>. - ๐ข Barcodes & QR - 1D (EAN-13 / 8, Code 128 / 39 / 93, UPC-A, ITF) and 2D (QR, Aztec, DataMatrix, PDF417), pure-PHP and vector.
- ๐ Markdown - a CommonMark core subset, flowing with automatic page breaks or inside an auto-sized cell, styleable via
MarkdownStyle. - ๐ Tables - data grids via
Page::table(): fixed orfillcolumns, repeated headers, zebra striping, borders, per-cell styling, column spanning, grouped headers. - ๐งฑ Multi-column layout - flow
cell()andmarkdown()across equal-width columns with$page->columns(...). - ๐ Bookmarks & hyperlinks - a nested table-of-contents sidebar and clickable URL / page areas.
- ๐งพ Interactive forms - text fields, checkboxes, radios, dropdowns, listboxes, buttons, with per-field styling, JavaScript actions, and field linking.
- โ๏ธ Digital signatures - real PKCS#7 / CMS via
Document::sign(), RFC 3161 timestamps, multiple signers, PAdES B-B / B-T, and LTV building blocks.
Right-to-left & accessibility
- ๐ Right-to-left text - Unicode bidi reordering (UAX #9) plus Arabic cursive shaping (contextual forms + lam-alef ligatures) on cells, tables, and Markdown; per-document, per-cell, or per-block direction.
- ๐๏ธ PDF/A archival - PDF/A-1 (PDF 1.4-based, levels 1b / 1a, transparency rejected), PDF/A-2, PDF/A-3 (levels b / u / a), and PDF 2.0-based PDF/A-4 (+ A-4f) with one
enablePdfA()call, veraPDF-validated; A-3 / A-4f embed Factur-X / ZUGFeRD e-invoices. - โฟ Tagged PDF & PDF/UA-1 - opt-in tagging via
enableTagging();enablePdfUA()produces veraPDF-validated PDF/UA-1 with artifacts, figure alt text, table header scopes, and tagged hyperlinks.
Read & modify existing PDFs
- ๐ Reading -
PdfReaderparses PDFs including encrypted ones (RC4 40/128-bit, AES-128, AES-256); pass an optional password or omit it for permissions-only encryption. Classic xref tables, cross-reference streams, incremental revisions, object streams, and common filters. - ๐งฉ Template import (FPDI-style) -
Document::importPdf()+Page::template()stamp any source page as a background or overlay: letterheads, watermarks, scanned-form fills. Encrypted source PDFs are supported (optional password). - ๐ง Modifying -
PdfEditor::open()saves changes as appended revisions, leaving the original bytes (and any signatures) intact: edit metadata, add pages, delete and reorder pages (bookmarks and links pointing at a removed page are cleaned up automatically), sign, timestamp, and enable LTV. Editing encrypted PDFs is supported - pass an optional password; signing an encrypted PDF is not yet supported. - ๐๏ธ Filling AcroForm fields - inspect and
setField()text / checkbox / radio / combobox / listbox values; each filled field gets a generated appearance. Fields that use a font embedded in the document (not only the standard 14) are supported too.flattenFields()freezes filled forms into static page content (all fields or a named subset; signature and button fields are kept). Works on encrypted PDFs.
Installation
composer require dragonofmercy/phppdf
Quick start
use DragonOfMercy\PhpPdf\Document; $pdf = new Document(); $pdf->addPage(); $pdf->save('out.pdf');
$pdf->output() returns the PDF bytes as a string instead of writing to disk.
Documentation
Full usage documentation lives in the wiki:
- Getting Started, Examples
- Text and Fonts, Cells, Tables, Graphics, Images
- Barcodes and QR Codes, SVG Support
- Markdown, Bookmarks and Hyperlinks, Viewer Preferences, Metadata and Encryption
- Interactive Forms, Digital Signatures, PDF/A Conformance
- Internals and contributing: Architecture, Contributing
Development
The library source lives under build/. To get started:
git clone https://github.com/dragonofmercy/php-pdf.git cd php-pdf/build composer install composer check # PHPStan (level max) + PHPUnit
See the Contributing wiki page for coding conventions, golden fixture workflow, and how to add new features.
License
MIT - see LICENSE.
Support
If this project helps to increase your productivity, you can give me a cup of coffee :)
