• Stars
    star
    1,234
  • Rank 38,045 (Top 0.8 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 14 years ago
  • Updated 3 months ago

Reviews

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

Repository Details

Easily create PDF and images in Symfony by converting html using webkit

KnpSnappyBundle

Build Status Scrutinizer Code Quality StyleCI

Snappy is a PHP wrapper for the wkhtmltopdf conversion utility. It allows you to generate either pdf or image files from your html documents, using the webkit engine.

The KnpSnappyBundle provides a simple integration for your Symfony project.

Limitations

If you use JavaScript to render your pages, you may encounter some issues because of wkhtmltopdf not being fully compatible with ES6 apis. The only way to solve this issue is to provide polyfills that fix the gaps between modern ES6 apis and the wkhtmltopdf rendering engine.

Installation

With composer, require:

composer require knplabs/knp-snappy-bundle

Then enable it in your kernel (optional if you are using the Flex recipe with Symfony >= 4) :

// config/bundles.php
<?php

return [
    //...
    Knp\Bundle\SnappyBundle\KnpSnappyBundle::class => ['all' => true],
    //...
];

Configuration

If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration.

# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe)
knp_snappy:
    pdf:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltopdf #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\"" for Windows users
        options:    []
    image:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltoimage #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\"" for Windows users
        options:    []

If you want to change temporary folder which is sys_get_temp_dir() by default, you can use

# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe)
knp_snappy:
    temporary_folder: "%kernel.cache_dir%/snappy"

You can also configure the timeout used by the generators with process_timeout:

# app/config/config.yml (or config/packages/knp_snappy.yaml if using Symfony4 and the Flex recipe)
knp_snappy:
    process_timeout: 20 # In seconds

Usage

The bundle registers two services:

  • the knp_snappy.image service allows you to generate images;
  • the knp_snappy.pdf service allows you to generate pdf files.

Generate an image from a URL

// @var Knp\Snappy\Image
$knpSnappyImage->generate('http://www.google.fr', '/path/to/the/image.jpg');

Generate a pdf document from a URL

// @var Knp\Snappy\Pdf
$knpSnappyPdf->generate('http://www.google.fr', '/path/to/the/file.pdf');

Generate a pdf document from multiple URLs

// @var Knp\Snappy\Pdf
$knpSnappyPdf->generate(array('http://www.google.fr', 'http://www.knplabs.com', 'http://www.google.com'), '/path/to/the/file.pdf');

Generate a pdf document from a twig view

// @var Knp\Snappy\Pdf
$knpSnappyPdf->generateFromHtml(
    $this->renderView(
        'MyBundle:Foo:bar.html.twig',
        array(
            'some'  => $vars
        )
    ),
    '/path/to/the/file.pdf'
);

Render an image as response from a controller

use Knp\Bundle\SnappyBundle\Snappy\Response\JpegResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SomeController extends AbstractController
{
    public function imageAction(Knp\Snappy\Image $knpSnappyImage)
    {
        $html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
            'some'  => $vars
        ));

        return new JpegResponse(
            $knpSnappyImage->getOutputFromHtml($html),
            'image.jpg'
        );
    }
}

Render a pdf document as response from a controller

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SomeController extends AbstractController
{
    public function pdfAction(Knp\Snappy\Pdf $knpSnappyPdf)
    {
        $html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
            'some'  => $vars
        ));

        return new PdfResponse(
            $knpSnappyPdf->getOutputFromHtml($html),
            'file.pdf'
        );
    }
}

Render a pdf document with a relative url inside like css files

use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SomeController extends AbstractController
{
    public function pdfAction(Knp\Snappy\Pdf $knpSnappyPdf)
    {
        $pageUrl = $this->generateUrl('homepage', array(), true); // use absolute path!

        return new PdfResponse(
            $knpSnappyPdf->getOutput($pageUrl),
            'file.pdf'
        );
    }
}

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

SnappyBundle and Snappy are based on the awesome wkhtmltopdf. SnappyBundle has been developed by KnpLabs.

More Repositories

1

snappy

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

Gaufrette

PHP library that provides a filesystem abstraction layer βˆ’ will be a feast for your files!
PHP
2,466
star
3

php-github-api

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

KnpPaginatorBundle

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

KnpMenuBundle

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

KnpMenu

Menu Library for PHP
PHP
1,380
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