Transcript
Page 1: Building PHP Documents with CakePdf - CakeFest 2012

BUILDING PHP DOCUMENTS WITH CAKEPDF

CakeFest Manchester, 2012

Page 2: Building PHP Documents with CakePdf - CakeFest 2012

ABOUT ME

• Jelle Henkens

• LemonBE on IRC, @lemonit on Twitter

• Belgian in the UK

• Lead Developer at Geneo Software

• CakePHP Core Team Developer

• Founder of followmy.tv

Page 3: Building PHP Documents with CakePdf - CakeFest 2012

WHAT IS CAKEPDF

• Generate PDF documents from HTML

• Easily pick which library to render the PDF

• PDF Encryption

• Layouts, views, helpers and more

• Use your own encryption / render engine

Page 4: Building PHP Documents with CakePdf - CakeFest 2012

WHY IT WAS BUILT• Massive differences in API between pdf libraries

// Using DomPdf

require_once("dompdf_config.inc.php");

$html = '<html><body>'. '<p>Put your html here.</p>'. '</body></html>';

$dompdf = new DOMPDF();$dompdf->set_paper('A4', 'landscape');$dompdf->$dompdf->load_html($html);$dompdf->render();$pdfData = $dompdf->output();

// Tcpdf example

require_once('config/lang/eng.php');require_once('tcpdf.php');

$html = '<html><body>'. '<p>Put your html here.</p>'. '</body></html>';

$tcpdf = new TCPDF('portrait', 'mm', 'A4');$tcpdf->AddPage();$tcpdf->writeHTML($html);$pdfData = $tcpdf->Output('', 'S');

Page 5: Building PHP Documents with CakePdf - CakeFest 2012

WHY IT WAS BUILT• And now with CakePdf// Using DomPdf

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

$html = '<html><body>'. '<p>Put your html here.</p>'. '</body></html>';

$cakePdf = new CakePdf(array( 'engine' => 'CakePdf.DomPdf', 'orientation' => 'portrait', 'pageSize' => 'A4'));

$pdfData = $cakePdf->output($html);

// Tcpdf example

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

$html = '<html><body>'. '<p>Put your html here.</p>'. '</body></html>';

$cakePdf = new CakePdf(array( 'engine' => 'CakePdf.Tcpdf', 'orientation' => 'portrait', 'pageSize' => 'A4'));

$pdfData = $cakePdf->output($html);

Page 6: Building PHP Documents with CakePdf - CakeFest 2012

BUILT IN RENDER ENGINESWkHtmlToPdf stable

External BinaryUses WebKit

DomPdf alpha PHP Based

Mpdf alpha PHP Based

Tcpdf alpha PHP Based

Very Nice!

Page 7: Building PHP Documents with CakePdf - CakeFest 2012

CONFIGURATION

CakePlugin::load('CakePdf', array( 'bootstrap' => true, 'routes' => true));

Add in Config/bootstrap.php

Setup

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

Needs RequestHandlerComponent

Page 8: Building PHP Documents with CakePdf - CakeFest 2012

CONFIGURATION

CakePlugin::load('CakePdf', array( 'bootstrap' => true));

Config/bootstrap.php

Special case for CakePHP 2.1.x

Router::parseExtensions('pdf');

Config/routes.php

Page 9: Building PHP Documents with CakePdf - CakeFest 2012

CONFIGURATION

// Config/bootstrap.phpConfigure::write('CakePdf', array( 'engine' => 'CakePdf.WkHtmlToPdf', 'pageSize' => 'A4', 'orientation' => 'portrait'));

Global settingsSettings

public function view($id) { $this->pdfConfig = array( 'orientation' => 'landscape', 'download' => true, 'filename' => 'invoice-2005.pdf' ); .. Rest of action logic ..}

Inside the controller

Page 10: Building PHP Documents with CakePdf - CakeFest 2012

TO VIEW OR NOT TO VIEW

• Generating PDF files with the .pdf extension in the URL• Viewing PDF documents in the browser

• Download to disk

• Smaller files

• Stand-alone to generate raw PDF data• Email attachments

• Offline processing

• Larger files

Page 11: Building PHP Documents with CakePdf - CakeFest 2012

REQUESTHANDLER FLOW

• Layout file App/View/Layout/pdf/default.ctp

• View file App/View/Orders/pdf/invoice.ctp

• All the CakePHP goodies to your disposal

• Helpers

• Blocks

• Elements

View in browser or download to disk

Page 12: Building PHP Documents with CakePdf - CakeFest 2012

STAND-ALONE FLOWApp::uses('CakePdf', 'CakePdf.Pdf');

$CakePdf = new CakePdf(array( 'engine' => 'CakePdf.Tcpdf', 'pageSize' => 'A5', 'orientation' => 'landscape', 'margin' => 10));

$html = '<html><head></head><body><p>CakeFest is the best</p></body></html>';

$rawPdfData = $CakePdf->output($html);

Page 13: Building PHP Documents with CakePdf - CakeFest 2012

ENCRYPTING PDF FILES

• Protect against viewing, printing, editing and more

• pdftk binary from PDFLabs

• 128 bit encryption

• Second pass encryption

• Encrypt existing PDF documents

Page 14: Building PHP Documents with CakePdf - CakeFest 2012

PASSWORD TYPES

• Owner password

• Unlock protected permissions

• Cannot be the same as the user password

• User password

• Will prompt before opening the PDF Document

• Cannot exist without an owner password

Page 15: Building PHP Documents with CakePdf - CakeFest 2012

CRYPTO CONFIGURATION

//Default configurationConfigure::write('CakePdf', array( 'engine' => 'CakePdf.WkHtmlToPdf', 'crypto' => 'CakePdf.Pdftk'));

Add in Config/bootstrap.php

Page 16: Building PHP Documents with CakePdf - CakeFest 2012

USING ENCRYPTING

//Action configurationpublic function view($id) { $this->pdfConfig = array( 'orientation' => 'landscape', 'protect' => true, 'userPassword' => 'foo', 'ownerPassword' => 'bar', 'permissions' => array( 'print' ) ); .. Rest of action logic ..}

Page 17: Building PHP Documents with CakePdf - CakeFest 2012

TECHNICAL DEMO


Top Related