All Projects â†’ KnpLabs â†’ Knpsnappybundle

KnpLabs / Knpsnappybundle

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

Projects that are alternatives of or similar to Knpsnappybundle

Lexikpayboxbundle
LexikPayboxBundle eases the implementation of the Paybox payment system
Stars: ✭ 42 (-95.95%)
Mutual labels:  symfony, symfony-bundle
Beelabrecaptcha2bundle
💻 Symfony bundle for Google Recaptcha2
Stars: ✭ 47 (-95.47%)
Mutual labels:  symfony, symfony-bundle
Sylius
Open Source eCommerce Platform on Symfony
Stars: ✭ 6,598 (+535.65%)
Mutual labels:  symfony, symfony-bundle
Google Analytics Api Symfony
Google Analytics API Symfony Bundle
Stars: ✭ 43 (-95.86%)
Mutual labels:  symfony, symfony-bundle
Seo Bundle
A SEO Solution for duplicate contents, page titles, etc.
Stars: ✭ 45 (-95.66%)
Mutual labels:  symfony, symfony-bundle
Symfony
The Symfony PHP framework
Stars: ✭ 26,220 (+2426.01%)
Mutual labels:  symfony, symfony-bundle
Sncredisbundle
A Redis bundle for Symfony supporting Predis and PhpRedis
Stars: ✭ 980 (-5.59%)
Mutual labels:  symfony, symfony-bundle
Sensiogeneratorbundle
Generates Symfony bundles, entities, forms, CRUD, and more...
Stars: ✭ 634 (-38.92%)
Mutual labels:  symfony, symfony-bundle
Symfony Crawler Bundle
Implements the crawler package into Symfony
Stars: ✭ 8 (-99.23%)
Mutual labels:  symfony, symfony-bundle
Javersphinxbundle
Symfony bundle which provides integration of Sphinx search engine with Symfony using SphinxQL
Stars: ✭ 18 (-98.27%)
Mutual labels:  symfony, symfony-bundle
Doctrine Test Bundle
Symfony bundle to isolate your app's doctrine database tests and improve the test performance
Stars: ✭ 689 (-33.62%)
Mutual labels:  symfony, symfony-bundle
Ismaambrosigeneratorbundle
Generates Symfony2 documents, forms and CRUD for MongoDB documents
Stars: ✭ 27 (-97.4%)
Mutual labels:  symfony, symfony-bundle
Craueformflowbundle
Multi-step forms for your Symfony project.
Stars: ✭ 654 (-36.99%)
Mutual labels:  symfony, symfony-bundle
Alicebundle
A Symfony bundle to manage fixtures with Alice and Faker.
Stars: ✭ 742 (-28.52%)
Mutual labels:  symfony, symfony-bundle
Knpgaufrettebundle
Easily use Gaufrette in your Symfony projects.
Stars: ✭ 646 (-37.76%)
Mutual labels:  symfony, symfony-bundle
Gluggibundle
Modular layout preview system, to be used within a symfony app.
Stars: ✭ 5 (-99.52%)
Mutual labels:  symfony, symfony-bundle
Liipfunctionaltestbundle
Some helper classes for writing functional tests in Symfony
Stars: ✭ 604 (-41.81%)
Mutual labels:  symfony, symfony-bundle
Graphqlbundle
This bundle provides tools to build a complete GraphQL server in your Symfony App.
Stars: ✭ 628 (-39.5%)
Mutual labels:  symfony, symfony-bundle
Api Platform
Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
Stars: ✭ 7,144 (+588.25%)
Mutual labels:  symfony, symfony-bundle
Webfactoryicutranslationbundle
Enables ICU message formatting for translations in Symfony applications.
Stars: ✭ 27 (-97.4%)
Mutual labels:  symfony, symfony-bundle

KnpSnappyBundle

Build Status Scrutinizer Code Quality StyleCI knpbundles.com

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

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].