SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
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

Mais conteúdo relacionado

Último

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 

Último (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Destaque

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 2024Albert Qian
 
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 InsightsKurio // The Social Media Age(ncy)
 
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 2024Search Engine Journal
 
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 summarySpeakerHub
 
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 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 Tessa Mero
 
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 IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
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 managementMindGenius
 
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...RachelPearson36
 
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...Applitools
 
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 WorkGetSmarter
 
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...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
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 wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Destaque (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