Developed with love by KnpLabs Hire us for your project!
396

KnpSnappyBundle

by KnpLabs

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

KnpSnappyBundle

Snappy is a PHP (5.3+) 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.

Build Status

knpbundles.com

Installation

With composer, add:

{
    "require": {
        "knplabs/knp-snappy-bundle": "dev-master"
    }
}

Then enable it in your kernel:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        //...
        new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),
        //...

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
knp_snappy:
    pdf:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltopdf
        options:    []
    image:
        enabled:    true
        binary:     /usr/local/bin/wkhtmltoimage
        options:    []

for windows users

# app/config/config.yml
knp_snappy:
    pdf:
        enabled:    true
        binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
        options:    []
    image:
        enabled:    true
        binary:     "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
        options:    []

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 an URL

$container->get('knp_snappy.image')->generate('http://www.google.fr', '/path/to/the/image.jpg');

Generate a pdf document from an URL

$container->get('knp_snappy.pdf')->generate('http://www.google.fr', '/path/to/the/file.pdf');

Generate a pdf document from multiple URLs

$container->get('knp_snappy.pdf')->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

$this->get('knp_snappy.pdf')->generateFromHtml(
    $this->renderView(
        'MyBundle:Foo:bar.html.twig',
        array(
            'some'  => $vars
        )
    ),
    '/path/to/the/file.pdf'
);

Render an image as response from a controller

$html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
    'some'  => $vars
));


return new Response(
    $this->get('knp_snappy.image')->getOutputFromHtml($html),
    200,
    array(
        'Content-Type'          => 'image/jpg',
        'Content-Disposition'   => 'filename="image.jpg"'
    )
);

Render a pdf document as response from a controller

$html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
    'some'  => $vars
));

return new Response(
    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="file.pdf"'
    )
);

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

$pageUrl = $this->generateUrl('homepage', array(), true); // use absolute path!

return new Response(
    $this->get('knp_snappy.pdf')->getOutput($pageUrl),
    200,
    array(
        'Content-Type'          => 'application/pdf',
        'Content-Disposition'   => 'attachment; filename="file.pdf"'
    )
);

Credits

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

Copyright (c) 2010 Knp Labs

The MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
knp_snappy:
temporary_folder: ~
pdf:
enabled: true
binary: wkhtmltopdf
options:

# Prototype
name: []
env: []
image:
enabled: true
binary: wkhtmltoimage
options:

# Prototype
name: []
env: []
  • Add travis badge to readme
    By akovalyov, 1 year ago
  • Merge pull request #77 from nurikabe/patch-1
    By pilot, 1 year ago
  • Merge pull request #81 from KnpLabs/readme-wkhtmltopdf-link
    By akovalyov, 2 years ago
  • Merge pull request #84 from emelendez89/patch-1
    By pilot, 2 years ago
  • Update README.markdown
    By , 2 years ago
  • Update wkhtmltopdf url
    By umpirsky, 2 years ago
  • Fix wkhtmltoimage Windows binary
    By nurikabe, 2 years ago
  • Add configuration for the windows users
    By pilot, 2 years ago
  • Merge pull request #72 from R2c/master
    By pilot, 2 years ago
  • Fixes the dependancy on composer.json for unit tests
    By , 2 years ago
  • add config option for temporary_folder - Unit test
    By R2c, 2 years ago
  • Fix dev branch alias
    By pilot, 2 years ago
  • Merge pull request #62 from piotrantosik/patch-1
    By pilot, 2 years ago
  • Update composer.json
    By piotrantosik, 2 years ago
  • Merge pull request #61 from Nyholm/master
    By pilot, 2 years ago
  • Added travis
    By Nyholm, 2 years ago
  • Merge pull request #59 from steveYeah/master
    By pilot, 2 years ago
  • Move to ternary condition
    By , 2 years ago
  • getOutputFromHtml::getOutputFromHtml() Account for $html can now be array
    By steveYeah, 2 years ago
  • Merge pull request #56 from KnpLabs/fix/logging-array-of-values
    By pilot, 2 years ago
  • Add var_export for logging
    By andrew, 2 years ago
  • Merge pull request #49 from devster/patch-1
    By pilot, 3 years ago
  • Fix missing "extra" node for branch alias
    By devster, 3 years ago
  • Merge pull request #46 from seangofus/master
    By docteurklein, 3 years ago
  • Updated with usage.
    By Sean Gofus, 3 years ago
  • Changed the debug $input to $debug_input so as to not overwrite $input.
    By Sean Gofus, 3 years ago
  • Check if $input is an array and if so flatten to string so sprintf works.
    By Sean Gofus, 3 years ago
  • Changed sprintf to vsprintf to handle arrays that are passed to it. This is to support the commit from the pull request: https://github.com/KnpLabs/snappy/pull/75 that handles multiple inputs.
    By Sean Gofus, 3 years ago
  • Merge pull request #41 from sstok/patch-1
    By pilot, 3 years ago
  • Update composer.json
    By sstok, 3 years ago