Add PDF export functionality in SonataAdminBundle

By default SonataAdminBundle doesn’t come with a PDF export functionality. Since this is a nice feature to have in your application here’s a way to add PDF export to the CRUD pages.

The example works with the standard Symfony2 installation and the AcmeDemoBundle. The PDF will be generated using KnpSnappyBundle (the installation process is not covered here).

The Doctrine Entity:


The Color Admin class:

Enable the PDF export link on list page

In order to add the PDF export option the “getExportFormats” method needs to be overridden in the ColorAdmin class:

The “pdf” options should now be displayed on the list page next to the default ones.

Create custom service to handle PDF export

First the default export service (identified by “sonata.admin.exporter”) needs to be overriden. Register the service in src/Acme/DemoBundle/Resources/config/services.xml and make it aware of the knpSnappyPdf. Also the template engine will be necessary.

Next extend the Sonata exporter class as defined in the overridden exporter service:

The last step is to add the Twig template used to render the PDF content, as specified in the custom exporter service.  For example:

Of course, there is room for improvement. You can do it “SonataAdmin” way with a custom PdfWriter class (similar with the built-in XlsWriter, CsvWriter or JsonWriter) to separate the PDF conversion code, instead of having the entire process in the custom Exporter class.

Published by Cristian Radulescu

PHP // MySql // Linux

Join the Conversation


  1. I followed this tutorial as it is but it throws me this exception:
    Attempted to call method “renderView” on class”projectName\AdminBundle\Export\Exporter.
    Any idea how to fix this?

  2. @Rym: I think there is a small error in the code. Try to use “$this->templateEngine->renderView” instead of “$this->renderView”.

    Please let me know if this works in order to update the code.

  3. I agree with Fatma, the service code is not clearly, it’s a bit difficult to understand.

    Would be great correct it


  4. Humm.. I’ve the same problem :
    Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: “Error: Call to a member function renderView() on a non-object”

    $html = $this->templateEngine->renderView(‘AppBundle:Export:pdf.html.twig’, array(
    ‘source’ => $source));
    $content = $this->knpSnappyPdf->getOutputFromHtml($html);

    It’s strange .. (SF 2.8)

  5. @Christophe: Looks like the templating service is not injected in the Exporter. First, you should check if the services.xml file is loaded. If this is not the problem, try to debug the Exporter::setTemplateEngine() method and see if the templating service is correctly set.

  6. thanks for this tutorial .
    I have a problem with the template
    this my error :
    Unable to find template “UserBundle:Export:pdf.html.twig” (looked into: /var/www/html/public/siemnew/app/Resources/views, /var/www/html/public/siemnew/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form, /var/www/html/public/siemnew/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views).

  7. For the people using YAML instead of XML here’s the code of the service :

    class: BlogBundle\Export\Exporter
    – [setKnpSnappyPdf, [@knp_snappy.pdf ]]
    – [setTemplateEngine, [@templating ]]

    I had a problem with the path of wkhtmltopdf in my config.yml, and this solved my problem :

    1 – copy the “wkhtmltopdf” folder from “C:\programs” to your “app/Resources/create_a_folder_named_native”

    2 – in your config.yml put this :
    binary: “%kernel.root_dir%/Resources/native/wkhtmltopdf/bin/wkhtmltopdf.exe”

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.