嘗試改變:
App::import('Vendor','xtcpdf');
到
App::import('Vendor','tcpdf/xtcpdf');
或可能遵循本教程: http://www.pedroventura.com/cakephp/crear-archivos-pdf-con-cakephp/
摘要:
文件:應用程序/銷售商/ TCPDF/xtcpdf.php
<?php
App::import('Vendor','tcpdf/tcpdf');
class XTCPDF extends TCPDF
{
var $xheadertext = 'PDF creado using CakePHP y TCPDF';
var $xheadercolor = array(0,0,200);
var $xfootertext = 'Copyright © %d XXXXXXXXXXX. All rights reserved.';
var $xfooterfont = PDF_FONT_NAME_MAIN ;
var $xfooterfontsize = 8 ;
function Header()
{
list($r, $b, $g) = $this->xheadercolor;
$this->setY(10);
$this->SetFillColor($r, $b, $g);
$this->SetTextColor(0 , 0, 0);
$this->Cell(0,20, '', 0,1,'C', 1);
$this->Text(15,26,$this->xheadertext);
}
function Footer()
{
$year = date('Y');
$footertext = sprintf($this->xfootertext, $year);
$this->SetY(-20);
$this->SetTextColor(0, 0, 0);
$this->SetFont($this->xfooterfont,'',$this->xfooterfontsize);
$this->Cell(0,8, $footertext,'T',1,'C');
}
}
?>
文件:應用/視圖/佈局/ pdf.ctp在控制器
<?php
header("Content-type: application/pdf");
echo $content_for_layout;
?>
動作:
function descargar($id = null)
{
if (!$id)
{
$this->Session->setFlash('no has seleccionado ningun pdf.');
$this->redirect(array('action'=>'index'));
}
Configure::write('debug',0);
$resultado = $this->MiControlador->findById($id); // info from database
$this->set("datos_pdf",$resultado); // info to view (pdf)
$this->layout = 'pdf';
$this->render();
}
and file:app/views/mi_aplicacion/descargar.ctp
<?php
App::import('Vendor','tcpdf/xtcpdf');
$tcpdf = new XTCPDF();
$textfont = 'freesans';
$tcpdf->SetAuthor("");
$tcpdf->SetAutoPageBreak(false);
$tcpdf->setHeaderFont(array($textfont,'',10));
$tcpdf->xheadercolor = array(255,255,255);
$tcpdf->xheadertext = 'Fecha: '. date('d-m-Y',time());
$tcpdf->xfootertext = 'www.example.cl';
$tcpdf->AddPage();
$tcpdf->SetTextColor(0, 0, 0);
$tcpdf->SetFont($textfont,'B',10);
$tcpdf->Cell(10,20,'Nombre:', 0, 0);
// more info
echo $tcpdf->Output('mi_archivo.pdf', 'D'); //D or I
?>
這是不合時宜的,但TCPDF是真的老派。爲什麼你不使用whhtmltopdf - 它會生成很多所見即所得的pdf,唯一的辦法是爲你的視圖創建一個pdf佈局。這當然可以使用,如果你可以上傳你的主機上的lib的可執行文件並調用它。 http://code.google.com/p/wkhtmltopdf/ –