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.

 

Installation:

Add the below line in composer.json:


"require": {
//...,
//...,
"knplabs/knp-snappy-bundle": "dev-master"
}

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


public function registerBundles()
{
$bundles = array(
//...
new Knp\Bundle\SnappyBundle\KnpSnappyBundle(),
//...

 

Configuration:

 

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


# app/config/config.yml
knp_snappy:
temporary_folder: %kernel.cache_dir%/snappy
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\"" for Windows users
options: [] image:
enabled: true
binary: /usr/local/bin/wkhtmltoimage #"\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\"" for Windows users
options: []
 

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


sudo wget -P Downloads http://download.gna.org/wkhtmltopdf/0.12/0.12.1/wkhtmltox-0.12.1_linux-trusty-amd64.deb

 

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

 


cd Downloads

 

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.


sudo dpkg -i wkhtmltox-0.12.1_linux-trusty-amd64.deb

 

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


cd /usr/local/bin

 

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


sudo cp wkhtmltoimage /usr/bin/wkhtmltoimage

sudo cp wkhtmltopdf /usr/bin/wkhtmltopdf

 

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

 

Usage:

 

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


$container->get('knp_snappy.image')->generate('http://www.google.fr', '/path/to/the/image.jpg');

To Generate a pdf document from an URL


$container->get('knp_snappy.pdf')->generate('http://www.google.fr', '/path/to/the/file.pdf');

 

To Generate a pdf document from multiple URLs


$container->get('knp_snappy.pdf')->generate(array('http://www.google.fr', 'http://www.knplabs.com', 'http://www.google.com'), '/path/to/the/file.pdf');

To Generate a pdf document from a twig view


$this->get('knp_snappy.pdf')->generateFromHtml(
$this->renderView(
'MyBundle:Foo:bar.html.twig',
array(
'some' => $vars
)
),
'/path/to/the/file.pdf'
);

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

 

Render an image as response from a controller


$html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
'some' => $vars
));
return new Response(
$this->get('knp_snappy.image')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'image/jpg',
'Content-Disposition' => 'filename="image.jpg"'
)
);

 

Render a pdf document as response from a controller


$html = $this->renderView('MyBundle:Foo:bar.html.twig', array(
'some' => $vars
));

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

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