knplabs/knp-snappy
A PHP library for generating PDFs, thumbnails, and snapshots from URLs or HTML pages.
Repository Health
Technical Analysis
Snappy is a PHP library that turns URLs or raw HTML into PDF documents and image snapshots. It is a thin, ergonomic wrapper around the webkit-based wkhtmltopdf and wkhtmltoimage binaries, handling process invocation, option passing, and output management so you can generate documents with a few method calls.
Because rendering is delegated to wkhtmltox, Snappy produces high-fidelity output that closely matches how a browser would render the page, including CSS and images. It integrates cleanly with Symfony, Laravel, and Laminas via community bundles, making it a common choice for invoice, report, and thumbnail generation in PHP applications.
What You Get
- A
Pdfgenerator that renders URLs or HTML into PDF documents - An
Imagegenerator for thumbnails and page snapshots - A clean option API mapping to wkhtmltopdf/wkhtmltoimage flags
- Helpers to output to a file or return the binary content directly
- Framework integrations for Symfony, Laravel, and Laminas via community bundles
Common Use Cases
- Generating invoices, reports, and receipts as PDFs from HTML templates
- Producing thumbnail images or snapshots of web pages
- Rendering server-side HTML into downloadable documents in web apps
- Batch-converting URLs into PDFs or images from PHP scripts
Under The Hood
Architecture - The library centers on an AbstractGenerator implementing a shared GeneratorInterface, with concrete Pdf and Image subclasses under src/Knp/Snappy. The abstract base assembles the command line from the binary path plus an options array, writes temporary input when given raw HTML, and runs the process, while dedicated exception classes handle error conditions like an existing output file.
Tech Stack - PHP 8.1+ depending on symfony/process to spawn and manage the external wkhtmltox binaries and psr/log for logging. The heavy lifting of rendering is done entirely by the external wkhtmltopdf/wkhtmltoimage executables, which must be installed separately.
Code Quality - The project is mature and actively maintained, using PHPUnit for tests, PHPStan for static analysis, and php-cs-fixer for style, with CI across GitHub Actions and AppVeyor. A clear interface/abstract-class/exception separation and a security-conscious README reflect a well-kept codebase.
API Design - The API is intentionally minimal: $pdf->generateFromHtml($html, $output) or $pdf->getOutputFromHtml($html) cover most needs, and options are passed as a plain array mirroring wkhtmltox flags. The main learning curve comes from understanding wkhtmltopdf’s own options and installing the binary, not from Snappy itself, keeping the wrapper approachable.