typesetterio / typesetter
The main typesetter service for generating PDFs from Markdown for ebooks.
Requires
- php: ^8.4
- ext-dom: *
- ext-libxml: *
- league/commonmark: ^2.4
- mpdf/mpdf: ^8.2.1
- symfony/yaml: ^7.0 || ^8.0
- tempest/highlight: ^2.28.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- mikey179/vfsstream: ^1.6.12
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^13.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This is the Typesetter main service. This can be used in your own projects directly if you want. You probably want to head over to typesetter-cli though.
Install
This requires PHP 8.4 or above.
composer require typesetterio/typesetter
Usage
Build a Config from an array of options, then pass it to Typesetter::generate() to get a PDF binary back from MPDF.
Example:
$config = new \Typesetterio\Typesetter\Config([ 'title' => 'Benjamin Button', 'author' => 'F. Scott Fitzgerald', 'theme' => 'bb', 'content' => 'content', 'toc-enabled' => true, 'toc-links' => true, 'toc-header' => 'Table of Contents', 'toc-levels' => ['H1', 'H2'], 'toc-attributes' => ['resetpagenum' => 1], 'footer' => '{PAGENO}', 'mpdf' => [ 'fontDir' => ['bb/fonts'], 'fontdata' => [ 'garamond' => ['R' => 'Garamond.ttf', 'B' => 'Garamond-Bold.ttf', 'I' => 'Garamond-Italic.ttf'], ], ], 'markdown-extensions' => ['md', 'markdown'], 'observers' => [ new \Typesetterio\Typesetter\Observers\DefaultMarkdownConfiguration(), new \Typesetterio\Typesetter\Observers\FirstElementInChapterCssClass(), new \Typesetterio\Typesetter\Observers\BreakToPageBreak(), new \Typesetterio\Typesetter\Observers\Credits(), ], ]); $service = new \Typesetterio\Typesetter\Typesetter(); $pdfContent = $service->generate($config); file_put_contents('my-pdf.pdf', $pdfContent);
toc-attributes takes any attribute of mPDF's <tocpagebreak> tag. mpdf takes any option of the mPDF constructor; fontDir and fontdata are added to mPDF's bundled fonts.
To learn more, please check out the documentation. This details configuration, customization, themes and cover generation, observers, listeners and more.
Demo
php demo/run.php generates demo/output.pdf, a book that exercises the library's features and states on each page what it should look like.
Development
Requires PHP 8.4 or newer and Composer. make act additionally needs Docker and act. On macOS, brew install php composer act.
Run make to list the available targets.
Changelog
See CHANGELOG.md.
Credits
This was heavily influenced by the Ibis project but is a complete rewrite.
This package stands on the shoulders of giants like MPDF and the League Commonmark library.
Aaron Saray is the primary author and maintainer.

