Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Say hello to redesigned Cloudways, an empowering Startup Program, enhanced Staging, a new Let’s Encrypt Wildcard SSL certificate feature, and more. GET STARTED

PDF Generation in Symfony Using KnpSnappyBundle

Updated on  15th August

5 Min Read
Reading Time: 5 minutes

When you are working on ecommerce projects or any invoice generation application, a much needed feature for you is the PDF generation of the records. This task sometimes can become a great pain due to non-availability of server side libraries for Symfony PDF generation. On the client side though, the task is easier to achieve.

While working with Symfony you often require PDF generation for your ecommerce or finance applications. In this article I will show you how you can generate PDFs with Symfony framework on the Cloudways Web Hosting for PHP platform.

PDF Generation in Symfony with knp snappy bundle

For this purpose, we will be using KnpSnappyBundle from Knpbundles. Snappy is a PHP (5.3+) wrapper for the wkhtmltopdf conversion utility. It allows you to generate PDF or image files directly from your html documents, using the webkit engine. The KnpSnappyBundle provides a simple integration for your Symfony. To enable this integration, I will be undertaking a two step process:

  1. Install Symfony in Cloudways app
  2. Add wkhtmltopdf binaries to the app

For the purpose of this article I’m assuming that you have already signed up for a Cloudways account and have launched the custom PHP app. Now move on to your application and let’s add wkhtmltopdf binaries.

You can find your required files at wkhtmltopdf official website. Since you wouldn’t have the root access on Cloudways servers, I will need to work a bit to add linux binaries of wkhtmltopdf. I will use the linux files listed below:

Now to add these binaries in your Symfony project; open SSH from server panel and cd to your application folder. Now run the following wget command:

This command will add folder to your directory now. I will rename this folder and unzip and extract it using the following command:

And that’s it! I now have the binary files installed and available for my project. Notice however, that they are not yet configured. I will be adding the the bin folder path in config file but it’s not a required step just yet so I’ll do it at a later stage.

Related: How To Create Simple REST API In Symfony 3.1

Install & Enable KnpSnappybundle

The next step is to enable KnpSnappybundle in the Symfony project. Using the composer is the most preferred way to install it. Run the following composer command:

The bundle is added now! Open Appkernel.php file and enable it:

Add bin Path to Snappy Config

I will now be configuring wkhtmltopdf by adding the binary file path in config. Open config.yml file and add the following code if you have root access:

Since I don’t have root access on Cloudways servers so I need to replace the binary path. The binary path will be the directory where I extract the wkhtmltopdf zip. I will find the absolute path using the pwd command:

Replace the above path with your directory path of bin folder:

The wkhtmltopdf is completely configured now! You can also add page settings in config file:

Generate First PDF File with URL

Now that our stage is set, it’s time to finally generate the first PDF file. I will use specific website URL to convert the homepage in PDF file. For this purpose I will use the DefaultController.php file and add the following code:

Now I will run the application URL and append the route “/” with it. The PDF file should be downloaded.

Now let’s try to add some custom view to the PDF file. To achieve this, first create a twig file in views folder. I will use template.html.twig and add the following code in it:

and in the template.html.twig file, add the simple code.

The file will need to downloaded. You should see a window like this pop up. I have IDM so in my case, it is being downloaded through IDM.

When you first open the newly created PDF file you will see the rendered twig file which I created earlier:

You might also like: How To Add ReCAPTCHA To Symfony 3 Forms

Also I have sent some values from controller to the view file. You can do this too.

Final Words

KnpSnappyBundle along with wkhtmltopdf make for a simple way to Symfony PDF generation. I have provided a basic code and procedure in this article that you can work with to create your own PDF files easily. This template can also serve as the stepping stone for more advanced and complex PDF file creations with more data.

If you have any queries and questions please comment below.

Share your opinion in the comment section. COMMENT NOW

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.