How To Generate PDF Files Using KnpSnappyBundle and wkhtmltopdf in symfony
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.
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.
1
|
wkhtmltopdf http://www.google.com output.pdf
|
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.
1
2
3
|
"require": {
"knplabs/knp-snappy-bundle": "~1.4"
}
|
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.
1
|
composer require knplabs/knp-snappy-bundle
|
config.yml
Add below code in config.yml
file. Update required information such as binary
. set path of excutable file of wkhtmltopdf tools.
1
2
3
4
5
6
|
knp_snappy:
temporary_folder: %kernel.cache_dir%/snappy
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf #Path of wkhtmltopdf tools
options: []
|
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.
1
2
3
4
5
6
7
8
9
|
$twigoutput = $this->renderView('AppBundle:ExportPdf:Pdf.html.twig', array());
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($twigoutput),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="pdf-output.pdf"'
)
);
|
Variable $twigoutput
holds html of Pdf.html.twig
twig file.
That’s it! We are ready to convert webpage to PDF.
Jane
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.