CakePHP dompdf integration image

I was under a lot of pressure to quickly implement PDF reports in my CakePHP application. Luckily I came across this excellent tutorial.

dompdf is an HTML to PDF converter written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.

I am going to add some flesh to the original and give some additional information based on my implementation.

  1. Starting point:
    1. I am working on a WAMP stack
    2. I have a standard CakePHP applications set-up
    3. I have a Customer in my app (you can naturally adapt to suit)
  2. Download dompdf and place the extracted files in /app/Vendor
  3. Add the following code to /app/Config/routes.php

  4. In your controller, we have to load RequestHandler component so CakePHP can parse the .pdf extension. The post mentions adding it to AppController which makes sense but states that this is not recommended. It did not work for me, so I ended up adding it to the relevant Controllers
  5. Create a new folder in /app/View/Layouts called pdf
  6. Add your new Layout, default.ctp:
  7. Create a new action in our Customer controller. You actually don’t need to do this: if you call the methods that already exist you will get the data you want to proceed, but it does provide for better control on the output for PDF file, eg. the ability to increase the memory allocation.
  8. Duplicate our Customer view.ctp, rename it to view_pdf.ctp to reflect the action name, and place it in a new pdf folder in app/View/Customer.
  9. Here he mentions battling with css files — I managed to get that working — more later!
  10. Create a link to your PDF:
  11. To get my View code a bit more organised, I created two Elements:
    1. pdf-head.ctp
    2. pdf-foot.ctp
  12. One of the issues I had was getting images into my PDF — I wanted a logo at the top right of each page. Note the fullBase attribute in my pdf-head.ctp Element! (This answer on StackOverflow nailed it for me:
  13. Please also note the footer funkiness: the odd order of the include is required to repeat the footer content at the bottom of each page (if that is what you are after).

Thanks again to the author of the original post:

Please note that I have already hit a short-coming of dompdf: it’s inability to handle large file sizes.

5 thoughts on “Integrating dompdf with CakePHP

  1. Hi, do you know if it’s already possible to integrate the dompdf 0.6.2 or 0.7 on cakephp 3.1 ??? Thanks.

      1. I’m facing problems with images extensions like png. When I switched to JPG, everything seems fine.

  2. Take a look at for output pdf files. Supports multiple pdf engines (Including DomPDF), and isn’t “hacky” in the way that it outputs the pdf data. Also supports cakephp 2.x and 3.x.

    Solution for your problem with DomPDF and large files is to not use DomPDF 🙂 If you use the CakePDF plugin, you can use a fantastic tool called WkHTMLToPdf ( which is a fantastic command line tool that uses the native webkit rendering engine to render your html into a pdf file. It is lightning quick and supports massive files (I’ve made a +100MB pdf with it before)

Leave a Reply

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