• Stars
    star
    4,383
  • Rank 9,792 (Top 0.2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 14 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

Snappy

Build Status AppVeyor CI Build Status Scrutinizer Code Quality

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:

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.

More Repositories

1

Gaufrette

PHP library that provides a filesystem abstraction layer − will be a feast for your files!
PHP
2,466
star
2

php-github-api

A simple PHP GitHub API client, Object Oriented, tested and documented.
PHP
2,133
star
3

KnpPaginatorBundle

SEO friendly Symfony paginator to sort and paginate
PHP
1,741
star
4

KnpMenuBundle

Object Oriented menus for your Symfony project.
PHP
1,397
star
5

KnpMenu

Menu Library for PHP
PHP
1,380
star
6

KnpSnappyBundle

Easily create PDF and images in Symfony by converting html using webkit
PHP
1,234
star
7

DoctrineBehaviors

Doctrine2 behavior traits that help handling Blameable, Loggable, Sluggable, SoftDeletable, Uuidable, Timestampable, Translatable, Tree behavior
PHP
913
star
8

knp-components

Various component pack, includes paginator
PHP
734
star
9

KnpGaufretteBundle

Easily use Gaufrette in your Symfony projects.
PHP
715
star
10

KnpTimeBundle

Provides helpers for time manipulation
PHP
600
star
11

KnpMarkdownBundle

Symfony wrapper for PHP markdown
PHP
417
star
12

KnpRadBundle

Rapid Application Development for Symfony2 [UNMAINTAINED]
JavaScript
291
star
13

KnpBundles

THE unofficial listing of Symfony2 bundles.
PHP
223
star
14

packagist-api

PHP API for Packagist
PHP
182
star
15

FriendlyContexts

Some Behat contexts
PHP
159
star
16

symfony2-autocomplete

A very simple script to autocomplete Symfony2 commands in a bash shell.
Shell
158
star
17

KnpIpsum

A Symfony2 application / base edition with lots of examples to learn about the best bundles
PHP
142
star
18

ConsoleServiceProvider

A Console service provider for Silex
PHP
107
star
19

marketplace

The Marketplace is where KNP ninjas share ideas, projects and stuff. THIS PROJECT IS MAINTAINED BY ubermuda
PHP
104
star
20

DictionaryBundle

Are you often tired to repeat static choices like gender or civility in your apps ?
PHP
81
star
21

KnpJsonSchemaBundle

A bundle to generate json schema based on various metadata
PHP
72
star
22

KnpUserBundle

[DEPRECATED] The development has moved to a new repository: https://github.com/FriendsOfSymfony/FOSUserBundle
PHP
68
star
23

KnpDisqusBundle

A Symfony bundle to fetch and render disqus comments via their API. Your SEO other-half will love it.
PHP
62
star
24

KnpOAuthBundle

You should probably use HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle) instead, though. -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs
PHP
48
star
25

symfony-with-composer

THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs - Symfony2 Standard Distribution, installable through Composer
PHP
46
star
26

KnpConsoleAutocompleteBundle

This Symfony2 bundle provides a simple way of autocompleting your commands in your shell. -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs -- Contact us if you want to be the official maintainer of this Bundle.
PHP
38
star
27

KnpZendCacheBundle

Integrates Zend Cache framework into Symfony2. -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs -- Contact us if you want to be the official maintainer of this Bundle.
PHP
38
star
28

rad-edition

The Knp Rad edition of Symfony2 [UNMAINTAINED]
PHP
36
star
29

ControllerBehaviors

Trait based symfony2 controllers -- THIS PROJECT IS NOT MAINTAINED ANYMORE
PHP
35
star
30

KnpLastTweetsBundle

Symfony2 bundle to easily add a widget with the latest tweets of a Twitter user to a page.
PHP
33
star
31

rad-fixtures-load

A command to load them all
PHP
25
star
32

materialized-path

