Created
June 30, 2011 05:48
-
-
Save abbajbryant/1055702 to your computer and use it in GitHub Desktop.
Quick and dirty implementation for importing and extending the fpdf.php class using a custom vendor and helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Place this in the same folder as the fpdf.php referenced in the App::import below with the filename report_pdf.php | |
App::import( 'Vendor', 'FPDF', array( | |
'file' => join( DS, array( 'fpdf', 'fpdf.php' )), | |
)); | |
* ReportPdf | |
* an example fpdf extension for Dominik Gajewski re: google groups topic FPFD or PDFLib with Cake | |
* this is where you would implement all the custom functions, ie header, footer, columns. | |
* @author Abba Bryant | |
*/ | |
class ReportPdf extends FPDF | |
{ | |
/** | |
* Construction | |
* @param string orientation - P, L, Portrait, Landscape | |
* @param string unit - pt, mm, cm, in | |
* @param mixed size - A3, A4, A5, Letter, Legal, array( x, y ) | |
* @access public | |
*/ | |
public function __construct( $orientation, $unit, $size ){ | |
parent::__construct( $orientation, $unit, $size ); | |
} | |
/** | |
* Custom Header function | |
* @access public | |
*/ | |
public function header( ){ | |
} | |
/** | |
* Custom Footer function | |
* @access public | |
*/ | |
public function footer( ){ | |
} | |
} | |
?> | |
<?php | |
// Place this in your app/views/helpers folder with the filename report.php | |
App::import( 'Vendor', 'ReportPdf', array( | |
'file' => join( DS, array( 'fpdf', 'report_pdf.php' )), | |
)); | |
/** | |
* ReportHelper | |
* an example helper for Dominik Gajewski re: google groups topic FPFD or PDFLib with Cake | |
* @author Abba Bryant | |
*/ | |
class ReportHelper extends AppHelper | |
{ | |
/** | |
* Settings | |
* @var array | |
* @access private | |
*/ | |
private $settings = array( | |
'orientation' => 'Portrait', // Portrait, Landscape, P, L | |
'unit' => 'mm', // pt, cm, mm, in | |
'size' => 'A4', // A3, A4, A5, Letter, Legal | |
); | |
/** | |
* Pdf Object | |
* @var mixed | |
* @access public | |
*/ | |
public $Pdf = null; | |
/** | |
* Construction | |
* @param array $settings | |
* @return null | |
* @access public | |
*/ | |
public function __construct( $settings = array( )){ | |
$this->settings = Set::merge( | |
$this->settings, | |
$settings | |
); | |
// instantiate our fpdf object | |
$this->Pdf = new ReportPdf( $this->settings[ 'orientation' ], $this->settings[ 'unit' ], $this->settings[ 'size' ] ); | |
} | |
/** | |
* output | |
* Control how the pdf is returned to the user | |
* @param string $name - filename generated | |
* @param string $destination - where to send the document I, D, F, S | |
* @return mixed - string if $destination = S | |
* @access public | |
*/ | |
public function output( $name = 'page.pdf', $destination = 'S' ){ | |
return $this->Pdf->Output( $name, $destination ); | |
} | |
} | |
?> | |
<?php | |
// Place this in app/views/tests/test_pdf.ctp | |
$this->Report->Pdf->AddPage( ); | |
$this->Report->Pdf->SetFont( 'Arial', 'B', 16 ); | |
$this->Report->Pdf->Cell( 40, 10, $data ); | |
echo $this->Report->output( ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No promises it works - I wrote it after 10 minutes of fpdf docs and using a recent(ish) custom helper I had lying around.