Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/KnpLabs/snappy
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage
https://github.com/KnpLabs/snappy
hacktoberfest html-to-image html-to-pdf image-generation pdf-generation php
Last synced: about 2 months ago
JSON representation
PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage
- Host: GitHub
- URL: https://github.com/KnpLabs/snappy
- Owner: KnpLabs
- License: mit
- Created: 2010-06-16T13:39:52.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2024-02-19T07:26:10.000Z (10 months ago)
- Last Synced: 2024-05-17T12:48:24.818Z (7 months ago)
- Topics: hacktoberfest, html-to-image, html-to-pdf, image-generation, pdf-generation, php
- Language: PHP
- Homepage: https://knplabs.com
- Size: 453 KB
- Stars: 4,347
- Watchers: 133
- Forks: 431
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
- my-awesome-starred - snappy - PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage (PHP)
- awesome-pdf - snappy
- awesome-php - Snappy - A PDF and image generation library. (Table of Contents / PDF)
- awesome-pdf - Snappy - Generate PDFs, thumbnails, and snapshots from websites. (Libraries / PHP)
- awesome-php-cn - Snappy - 一个PDF和图像生成库. (目录 / PDF)
- awesome-projects - Snappy - A PDF and image generation library. (PHP / PDF)
- php-awesome - **Snappy** - 根据 URL 或 HTML 页面生成图片或 PDF 格式的缩略图 (类库 / PDF/条形码)
- awesome-php - Snappy - A PDF and image generation library. (Table of Contents / PDF)
README
# Snappy
![Build Status](https://github.com/KnpLabs/snappy/actions/workflows/build.yaml/badge.svg)
[![AppVeyor CI Build Status](https://ci.appveyor.com/api/projects/status/github/KnpLabs/snappy?branch=master&svg=true)](https://ci.appveyor.com/project/NiR-/snappy)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/KnpLabs/Gaufrette/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/KnpLabs/Gaufrette/?branch=master)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](http://wkhtmltopdf.org/)
available on OSX, linux, windows.You will have to download wkhtmltopdf `0.12.x` in order to use Snappy.
Please, check [FAQ](doc/faq.md) 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`](https://github.com/KnpLabs/KnpSnappyBundle), for Symfony
* [`barryvdh/laravel-snappy`](https://github.com/barryvdh/laravel-snappy), for Laravel
* [`mvlabs/mvlabs-snappy`](https://github.com/mvlabs/MvlabsSnappy), for Zend Framework## Installation using [Composer](http://getcomposer.org/)
```bash
composer require knplabs/knp-snappy
```## Usage
### Initialization
```php
setBinary('/usr/local/bin/wkhtmltopdf');
```### Display the pdf in the browser
```php
$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
```php
$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
```php
$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
```php
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$snappy->generateFromHtml('Bill
You owe me money, dude.
', '/tmp/bill-123.pdf');
```### Pass options to snappy
```php
// 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', 'Bill cover
');
$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.
```php
$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`:
```bash
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:
```bash
composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x
```And then you can use it
```php
setOption('toc', true);
$snappy->setOption('xsl-style-sheet', 'http://path/to/stylesheet.xsl') //or local file;$snappy->generateFromHtml('
Some content
', '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 codeIf 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](https://knplabs.com/en/blog/news-for-our-foss-projects-maintenance)).
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](http://knplabs.com) team.