[DEPRECATED] A simple, database-agnostic tree implementation using materialized paths. /!\ moved to KnpLabs/DoctrineBehaviors
PHP
24
star
33

TwigExplorerBundle

Find your way through the twig extensions
PHP
23
star
34

geekweek12

[UNMAINTAINED]
PHP
21
star
35

mink-demo

THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs - !!! DEPRECATED. Use https://github.com/Behat/MinkExtension-example instead !!!
PHP
20
star
36

RepositoryServiceProvider

Silex Extensions, by KnpLabs -- THIS PROJECT IS MAINTAINED BY ubermuda
PHP
19
star
37

PiwikClient

[UNMAINTAINED] Simple Piwik API client, written in PHP 5.3
PHP
17
star
38

KnpPiwikBundle

Piwik API client bundle for Symfony2 -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs -- Contact us if you want to be the official maintainer of this Bundle.
PHP
17
star
39

MigrationServiceProvider

Simple Doctrine2 migration manager for Silex -- THIS PROJECT IS MAINTAINED BY ubermuda
PHP
16
star
40

MartIrc

[UNMAINTAINED] You're Back To the IRC! MartIrc is a NodeJS wrapper to connect to IRC using a browser supporting Websockets
JavaScript
16
star
41

KnpInvoice

[UNMAINTAINED] Lib to generate invoices
PHP
15
star
42

MegaMAN

Quick access to your vendors documentation
HTML
15
star
43

php-lastfm-client

[UNMAINTAINED] A Last.fm PHP client
PHP
15
star
44

behat-webapi-demo

[UNMAINTAINED] Behat + WebApiContext Demo
PHP
14
star
45

MediaExposer

THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs - A media exposition library written in PHP 5.3
PHP
14
star
46

symfony-light

The "Symfony Light Edition" distribution -- a git oriented lightweight skeleton -- THIS PROJECT IS NOT MAINTAINED ANYMORE
PHP
13
star
47

rad-resource-resolver

Knp Rad Resource Resolver
PHP
12
star
48

server-side-renderer

Docker image to server side render html pages using a browser (supports SPAs)
JavaScript
12
star
49

KnpInvoiceBundle

[UNMAINTAINED] An invoice generator
PHP
11
star
50

KnpMailjetBundle

Symfony2 bundle for handling Mailjet API
PHP
10
star
51

mailjet-api-php

PHP API for Mailjet
PHP
10
star
52

Ciboulette

[UNMAINTAINED] local unobtrusive CI
Shell
9
star
53

KnpMarkupValidatorBundle

[UNMAINTAINED] Provide markup validation for your Symfony2 based projects -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs
PHP
8
star
54

rad-auto-registration

Auto register some common services
PHP
8
star
55

Sn4k3

Hi frand, beleve in bein a snek
PHP
8
star
56

rad-url-generation

Simply auto-complete needed route parameters with existing ones.
PHP
7
star
57

KnpMediaExposerBundle

Integration of the MediaExposer library for your Symfony project. -- THIS PROJECT IS NO LONGER MAINTAINED by KNP Labs -- Contact us if you want to be the official maintainer of this Bundle.
PHP
7
star
58

StaticMapApi

[UNMAINTAINED] This library eases the creation of static maps by generating the URLs for Google's api.
PHP
7
star
59

knp-react-app

A functional oriented react app bootstrap. Do not loose time to configure and bootrstrap a project, use it and make !
JavaScript
6
star
60

muse

Inspires some JSON data basing itself on a JSON schema
PHP
6
star
61

PhpSpecWellDoneExtension

[UNMAINTAINED] Extension for PHPSPEC. Find out which classes are no specification.
PHP
6
star
62

fixtures

THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs - A fixtures framework for your PHP 5.3+ applications
PHP
6
star
63

rad-domain-event

PHP
5
star
64

php-json-schema

PHP
5
star
65

knpDeployPlugin

[UNMAINTAINED] Deployment plugin for symfony 1.4 to deploy to multiple servers and exec commands (migration, clear cache, add crontab) after the deployment on all or specific servers.
PHP
5
star
66

