pdftoolkit / html-to-pdf
Object-oriented HTML and CSS pagination to tagged PDF in native PHP
Requires
- php: ^8.3
- php-64bit: *
- ext-dom: *
- ext-mbstring: *
- masterminds/html5: ^2.10
- pdftoolkit/pdf: ^0.12.2
- pdftoolkit/pdf-fonts: ^0.2.1
- pdftoolkit/pdf-images: ^0.2.2
- sabberworm/php-css-parser: ^9.0
- symfony/css-selector: ^7.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pdftoolkit/pdf-reader: ^0.8.1
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
An independent, object-oriented native PHP HTML/CSS paginator built on the PDFToolkit core. It produces selectable Unicode text, embedded TrueType fonts, images and a logical PDF structure tree without a browser or JavaScript engine.
Version 0.4 extends the documented native input profile. HTML/CSS coverage remains partial, and unsupported features are rejected by default. See coverage, architecture and examples. Tagged output is not a PDF/UA or PDF/A certificate. Development follows CSS1 → CSS2 → CSS2.1 → modules, with explicit qualification gates.
Version 0.4 adds signed word-spacing, letter-spacing and first-line
text-indent, including inherited lengths and percentage indentation. The
CSS1 examples show these text properties alongside the historical
box/float/clear case and independent borders. Text remains selectable and link
annotations follow its adjusted geometry. The registry records 52 partial and
10 unsupported requirements; complete CSS1 remains unqualified.
Install
composer require pdftoolkit/html-to-pdf:^0.4
The core is installed under its canonical Composer name pdftoolkit/pdf:^0.12.2.
If your application directly requires roukmoute/pdf, replace that requirement
with pdftoolkit/pdf:^0.12.2 and update the lock with Composer.
PHP namespaces and APIs are unchanged.
PHP 8.3+ on a 64-bit runtime is required, as are DOM and mbstring. php-64bit
is a Composer platform check, not an extension to install separately. Production
dependencies are stable Composer packages; the PDFToolkit core does not depend
on this optional module.
Convert
use PDF\HtmlToPdf\ConversionOptions; use PDF\HtmlToPdf\Converter; use PDF\HtmlToPdf\Font\FontFace; use PDF\HtmlToPdf\Font\FontSet; use PDF\HtmlToPdf\HtmlSource; $fonts = new FontSet( regular: new FontFace(file_get_contents('fonts/Lato-Regular.ttf')), bold: new FontFace(file_get_contents('fonts/Lato-Bold.ttf')), ); $result = (new Converter())->convert( new HtmlSource('<!doctype html><html lang="en"><title>Example</title>' .'<body><h1>Hello</h1><p>Selectable Unicode: café.</p></body></html>'), new ConversionOptions(fonts: $fonts), ); file_put_contents('example.pdf', $result->pdf);
File reads and writes in this example belong to the calling application. The converter accepts bytes, never fetches URLs, discovers system fonts or writes files implicitly. Supply the actual regular/bold/italic/bold-italic variants used by the document. Missing glyphs, unsupported scripts and absent variants fail explicitly. No synthetic bold or silent fallback font is used.
Usage and resource policies explains page geometry, explicit
image/CSS catalogs, diagnostics and limits. Run the included examples with
php -d memory_limit=512M examples/generate.php _build/examples after composer install.
Development
composer install composer check php -d memory_limit=512M examples/generate.php _build/examples python tools/validate_pdf.py --directory _build/examples
The independent oracle requires the packages in tools/validation-requirements.txt,
qpdf and Poppler. CI validates PHP 8.3, 8.4 and 8.5 and retains generated PDF,
page renders and validation reports. See releasing.
Origins and license
This independent implementation is licensed under MIT and is developed from primary HTML/CSS specifications. Bundled fonts retain their separate OFL licenses; the unchanged historical W3C fixture retains its BSD notice. See primary references and asset provenance.