How to generate PDF in CakePHP 2.x with dompdf

13 November 2012

Posted in CakePHP

How to generate PDF in CakePHP 2.x with dompdf

For some applications, generating PDF file is crucial. Let's imagine invoicing applications, created with CakePHP, generating PDF is a must. But how to do this? How to generate the file without using CLI?We will be using dompdf for this task. You can get it free and licensed under LGPL. 

Make sure your meet the requirement:

  1. Using latest version of CakePHP 2.2.x
  2. Running server with PHP 5.3.x (never tested with PHP 5.2)
  3. Latest version of dompdf 0.6.0 Beta 3

Let's get started!

Step 1

Download dompdf, extract and move the folder to our app/Vendor. Make sure the folder name is dompdf.

Image of dompdf inside Vendor's folder

Step 2

Open our app/Config/routes.php and add the following code:

Router::parseExtensions('pdf');

Step 3

On our controller, we have to load RequestHandler component so CakePHP can parse .pdf extension.

class PostsController extends AppController {
	
	public $components = array('RequestHandler');

	// the rest of the codes
}

We can load it in our AppController as well so it will be available to other controllers.

Step 4

Create new folder, pdf, in app/View/Layouts, then create a file, default.ctp and add the following codes:

<?php 
require_once(APP . 'Vendor' . DS . 'dompdf' . DS . 'dompdf_config.inc.php');
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->set_paper = 'A4';
$dompdf->load_html(utf8_decode($content_for_layout), Configure::read('App.encoding'));
$dompdf->render();
echo $dompdf->output();

Step 5

For better control on the output for PDF file, we will create new action in our controller. 

public function view_pdf($id = null) {
	$this->Post->id = $id;
	if (!$this->Post->exists()) {
		throw new NotFoundException(__('Invalid post'));
	}
	// increase memory limit in PHP 
	ini_set('memory_limit', '512M');
	$this->set('post', $this->Post->read(null, $id));
}

By doing this, we can performs additional task for the output, for example increasing PHP memory limit, generating invoice numbers and so on.

Step 6

Duplicate our view.ctp and rename it to view_pdf.ctp to reflect our action name. You can add CSS codes inside this files since linking to external CSS files is not working for me at the moment.

Step 7

Now we can create a link to generate the PDF file using these code:

<?php echo $this->Html->link(__('PDF'), array('action' => 'view_pdf', 'ext' => 'pdf', $post['Post']['id'])); ?>

Done!

Now you can easily generate PDF file in your CakePHP application.

Credit:

  1. http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2/
  2. http://stackoverflow.com/questions/8158129/loading-vendor-files-in-cakephp-2-0

Comments (6)

  • karim

    karim

    04 December 2012 at 16:46 |
    This is best done as a business method rather than controller action. It does not necessarily fit mvc for example if you want ti generate a batch of invoices or send pdf as an email attachment. Best to make it a business method instead.
    • Syahril Zulkefli

      Syahril Zulkefli

      05 December 2012 at 15:23 |
      totally agree with you. this is just a quick and dirty implementation :)
  • karim

    karim

    05 December 2012 at 15:57 |
    I point this out because I fell into this trap myself when developing my Zend Framework/dompdf application.
  • cedric

    cedric

    17 December 2012 at 19:36 |
    Brillant post man! it work well and very clear explanations.
    thanks again
  • Kapil Bhagchandani

    Kapil Bhagchandani

    29 December 2012 at 19:08 |
    This tutorial looks great for fast bake . But i am getting trouble using the same on my server . and i have to manually call core
    header function in my action to indicate the content type as pdf instead of html . might be a fix is needed . Is there any better way of doing so .
  • Jason

    Jason

    27 January 2013 at 17:58 |
    you maybe have to call
    [CODE]$this->response->type('pdf');[/CODE]
    in the controller before "render"
    to avoid scrambled output in the browser

Leave a comment

You are commenting as guest.