SlideShare a Scribd company logo
BUILDING PHP DOCUMENTS
      WITH CAKEPDF
      CakeFest Manchester, 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
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
WHY IT WAS BUILT
• Massive   differences in API between pdf libraries
// Using DomPdf                          // Tcpdf example

require_once("dompdf_config.inc.php");   require_once('config/lang/eng.php');
                                         require_once('tcpdf.php');
$html =
  '<html><body>'.                        $html =
  '<p>Put your html here.</p>'.            '<html><body>'.
  '</body></html>';                        '<p>Put your html here.</p>'.
                                           '</body></html>';
$dompdf = new DOMPDF();
$dompdf->set_paper('A4', 'landscape');   $tcpdf = new TCPDF('portrait', 'mm',
$dompdf->                                'A4');
$dompdf->load_html($html);               $tcpdf->AddPage();
$dompdf->render();                       $tcpdf->writeHTML($html);
$pdfData = $dompdf->output();            $pdfData = $tcpdf->Output('', 'S');
WHY IT WAS BUILT
• And   now with CakePdf
// Using DomPdf                        // Tcpdf example

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

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

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

$pdfData = $cakePdf->output($html);    $pdfData = $cakePdf->output($html);
BUILT IN RENDER ENGINES
                               External Binary
  WkHtmlToPdf   stable
                                Uses WebKit


    DomPdf      alpha            PHP Based



     Mpdf       alpha    Very Nice!
                               PHP Based



     Tcpdf      alpha            PHP Based
CONFIGURATION
                           Setup
Add in Config/bootstrap.php

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



Needs RequestHandlerComponent

class AppController extends Controller {
    public $components = array('RequestHandler');
}
CONFIGURATION
           Special case for CakePHP 2.1.x
Config/bootstrap.php

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




Config/routes.php

Router::parseExtensions('pdf');
CONFIGURATION
                            Settings
Global settings
// Config/bootstrap.php
Configure::write('CakePdf', array(
    'engine' => 'CakePdf.WkHtmlToPdf',
    'pageSize' => 'A4',
    'orientation' => 'portrait'
));

Inside the controller
public function view($id) {
    $this->pdfConfig = array(
        'orientation' => 'landscape',
        'download' => true,
        'filename' => 'invoice-2005.pdf'
    );
    .. Rest of action logic ..
}
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
REQUESTHANDLER FLOW
              View in browser or download to disk


• 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
STAND-ALONE FLOW
App::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);
ENCRYPTING PDF FILES

• Protect   against viewing, printing, editing and more

• pdftk   binary from PDFLabs

• 128   bit encryption

• Second    pass encryption

• Encrypt   existing PDF documents
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
CRYPTO CONFIGURATION

Add in Config/bootstrap.php

//Default configuration
Configure::write('CakePdf', array(
    'engine' => 'CakePdf.WkHtmlToPdf',
    'crypto' => 'CakePdf.Pdftk'
));
USING ENCRYPTING
//Action configuration
public function view($id) {
    $this->pdfConfig = array(
        'orientation' => 'landscape',
        'protect' => true,
        'userPassword' => 'foo',
        'ownerPassword' => 'bar',
        'permissions' => array(
            'print'
        )
    );
    .. Rest of action logic ..
}
TECHNICAL DEMO
http://github.com/ceeram/CakePdf




              THANKS


Jelle Henkens - @lemonit - jelle.henkens@gmail.com

More Related Content

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

Powerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara LaskowskaPowerful Start- the Key to Project Success, Barbara Laskowska
Powerful Start- the Key to Project Success, Barbara Laskowska
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
PLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. StartupsPLAI - Acceleration Program for Generative A.I. Startups
PLAI - Acceleration Program for Generative A.I. Startups
 
Intelligent Gimbal FINAL PAPER Engineering.pdf
Intelligent Gimbal FINAL PAPER Engineering.pdfIntelligent Gimbal FINAL PAPER Engineering.pdf
Intelligent Gimbal FINAL PAPER Engineering.pdf
 
Transforming The New York Times: Empowering Evolution through UX
Transforming The New York Times: Empowering Evolution through UXTransforming The New York Times: Empowering Evolution through UX
Transforming The New York Times: Empowering Evolution through UX
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101AI presentation and introduction - Retrieval Augmented Generation RAG 101
AI presentation and introduction - Retrieval Augmented Generation RAG 101
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Motion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in TechnologyMotion for AI: Creating Empathy in Technology
Motion for AI: Creating Empathy in Technology
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 

Featured

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Building PHP Documents with CakePdf - CakeFest 2012

  • 1. BUILDING PHP DOCUMENTS WITH CAKEPDF CakeFest Manchester, 2012
  • 2. 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
  • 3. 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
  • 4. WHY IT WAS BUILT • Massive differences in API between pdf libraries // Using DomPdf // Tcpdf example require_once("dompdf_config.inc.php"); require_once('config/lang/eng.php'); require_once('tcpdf.php'); $html = '<html><body>'. $html = '<p>Put your html here.</p>'. '<html><body>'. '</body></html>'; '<p>Put your html here.</p>'. '</body></html>'; $dompdf = new DOMPDF(); $dompdf->set_paper('A4', 'landscape'); $tcpdf = new TCPDF('portrait', 'mm', $dompdf-> 'A4'); $dompdf->load_html($html); $tcpdf->AddPage(); $dompdf->render(); $tcpdf->writeHTML($html); $pdfData = $dompdf->output(); $pdfData = $tcpdf->Output('', 'S');
  • 5. WHY IT WAS BUILT • And now with CakePdf // Using DomPdf // Tcpdf example App::uses('CakePdf', 'CakePdf.Pdf'); App::uses('CakePdf', 'CakePdf.Pdf'); $html = $html = '<html><body>'. '<html><body>'. '<p>Put your html here.</p>'. '<p>Put your html here.</p>'. '</body></html>'; '</body></html>'; $cakePdf = new CakePdf(array( $cakePdf = new CakePdf(array( 'engine' => 'CakePdf.DomPdf', 'engine' => 'CakePdf.Tcpdf', 'orientation' => 'portrait', 'orientation' => 'portrait', 'pageSize' => 'A4' 'pageSize' => 'A4' )); )); $pdfData = $cakePdf->output($html); $pdfData = $cakePdf->output($html);
  • 6. BUILT IN RENDER ENGINES External Binary WkHtmlToPdf stable Uses WebKit DomPdf alpha PHP Based Mpdf alpha Very Nice! PHP Based Tcpdf alpha PHP Based
  • 7. CONFIGURATION Setup Add in Config/bootstrap.php CakePlugin::load('CakePdf', array( 'bootstrap' => true, 'routes' => true )); Needs RequestHandlerComponent class AppController extends Controller { public $components = array('RequestHandler'); }
  • 8. CONFIGURATION Special case for CakePHP 2.1.x Config/bootstrap.php CakePlugin::load('CakePdf', array( 'bootstrap' => true )); Config/routes.php Router::parseExtensions('pdf');
  • 9. CONFIGURATION Settings Global settings // Config/bootstrap.php Configure::write('CakePdf', array( 'engine' => 'CakePdf.WkHtmlToPdf', 'pageSize' => 'A4', 'orientation' => 'portrait' )); Inside the controller public function view($id) { $this->pdfConfig = array( 'orientation' => 'landscape', 'download' => true, 'filename' => 'invoice-2005.pdf' ); .. Rest of action logic .. }
  • 10. 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
  • 11. REQUESTHANDLER FLOW View in browser or download to disk • 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
  • 12. STAND-ALONE FLOW App::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);
  • 13. ENCRYPTING PDF FILES • Protect against viewing, printing, editing and more • pdftk binary from PDFLabs • 128 bit encryption • Second pass encryption • Encrypt existing PDF documents
  • 14. 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
  • 15. CRYPTO CONFIGURATION Add in Config/bootstrap.php //Default configuration Configure::write('CakePdf', array( 'engine' => 'CakePdf.WkHtmlToPdf', 'crypto' => 'CakePdf.Pdftk' ));
  • 16. USING ENCRYPTING //Action configuration public function view($id) { $this->pdfConfig = array( 'orientation' => 'landscape', 'protect' => true, 'userPassword' => 'foo', 'ownerPassword' => 'bar', 'permissions' => array( 'print' ) ); .. Rest of action logic .. }
  • 18. http://github.com/ceeram/CakePdf THANKS Jelle Henkens - @lemonit - jelle.henkens@gmail.com