Welcome to Abdul Malik Ikhsan's Blog

Symfony2 : Snappy Bundle – Create PDF from HTML

Posted in Symfony2 Framework, Tutorial PHP by samsonasik on August 15, 2011

Snappy adalah Bundle yang digunakan untuk utilitas konversi wkhtmltopdf di PHP 5.3, memungkinkan kita untuk  menghasilkan file Pdf dari dokumen html. KnpSnappyBundle menyediakan integrasi dengan Symfony2 Framework. Untuk bisa dapat melakukan konversi, terlebih dahulu kita download dulu binary wkhtmltopdf di sini : http://code.google.com/p/wkhtmltopdf/ . Setelah dapat file binary-nya, kita install deh. Setelah kita install, kita bisa mengintegrasikan dengan Symfony2 dengan cepat dan mudah.

Pertama, kita konfigurasi dulu :

# app/config/config.yml
knp_snappy:
    pdf:
        enabled:    true
        binary:     C:\wkhtmltopdf\wkhtmltopdf.exe #path to binary wkhtmltopdf
        options:    []

Kemudian, kita unduh vendornya dari git repository : ( install dulu git version controlnya )

git submodule add https://github.com/knplabs/snappy.git vendor/snappy

Kita unduh juga Bundle-nya :

git submodule add https://github.com/knplabs/KnpSnappyBundle.git vendor/bundles/Knp/Bundle/SnappyBundle

Kalau sudah, kita daftarkan dalam autoload :

$loader->registerNamespaces(array(
//................SNAPPY..........//
'Knp'                        => __DIR__.'/../vendor/bundles',
'Knp\\Snappy'                => __DIR__.'/../vendor/snappy/src'
//-----------------------------------------------
));

Kita tambahkan pula dalam Kernel :

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

Okey, time to testing :

        $html = $this->renderView('Testmodule:Test:index.html.twig', array(
          'some'  => 'somethingvalue'
        ));

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

Semoga bermanfaat 🙂

Referensi :
http://pooteeweet.org/blog/0/1915?utm_source=twitterfeed&utm_medium=laconi#m1915
http://symfony2bundles.org/knplabs/KnpSnappyBundle
https://github.com/knplabs/KnpSnappyBundle/blob/master/README.markdown

7 Responses

Subscribe to comments with RSS.

  1. CyberSpace said, on August 26, 2011 at 11:29 pm

    Sungguh Sempurna… Thanks Share ya bro

  2. samsonasik said, on September 4, 2011 at 12:05 am

    silakan asal menyertakan sumber aslinya 🙂

  3. Muziasih Rukmantoro said, on November 14, 2011 at 6:08 pm

    pengen belajar Symfony nih mas…help me dunk ..

  4. samsonasik said, on November 15, 2011 at 3:24 pm

    boleh 🙂

  5. Diego Delon said, on September 25, 2014 at 4:53 pm

    If you need a module that allow easy to thumbnail, snapshot or PDF generation from a url or a html you can take a look to MvlabsSnappy based on Snappy PHP (5.3+) wrapper for the wkhtmltopdf & wkhtmltoimage conversion utility.

  6. Irwan Ismail said, on October 3, 2014 at 11:10 am

    kalau mau generate pdf bagi protected page gimana ya?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: