Book Image

CakePHP 2 Application Cookbook

Book Image

CakePHP 2 Application Cookbook

Overview of this book

Table of Contents (20 chapters)
CakePHP 2 Application Cookbook
Credits
Foreword
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Writing some PDF content to a file


You may want to create a PDF and save it to a file on your server. The CakePdf plugin can handle this with just the few following lines of code:

App::uses('CakePdf', 'CakePdf.Pdf');

$CakePdf = new CakePdf();
$CakePdf->template('example', 'report');
$CakePdf->write(WWW_ROOT . 'files' . DS . 'report.pdf');

First, we create an instance of the CakePdf class. The template() method then lets you define the view to use, followed by the optional layout. Note that the view, in this case, will be located in app/View/Pdf/.

If you wanted to handle the PDF output some other way, you can use the output() method on the CakePdf class to return the raw output that was generated.

See also

  • The Including a plugin recipe from Chapter 1, Lightning Introduction

  • The Using blocks recipe