Ralf Hohoff

fpdf in CakePHP

Nach http://bakery.cakephp.org/articles/sdevore_myopenid_com/2007/01/27/pdf-helper-using-fpdf sollte es gar nicht so schwierig sein, fpdf in cakephp zu nutzen.
Ist es aber doch ein bischen…

Umgesetzt auf cakephp 1.2.x

// Dateiname: \views\helpers\pdf.php
< ?php   
//vendor('fpdf/fpdf.php');  
//App::import('Vendor', 'Fpdf', array('file' => 'fdpf'.DS.'fpdf.php'));
App::import('Vendor', 'Fpdf', array('file' => 'fdpf/fpdf.php'));
 
if (!defined('PARAGRAPH_STRING')) define('PARAGRAPH_STRING', '~~~');
 
class pdfHelper extends FPDF {
 
	// to avoid warnings ...
 
	var $helpers = array(); 
 
	// Variablen
	var $fontCustomer = "";
	var $title = "";

Mehr Informationen zu den Warnings.

// Dateiname: \vendors\fdpf\fpdf.php
// ORIGINAL: function FPDF($orientation='P', $unit='mm', $format='A4')
// Anpassung laut http://cakephp.1045679.n5.nabble.com/FPDF-in-cakephp-1-2-td1298891.html
// Ansonsten Fehlermeldungen
function FPDF($w, $orientation='P', $unit='mm', $format='A4')
// Dateiname: \controllers\presses_controller.php
 
function displaypdf($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Press.', true));
			$this -> render('blank');
			//$this->redirect(array('action'=>'index'));
		}
		$this->set('press', $this->Press->read(null, $id));
 
        $this->layout = 'pdf'; //this will use the pdf.ctp layout in views/layouts
        //$this->set('data','hello world!');
        //$this->render();
    }
// Dateiname: C:\xampp\htdocs\cake\hoecker\views\layouts\pdf.ctp
< ?php  
header("Content-type: application/pdf");  
echo $content_for_layout;  
?>

Es wird das cakephp-Modell „Press“ benutzt.

// Dateiname: C:\xampp\htdocs\cake\hoecker\views\presses\displaypdf.ctp
< ?php 
	$id = $press['Press']['id']; // zur späteren Nutzung
 
	// Explizites Initiieren 
	$pdf->setup(null, 'Portrait','mm','A4');
 
	$pdf->AddFont('franklin', '', 'franklinbook.php'); // Standard Hausschrift
	$pdf->fontCustomer = 'franklin';
 
	$pdf->title='Cool !!'; //zwingend bevor die erste Seite erzeugt wird! 
 
	$pdf->FirmaName = 'Meine Firma GmbH';
 
	$pdf->AddPage(); 
 
	$pdf->SetFont('franklin','',24);
 
	//debug($press);
    $pdf->Cell(40,10,$press['Press']['name'],0,1);
	$pdf->Cell(40,10,$press['Press']['id'],0,1); 
 
    echo $pdf->fpdfOutput();
?>

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert