2017 © Pedro Peláez
 

library knp-snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

image

knplabs/knp-snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  • Monday, January 22, 2018
  • by Knplabs
  • Repository
  • 136 Watchers
  • 2856 Stars
  • 7,751,666 Installations
  • PHP
  • 41 Dependents
  • 3 Suggesters
  • 351 Forks
  • 31 Open issues
  • 25 Versions
  • 7 % Grown

The README.md

Snappy

Build Status AppVeyor CI Build Status Scrutinizer Code Quality, (*1)

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., (*2)

You will have to download wkhtmltopdf 0.12.x in order to use Snappy., (*3)

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., (*4)

Following integrations are available: * knplabs/knp-snappy-bundle, for Symfony * barryvdh/laravel-snappy, for Laravel * mvlabs/mvlabs-snappy, for Zend Framework, (*5)

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., (*6)

$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:, (*7)

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:, (*8)

composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x

And then you can use it, (*9)

<?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, (*10)

Some use cases

If you want to generate table of contents and you want to use custom XSL stylesheet, do the following:, (*11)

<?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('

Some content, (*12)

', '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., (*13)

  • 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., (*14)

Maintainers

KNPLabs is looking for maintainers (see why)., (*15)

If you are interested, feel free to open a PR to ask to be added as a maintainer., (*16)

We’ll be glad to hear from you :), (*17)

Credits

Snappy has been originally developed by the KnpLabs team., (*18)

The Versions

22/01 2018

dev-master

9999999-dev http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

22/01 2018

v1.0.4

1.0.4.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

22/01 2018

2.0.x-dev

2.0.9999999.9999999-dev http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

04/12 2017

v1.0.3

1.0.3.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

04/10 2017

v1.0.2

1.0.2.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

21/09 2017

v1.0.1

1.0.1.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

20/09 2017

v1.0

1.0.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

16/06 2017

v0.5.0

0.5.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

17/11 2015

0.4.3

0.4.3.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

13/11 2015

dev-doc/locales

dev-doc/locales http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

16/09 2015

0.4.2

0.4.2.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

10/08 2015

0.4.1

0.4.1.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

04/08 2015

dev-doc/faq

dev-doc/faq http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

knplabs pdf thumbnail wkhtmltopdf snapshot knp

07/05 2015

0.3.4

0.3.4.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

07/05 2015

0.4

0.4.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

11/03 2015

0.3.3

0.3.3.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

19/12 2014

0.3.2

0.3.2.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

26/11 2014

0.3.1

0.3.1.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

24/11 2014

0.3.0

0.3.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

14/10 2014

0.2.1

0.2.1.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

knplabs pdf thumbnail wkhtmltopdf snapshot knp

20/03 2014

0.2.0

0.2.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

knplabs pdf thumbnail wkhtmltopdf snapshot knp

06/09 2013

0.1.2

0.1.2.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

knplabs pdf thumbnail wkhtmltopdf snapshot knp

10/04 2013

0.1.1

0.1.1.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

knplabs pdf thumbnail wkhtmltopdf snapshot knp

07/11 2012

dev-refactoring

dev-refactoring http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

 

knplabs pdf thumbnail snapshot knp

07/11 2012

0.1.0

0.1.0.0 http://github.com/KnpLabs/snappy

PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

knplabs pdf thumbnail snapshot knp