Skip to content

HTML and CSS to PDF for PHP and Laravel, without a browser

Fagus is a Composer package that turns HTML and CSS into PDF the way Chrome prints them. There is no Chrome, Puppeteer or Node to install, no Docker image and no system library: the rendering happens in a native engine written in Rust.

composer require nforst/fagus
php
use Fagus\Fagus;

Fagus::html($html)->save('invoice.pdf');

Status: early development. It renders real invoices and reports like Chrome prints them, but the CSS coverage is still growing, and releases before 1.0 may change the API.

Why Fagus

  • Simple

    composer require is all it takes, on Linux (x64 and arm64, glibc or musl, so Alpine too), macOS (Intel and Apple silicon) and Windows x64. Every platform prints the same PDF.

  • Fast

    A one-page invoice in about 2.5 ms once the worker runs, ten pages in about 17 ms, measured from PHP on an Apple M2 Pro (tagged PDF). The first document of a process takes about 9 ms. For one page that is 10 to 30 times faster than mPDF, Dompdf or a Chrome kept running.

  • Faithful

    Modern CSS (flex, grid, Tailwind v4 included) renders the way it does in Chrome; page breaks, running headers, page counters and repeated table headers work the way print CSS specifies them.

  • Standards

    PDF/A-3 with ZUGFeRD/Factur-X attachments and accessible PDF (PDF/UA) built in.

  • Safe

    No network access, no files outside the directory you allow, no scripts; the engine has no unsafe code of its own.

PHP and Laravel

The page size, margins, headers and footers come from @page rules in the document, the PDF's title from <title> and its language from <html lang>, as in a browser.

PHP

PHP
use Fagus\Conformance;
use Fagus\Fagus;

$pdf = Fagus::html($html)
    ->baseDirectory(__DIR__.'/resources/pdf')   // relative URLs and the files they may read
    ->resource('logo.png', $logo)                // data the document refers to
    ->pdfA(Conformance::PdfA3b)
    ->facturX($invoiceXml)                       // ZUGFeRD / Factur-X
    ->render();

$pdf->save('invoice.pdf');
$pdf->pages();
$pdf->diagnostics();  // CSS that was ignored, resources that are missing, with their position

Fagus::html($html)->accessible()->save('letter.pdf'); // PDF/UA-1

Laravel, from a Blade view

Laravel
use Fagus\Laravel\Facades\Fagus;

return Fagus::view('invoices.show', ['invoice' => $invoice])->name('invoice.pdf'); // inline
Fagus::view('invoices.show', $data)->download('invoice.pdf');
Fagus::view('invoices.show', $data)->pdfA('3b')->store('invoices/2026-0042.pdf', 's3');

In Laravel 13 the same package registers itself (facade Fagus, Blade views, responses, disks).

Speed

Time per PDF of an invoice, the median of three runs in new processes on an Apple M2 Pro (macOS 27). First is the first PDF of a process, as in a PHP-FPM request (for Fagus it includes starting the worker, for Chrome kept running it does not include starting the browser); every other column is a further PDF in the same process, as in a queue worker or Octane. The invoice uses only CSS 2.1 (tables, no flex or grid) so that every engine lays it out.

Engine1 page, first1 page10 pages40 pages
Fagus9 ms2.3 ms17 ms63 ms
Chrome 157, kept running (as in Gotenberg)103 ms75 ms122 ms217 ms
mPDF 8.363 ms25 ms233 ms¹898 ms¹
Dompdf 3.170 ms48 ms349 ms¹2,550 ms¹
WeasyPrint 66158 ms105 ms668 ms2,596 ms
Browsershot 5.5 (a new Chrome per PDF)611 ms601 ms650 ms751 ms

¹ Same content, other page breaks: Dompdf sets the 10 pages on 12 and the 40 on 44, mPDF (which ignores @font-face and uses its own font) on 8 and 31.

With modern CSS (the invoice of tools/gen.py: flex, grid, margin boxes with page numbers), which Dompdf and mPDF cannot lay out, Fagus takes 2.5 ms for one page and 17.5 ms for ten, Chrome 81 and 123 ms, WeasyPrint 134 and 715 ms. Chrome is measured from Node with Puppeteer; through Gotenberg an HTTP request comes on top. The scripts and documents are in bench/compare.

Compared with other renderers

Dompdf and mPDF stop at CSS 2 with some extensions, without flex or grid; Browsershot, Snappy and Gotenberg need a browser or a service. Fagus sits in between: a PHP package with the CSS of a browser.

FeatureFagusChrome (Browsershot, Gotenberg)WeasyPrintDompdfmPDF
Installs withComposer (downloads one binary)Node, Puppeteer and Chrome, or a servicePython and PangoComposerComposer
LicenceMIT or Apache-2.0MIT, BSDBSD-3-ClauseLGPL-2.1GPL-2.0
Flexbox, gridyesyesyes²nono
Headers and footers in @page margin boxes, "page 2 of 5"yesyesyesno³no³
HTML in headers and footers (position: running()), string-setyesnoyesnono
Table of contents with page numbers (target-counter())yesnoyesnono³
Table headers repeated on every pageyesyesyesyesyes
Hyphenation (hyphens: auto)yesyesyesnoyes
Footnotes (float: footnote)nonoyesnono
JavaScriptno⁴yesnonono
Bookmarks (PDF outline)yeswith an optionyesnono³
Tagged PDF, PDF/UAyestagged onlywith an optionnono
PDF/A2b, 2u, 2a, 3b, 3u, 3anoyesno1b, 3b
ZUGFeRD / Factur-Xbuilt innoby handnoby hand
Same PDF on Linux, macOS and Windowsyesdepends on the fonts installeddepends on the fonts installedyesyes

² WeasyPrint passes the simple cases tested here; its documentation calls flex layout partly supported. ³ mPDF has headers, footers, bookmarks and tables of contents through its own tags (<htmlpageheader>, <bookmark>, <tocpagebreak>), Dompdf page numbers through a PHP callback, not through CSS. ⁴ Fagus runs no scripts on purpose, so that a template cannot reach the network or the server; draw charts on the server instead, as inline SVG or images (with any PHP chart library).

The CSS rows are checked by rendering the documents in bench/compare/probes with every engine (bench/compare/run.sh); the other rows follow each project's documentation. Snappy and laravel-snappy use wkhtmltopdf, whose repository was archived in 2023 and whose WebKit dates from 2012; it is not measured.

Install

PHP 8.3 or newer. Composer asks whether to trust the plugin of nforst/fagus: it installs the fagus binary of the package's release after every composer install and composer update.

composer require nforst/fagus