PDF is most widely used format to share the information online as most computers and mobile devices support PDF files. PDF is read-only and probably the best format for documents sharing.




This tutorial explains how to generate PDF files with Knp-Snappy-Bundle and the wkhtmltopdf library in Symfony. Symfony is an open source PHP Framework, started in 2005 by Fabien Potencier. Now a days, It is widely used in large scale projects.

Knp-Snappy-Bundle is developed by KnpLabs. Knp-Snappy-Bundle is used to convert HTML pages into PDF as well as Images. wkhtmltopdf is command line tool used to convert HTML page into PDF and Image files.

Why Knp-Snappy-Bundle and wkhtmltopdf?

You can use dompdf, tcpdf, knp-snappy-bundle and more on bundles to gengerate PDF in symfony. As per my experience TCPDF or DOMPDF are handy bundles to create PDF files but if you want to create PDF of large data than it mostly return memory exhausted and timeout errors. KnpSnappyBundle can generate large PDF files comparing to other bundles as it uses wkhtmltopdf command line tools to generate PDFs.

 

Create PDF Files Using Knp-Snappy-Bundle and wkhtmltopdf

 

We are using Symfony 2.7 stable version of Symfony framework and 1.4 version of knp-snappy-bundle.

Install wkhtmltopdf Tool

Read: How to install wkhtmltopdf in Linux

Test either installation completed successfully or not by converting sample page.

If above command generates PDF of website successfully than wkhtmltopdf tool is ready to use.

Configure Knp-Snappy-Bundle In Symfony

Add bundle in composer. Append required element with bundle in composer.json file. composer.json file usually placed in root of project.

We have used 1.4 version of knp-snappy-bundle. Now update composer using composer update command. If you do not want to update whole composer than use following composer require command to install knp-snappy-bundle bundle.

 

config.yml

Add below code in config.yml file. Update required information such as binary. set path of excutable file of wkhtmltopdf tools.

 

Make sure you have set correct path.

 

Generate A PDF Document From Twig View

Just use the knp_snappy.pdf service to conver the PDF file from the HTML code generated in a twig template.

Variable $twigoutput holds html of Pdf.html.twig twig file.

That’s it! We are ready to convert webpage to PDF.



  1. I have been browsing online more than 3 hours these days, but I never found any attention-grabbing
    article like yours. It’s lovely worth sufficient for me.
    In my view, if all site owners and bloggers made just right content as you
    probably did, the web will likely be a lot more useful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">