Giter Site home page Giter Site logo

ejushiro / knpsnappybundle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from knplabs/knpsnappybundle

0.0 2.0 0.0 103 KB

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

Home Page: http://knplabs.com

License: MIT License

PHP 100.00%

knpsnappybundle's Introduction

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": "~1.4"
    }
}

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 #"\"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
knp_snappy:
    temporary_folder: %kernel.cache_dir%/snappy

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.

knpsnappybundle's People

Contributors

pilot avatar mbontemps avatar akovalyov avatar herzult avatar wouterj avatar seangofus avatar docteurklein avatar l3pp4rd avatar mhor avatar lwagner avatar stephpy avatar jvasseur avatar r2c avatar t3chn0r avatar emelendez89 avatar andrewtch avatar sstok avatar umpirsky avatar piotrantosik avatar nyholm avatar lucascourot avatar stloyd avatar devster avatar craue avatar

Watchers

James Cloos avatar Eduardo Romão avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.