rad-user

Simply handle password encryption and salt generation.
PHP
5
star
67

rad-doctrine-event

Access to your doctrine events from the Symfony DIC.
PHP
5
star
68

should-skip-ci

A CLI tool to skip a CI build that is not concerned by the latest changes.
Rust
4
star
69

knpTime

THIS PROJECT IS DEPRECATED AND WILL NOT BE MAINTAINED ANYMORE. Allows the usage of a fake time to facilitate testing
PHP
4
star
70

symfony-training

The symfony training project
PHP
4
star
71

knp-swagger-generator

An open api 3.0 document generator for your javascript project
TypeScript
4
star
72

KnpRevealBundle

[UNMAINTAINED] Simple bundle that uses Reveal.js and Symfony to make maintainable presentations
JavaScript
4
star
73

rad-security

Provide RAD security components
PHP
3
star
74

knpSsoPlugin

[UNMAINTAINED] SSO security filter for symfony1.4
PHP
3
star
75

KnpRadBundle-documentation

Project codename "SoR" -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs
PHP
3
star
76

jenkins-commentator

Internal app used to comment on github with jenkins build data - from https://gist.github.com/1911084
CoffeeScript
3
star
77

valid-http-cache

HTTP Cache (in)validation library based on HttpFoundation [UNMAINTAINED]
PHP
3
star
78

php-json-schema-bundle

Symfony bundle for the knplabs/php-json-schema library
PHP
2
star
79

rad-components-site

Official site of the RAD components collection
HTML
2
star
80

KnpMenuServiceProvider

Silex service provider for the KnpMenu library
PHP
2
star
81

dorce-hell

ML algorithm for detecting good "Title of your sex-tape" candidates
Python
2
star
82

rad-view-renderer

PHP
2
star
83

knpDoctrineVersionPlugin

[UNMAINTAINED] With this symfony plugin you can directly change or get the current Doctrine migration version.
PHP
2
star
84

KnpRadTable

[UNMAINTAINED] This project will not longer be maintained anymore.
PHP
2
star
85

dmRatablePlugin

[UNMAINTAINED] Allow visitors to rate records
JavaScript
2
star
86

knpMarkdownPlugin

[UNMAINTAINED] Provides a symfony markdown editor widget based on markItUp, with preview.
JavaScript
2
star
87

eslint-config-fp

ESLint configuration for functional programming
JavaScript
2
star
88

knpTestPlugin

[UNMAINTAINED] Base class for symfony functional tests. Uses DB transactions.
PHP
2
star
89

rad-prototype

PHP
2
star
90

knpLocaleAwareNumberPlugin

[UNMAINTAINED] knpValidatorLocaleAwareNumber is a symfony 1.4 plugin providing a validator and a widget to use in a locale aware environment. Useful if you want to use commas instead of dots. -- Time goes by. This project is deprecated. Feel free to contact us if you want to give rebirth to the project and being official maintainer.
PHP
2
star
91

gitbot

[HACKATHON]
JavaScript
1
star
92

synora

Syñora is a synthesizer using native brower api
JavaScript
1
star
93

symfony-twig-react-example

A proof of concept using a Twig powered Symfony app and a React SPA
PHP
1
star
94

sh00t3r

Rust
1
star
95

edgar-hero

A simple animated Edgar
JavaScript
1
star
96

bad-jokes

A React Native toy project (Hackathon #7).
JavaScript
1
star
97

symfony-template

Dockerfile
1
star
98

docker-images

Set of Docker images used for dev purpose at KNPLabs.
Shell
1
star
99

rad-edition-site

Provides a more opiniated edition of Symfony2 than the standard one to accelerate some processes during your development. [UNMAINTAINED]
HTML
1
star
100

knpDmRackspacePlugin

[UNMAINTAINED] Diem plugin to publish your assets automatically to Rackspace File
PHP
1
star