This plugin for the CakePHP Framework allows you to use the dompdf HTML to PDF converter to easily create PDF documents. It's tightly integrated with the CakePHP view renderer.
Download the repository and extract the archive to a folder called cakephp-dompdf-view in your Plugin folder.
Download dompdf 0.6 and move the content of the archive to Plugin/cakephp-dompdf-view/Vendor/dompdf.
Please create a cache folder for dompdf called dompdf in APP/tmp/cache and change the permissions to 777.
Because of the tight integration between this plugin and CakePHP, there's only a few things you have to do to get started.
- Load the plugin by adding
CakePlugin::load('cakephp-dompdf-view', array('bootstrap' => true));toAPP/Config/bootstrap.php. - Instruct CakePHP to look for the PDF extension by adding
Router::parseExtensions('pdf');toAPP/Config/routes.php. If you already useRouter::parseExtensionsin your project, simply add 'pdf' to the list. - Finally, make sure that the RequestHandlerComponent is loaded for all controllers by adding
public $components = array('RequestHandler');toAPP/Controller/AppController.php.
Eventually, I'll add a paragraph here on how to use the config file located in APP/Plugin/cakephp-dompdf-view/Config/dompdf.php.
You'll need to create the default layout for your PDF documents by creating a file named default.ctp in APP/View/Layouts/pdf.
For each action that should return a PDF, simply create a pdf folder in the folder where the views for the controller reside and create a file with the name of the action inside the pdf folder.
For example, if your controller is named DocumentsController and the action is named view, you need to create the view.ctp file in APP/View/Documents/pdf.
There are a few view variables that you can use to change the behaviour of the view.
downloadSet to true to set aContent-Dispositionheader. This is ideal for file downloads.nameThe filename that will be sent to the user, specified with the extension.paperOrientationThe paper orientation. Must be either 'landscape' or 'portrait'.paperSizeThe paper size. Acceptable values include 'letter', 'legal', 'a4', etc. SeeCPDF_Adapter::$PAPER_SIZES.
Example:
public function view($id)
{
// ...
$params = array(
'download' => false,
'name' => 'example.pdf',
'paperOrientation' => 'portrait',
'paperSize' => 'legal'
);
$this->set($params);
}
To access any action as a PDF, all you have to do is point your browser to the URL associated with the action in question and add '.pdf' at the end just like you would do for JSON or XML.
For example:
http://example.com/documents/view/100.pdf