“Imagination is everything. It is the preview of life's coming attractions”

- Albert Einstein

  • Html To Pdf Conversion With WebKit In Symfony 2.1.x


    Html To Pdf Conversion With WebKit In Symfony 2.1.x

    Don’t you just love to hear one lovely sentence - "Hey, we’ve got a new server-side generated PDF document to make". When you’re a Php developer, all you can do then is hit yourself in the face with an empty plate to prepare for incoming pain - all FPDF based libraries will make sure of that. But.. there’s a different solution - Wkhtmltopdf, WebKit based shell utility for html to pdf conversion written in C++. Now the question is - how to use it in Symfony 2.1.x framework?

    See the effects for yourself:

    Like all semi-rational people do, let me show everything in a few steps (didn’t expect that, did you?):

    1. First thing that’ll we need here is of course wkhtmltopdf utility, which can be downloaded here. I suggest download of version 0.9.9 (wkhtmltopdf-0.9.9-static-amd64.tar.bz2 or wkhtmltopdf-0.9.9-static-i386.tar.bz2) due to some issues with version 0.11.0. If you’re using Linux, the best place for unpacking archive’s content would be /usr/local/bin/ directory (but you can choose any other that suits you). Remember to grant 755 permissions on the executable.
    2. Next we’ll need to get the KnpSnappyBundle - Symfony 2.x wrapper bundle for the Snappy library which is a Php wrapper for wkhtmltopdf. My preferred way of fetching both is to manually use git clone in root directory of your Symfony2 project:
      git clone https://github.com/KnpLabs/snappy.git vendor/snappy
      git clone https://github.com/KnpLabs/KnpSnappyBundle.git vendor/bundles/Knp/Bundle/SnappyBundle
      
    3. Once we have cloned both repositories, we must add proper namespaces in Symfony2’s composer. I prefer the manual way - you’ll need to add namespaces to the array in vendor/composer/autoload_namespaces.php file, just before '' => $baseDir . '/src/', element:
      return array(
          ...
          // General Knp bundles namespace
          'Knp' => $vendorDir . '/bundles/',
          // Snappy library namespace
          'Knp\\Snappy'  => $vendorDir . '/snappy/src/',
      
    4. Just one more step - we’ll need to add KnpSnappyBundle to app/AppKernel.php to registered namespaces array:
      public function registerBundles()
      {
          $bundles = array(
              ...
              new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),
      
    5. Snappy can be called by using the get service method on any container object. In controller actions the magic is done like this:
      $this->get('knp_snappy.pdf')->generate('http://www.cnn.com', '/var/www/my_app/web/pdf');
      

      Above code will save target webpage to a PDF file. You can also output any twig template in controller return statement like this:

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

    That’s it - welcome to the land of easy html2pdf conversion. All that’s left now is to sit comfortably in your chair and pity all those TCPDF, MPDF and other self-slapping stuff lovers.

    Back