knplabs/knp-snappy
Snappy is a PHP wrapper for wkhtmltopdf/wkhtmltoimage to generate PDFs, thumbnails, and snapshots from URLs or HTML. Supports Linux/macOS/Windows, configurable binaries and options, and integrates with Symfony and Laravel via community bundles.
composer require knplabs/knp-snappy and ensure wkhtmltopdf (v0.12.x) is installed system-wide or via Composer (e.g., h4cc/wkhtmltopdf-amd64).Pdf generator with the binary path and call getOutput() or generate():
use Knp\Snappy\Pdf;
$snappy = new Pdf('/usr/bin/wkhtmltopdf'); // or vendor path
header('Content-Type: application/pdf');
echo $snappy->getOutput('https://example.com');
barryvdh/laravel-snappy package for seamless service provider registration, config binding (config/snappy.php), and facade usage (SnappyPdf::loadView(...)).generateFromHtml() or getOutput() to merge multiple documents into one PDF.$html = view('reports.monthly', $data)->render();
$snappy->generateFromHtml($html, storage_path('app/reports/monthly.pdf'));
header-html/footer-html options—ensure they’re valid standalone HTML with <!DOCTYPE>.cache-dir option to avoid redundant rendering of identical HTML, or cache output files in storage for reuse.wkhtmltopdf ≥0.12.2 works without xvfb, but if stuck on older versions, use xvfb-run or upgrade binaries.--enable-local-file-access is a major attack vector—only enable it with sanitized, trusted HTML and sandbox the process.page-break-inside:avoid to tr.wkhtmltopdf to ≥0.12.3-dev.encoding => 'utf-8' and ensure HTML <meta charset="UTF-8">.setLogger() with a PSR-3 logger to log exact wkhtmltopdf commands and stderr output.h4cc), verify paths in vendor/h4cc/.../bin/ and test with exec() manually.setlocale(LC_CTYPE, 'es_ES.UTF-8') before instantiation to fix escapeshellarg behavior.RuntimeException with getError() to decode wkhtmltopdf-specific errors (e.g., code 1 often means file not found or invalid CSS).config/snappy.php, use absolute paths for binaries—even base_path('vendor/bin/wkhtmltopdf-amd64') may fail; prefer realpath() or __DIR__ . '/../../vendor/bin/...'.How can I help you explore Laravel packages today?