PDF Generation in Symfony Using KnpSnappyBundle

by Shahroze Nawaz  August 15, 2017

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 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.

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:

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.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About 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 shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...