loader image
Quick Tips

Convert HTML Content to image or PDF in Symfony Framework using KnpSnappyBundle

In this article, I am going to explain how to convert a HTML Content/Page into an image/PDF in Symfony framework using KnpSnappyBundle.

Recently I encountered a situation as such to handle a bulk mailing system, wherein it was necessary to convert the html content into images. I used the KnpSnappyBundle of the Symfony framework for the purpose and it was a breeze. I will detail the process for the same below.

The KnpSnappyBundle provides a simple integration for your Symfony project. As always, first we need to install it.


Add the below line in composer.json:


Update the composer.json file. Then enable it in your kernel. For this, within the app/AppKernel.php file,  include the following line:



Next is to configure the installed files as well as the app/config/config.yml

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. If you want to change temporary folder which is sys_get_temp_dir() by default, you can use


Install the utilities if not already installed.

How to install skhtmltopdf and wkhtmltoimage: (for Ubuntu)

Install Wkhtmltopdf manually from the terminal.

The first thing you need to do is download the Wkhtmltopdf files from the website.  To download these files, we use wget -P


This is telling Ubuntu to download the files from the website with sudo (administrator) rights and to place them in the Downloads folder.

Now that you have the files in your Downloads folder, you should navigate to them


The next step is to install it by using dpkg. Dpkg is the package installer from Ubuntu and will be responsible for installing or removing files.

So let us install the package.


Now that it is installed, you still need to move the wkhtmltopdf and wkhtmltoimage files under /usr/bin/ to make Wkhtmltopdf work with Ubuntu and accessible for the user. To do this, first navigate to the correct folder


Now copy the files from your Downloads folder to your bin so that your Odoo can use them.


When you now reload your Odoo or restart the server you can use Wkhtmltopdf and PDFs will be generated automatically!


After installation and configuration, they are ready to be used for our purpose, which is to convert HTML content into images or pdfs.

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.

To Generate an image from an URL


To Generate a pdf document from an URL


To Generate a pdf document from multiple URLs


To Generate a pdf document from a twig view


Below are the methods to render the converted image or pdf:

Render an image as response from a controller


Render a pdf document as response from a controller


Based on the need, the bundle can be used to generate images or pdfs of the required html contents into the specified paths in the Symfony framework. For further understanding, refer the documentation provided for KnpSnappyBundle: https://github.com/KnpLabs/KnpSnappyBundle

The following two tabs change content below.
Vignesh Thandapani

Vignesh Thandapani

An enthusiastic Tech Lead with 6 plus years of experience in Web development arena. Owns legitimate experience in CorePHP, Laravel, Symfony, CakePHP, Wordpress, Joomla. Behalf, a young Aspiring "Travel admirer" craves to live with Nature.

Leave a Reply

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

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.