knplabs / knp-snappy
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.
Installs: 52 036 886
Dependents: 65
Suggesters: 4
Security: 2
Stars: 4 395
Watchers: 131
Forks: 436
Open Issues: 12
Requires
- php: >=8.1
- psr/log: ^2.0||^3.0
- symfony/process: ^5.0||^6.0||^7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- pedrotroller/php-cs-custom-fixer: ^2.19
- phpstan/phpstan: ^1.0.0
- phpstan/phpstan-phpunit: ^1.0.0
- phpunit/phpunit: ^8.5
- dev-master / 1.x-dev
- v1.5.0
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- v0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-v2-unstable
- dev-v2/frontend
- dev-v2-unstable.php-cs-fixer
- dev-feat/implement-headless-chromium
- dev-v2-unstable-wkhtmltopdf
- dev-fix/tests-pipeline
- dev-chore/support-symfony-7
- dev-v2-unstable-repository-split
- dev-v2-unstable-add-chromium-headless
- dev-v2-unstable-dompdf
- dev-v2-unstable-change-interfaces
- dev-doc/update-readme
- dev-v2-unstable-proto
- dev-fix/improve-php-cs-fixer-workflow
- dev-change-folder-structure-to-support-multiple-generators
This package is auto-updated.
Last update: 2024-11-19 08:13:36 UTC
README
Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimage available on OSX, linux, windows.
You will have to download wkhtmltopdf 0.12.x
in order to use Snappy.
Please, check FAQ before opening a new issue. Snappy is a tiny wrapper around wkhtmltox, so lots of issues are already answered, resolved or wkhtmltox ones.
Following integrations are available:
knplabs/knp-snappy-bundle
, for Symfonybarryvdh/laravel-snappy
, for Laravelmvlabs/mvlabs-snappy
, for Zend Framework
Installation using Composer
composer require knplabs/knp-snappy
Usage
Initialization
<?php require __DIR__ . '/vendor/autoload.php'; use Knp\Snappy\Pdf; $snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); // or you can do it in two steps $snappy = new Pdf(); $snappy->setBinary('/usr/local/bin/wkhtmltopdf');
Display the pdf in the browser
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); header('Content-Type: application/pdf'); echo $snappy->getOutput('http://www.github.com');
Download the pdf from the browser
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="file.pdf"'); echo $snappy->getOutput('http://www.github.com');
Merge multiple urls into one pdf
$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'));
Generate local pdf file
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); $snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf');
Pass options to snappy
// Type wkhtmltopdf -H to see the list of options $snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); $snappy->setOption('disable-javascript', true); $snappy->setOption('no-background', true); $snappy->setOption('allow', array('/path1', '/path2')); $snappy->setOption('cookie', array('key' => 'value', 'key2' => 'value2')); $snappy->setOption('post', array('key' => 'value')); $snappy->setOption('cover', 'pathToCover.html'); // .. or pass a cover as html $snappy->setOption('cover', '<h1>Bill cover</h1>'); $snappy->setOption('toc', true); $snappy->setOption('cache-dir', '/path/to/cache/dir');
Reset options
Options can be reset to their initial values with resetOptions()
method.
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf'); // Set some options $snappy->setOption('copies' => 4); // .. // Reset options $snappy->resetOptions();
wkhtmltopdf binary as composer dependencies
If you want to download wkhtmltopdf and wkhtmltoimage with composer you add to composer.json
:
composer require h4cc/wkhtmltopdf-i386 0.12.x composer require h4cc/wkhtmltoimage-i386 0.12.x
or this if you are in 64 bit based system:
composer require h4cc/wkhtmltopdf-amd64 0.12.x composer require h4cc/wkhtmltoimage-amd64 0.12.x
And then you can use it
<?php use Knp\Snappy\Pdf; $myProjectDirectory = '/path/to/my/project'; $snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386'); // or $snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');
N.B. These static binaries are extracted from Debian7 packages, so it might not be compatible with non-debian based linux distros
Some use cases
If you want to generate table of contents and you want to use custom XSL stylesheet, do the following:
<?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');
Bugs & Support
If you found a bug please fill a detailed issue with all the following points. If you need some help, please at least provide a complete reproducer so we could help you based on facts rather than assumptions.
- OS and its version
- Wkhtmltopdf, its version and how you installed it
- A complete reproducer with relevant php and html/css/js code
If your reproducer is big, please try to shrink it. It will help everyone to narrow the bug.
Maintainers
KNPLabs is looking for maintainers (see why).
If you are interested, feel free to open a PR to ask to be added as a maintainer.
We’ll be glad to hear from you :)
Credits
Snappy has been originally developed by the KnpLabs team.