Install Snappy via Composer
masterInstall the Snappy library using Composer to enable PDF and image generation from URLs or HTML pages.
composer require knplabs/knp-snappyrepository·master·Indexed 26 days ago
https://github.com/knplabs/snappyA PHP library that acts as a wrapper around the wkhtmltopdf and wkhtmltoimage binaries. It enables developers to generate PDFs, snapshots, or thumbnails from URLs or HTML content, featuring support for custom options, headers, footers, and Table of Contents.
Install the Snappy library using Composer to enable PDF and image generation from URLs or HTML pages.
composer require knplabs/knp-snappyWhen converting HTML files that use relative links or media URLs, you must either:
<base> HTML tag to specify the base URL for the document.setLogger() method on your generator instance.If you do not have wkhtmltopdf installed on your system, you can install static binaries as Composer dependencies. Note that these are extracted from Debian 7 packages and may not be compatible with non-Debian Linux distributions.
# For i386 systems
composer require h4cc/wkhtmltopdf-i386 0.12.x
composer require h4cc/wkhtmltoimage-i386 0.12.x
# For 64-bit systems
composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.xTo prevent tables from breaking incorrectly across pages, use proper <thead> and <tbody> tags and apply the following CSS:
table { page-break-inside:auto; }
tr { page-break-inside:avoid; page-break-after:auto; }
thead { display:table-header-group; }
tfoot { display:table-footer-group; }To manually force a page break, use the CSS page-break-after: always; on a container element:
<style type="text/css">
.page {
overflow: hidden;
page-break-after: always;
}
</style>
<div class="page">
new page
</div>To add a header or footer to every page, provide either a valid file path or an HTML string via the header-html or footer-html options.
Important requirements:
<html>, <head>, and <body> tags..html extension to avoid 'unknown error' exit codes.wkhtmltopdf versions compiled against unpatched Qt (common in Linux distributions). Use the official version from wkhtmltopdf.org for best results.<?php
require __DIR__ . '/vendor/autoload.php';
$header = <<<HTML
<!DOCTYPE html>
<html
<head><style type="text/css">p { color: #FF0000; }</style></head>
<body><p>Lorem ipsum</p></body>
</html>
HTML;
$footer = <<<HTML
<!DOCTYPE html>
<html
<head><style type="text/css">p { color: #0000FF; }</style></head>
<body><p>Lorem ipsum</p></body>
</html>
HTML;
// Use a temporary file with .html extension for the footer path
$footerPath = tempnam('/tmp', 'footer') . '.html';
file_put_contents($footerPath, $footer);
$pdf = new \Knp\Snappy\Pdf(__DIR__ . '/vendor/bin/wkhtmltopdf-amd64');
$pdf->generateFromHtml('', '/tmp/out/test.pdf', ['header-html' => $header, 'footer-html' => $footerPath], true);wkhtmltopdf: cannot connect to X server, ensure you are using wkhtmltopdf >= 0.12.2, which no longer requires an X server or emulation. If you cannot update, you may need to use xvfb to provide a virtual X server.To ensure accented characters and UTF-8 symbols render correctly:
<meta charset="UTF-8" /> in your HTML document.encoding option as utf-8 to the generator.If accents in options (like footer-right) are being mangled (e.g., 'Página' becoming 'Página'), set the server locale using setlocale(LC_CTYPE, 'your_locale.UTF-8'). Ensure the locale is actually installed on your server by running locale -a.
wkhtmltopdf returns an error code (such as 1 for ContentNotFoundError), the generate method will throw a RuntimeException. You should catch this exception and check the error code to handle the failure appropriately.You can generate a single PDF from multiple URLs or multiple HTML strings by passing an array of inputs to the generate() or generateFromHtml() methods instead of a single string.
<?php
$pdf = new \Knp\Snappy\Pdf(__DIR__ . '/vendor/bin/wkhtmltopdf-amd64');
$pdf->generate(['https://google.com', 'https://google.jp'], '/tmp/out/test.pdf');
// or
$pdf->generateFromHtml(['<html><body>Doc 1</body></html>', '<html><body>Doc 2</body></html>'], '/tmp/out/test.pdf');Pass an array of URLs to getOutput() to combine them into one continuous PDF document.
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput(array('http://www.github.com','http://www.knplabs.com','http://www.php.net'));To create a PDF with a Table of Contents (TOC) using a custom XSL stylesheet, enable the toc option and provide the path to your .xsl file via xsl-style-sheet.
<?php
$snappy = new Pdf('/path/to/binary');
$snappy->setOption('toc', true);
$snappy->setOption('xsl-style-sheet', 'http://path/to/stylesheet.xsl'); // or local file
$snappy->generateFromHtml('<p>Some content</p>', 'test.pdf');