提问者:小点点

CakePHP 2.1和创建pdf


大家好,我正在尝试创建一个发票系统,但是当我转到相关的url时,我得到一个空白页面,标题就是url。我遵循了以下教程[http://bakery.cakephp.org/articles/kalileo/2010/06/08/creating-pdf-files-with-cakephp-and-tcpdf]

这是发票控制器中的我的viewPdf函数

    function viewPdf($id = null) 
    { 
        if (!$id) 
        { 
            $this->Session->setFlash('Sorry, there was no property ID submitted.'); 
            $this->redirect(array('action'=>'index_admin'), null, true); 
        } 
        Configure::write('debug',0); // Otherwise we cannot use this method while developing 

        $id = intval($id); 

        $property = $this->__view($id); // here the data is pulled from the database and set for the view 

        if (empty($property)) 
        { 
            $this->Session->setFlash('Sorry, there is no property with the submitted ID.'); 
            $this->redirect(array('action'=>'index'), null, true); 
        } 

        $this->layout = 'pdf'; //this will use the pdf.ctp layout 
        $this->render(); 
    } 

//End of Controller
}

这是我的视图PDF视图

<?php 
App::import('Vendor','xtcpdf');  
$tcpdf = new XTCPDF(); 
$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans' 
$fpdf->xheadertext = 'YOUR ORGANIZATION';
$tcpdf->SetAuthor("KBS Homes & Properties at http://kbs-properties.com"); 
$tcpdf->SetAutoPageBreak( false ); 
$tcpdf->setHeaderFont(array($textfont,'',40)); 
$tcpdf->xheadercolor = array(150,0,0); 
$tcpdf->xheadertext = 'KBS Homes & Properties'; 
$tcpdf->xfootertext = 'Copyright © %d KBS Homes & Properties. All rights reserved.'; 

// add a page (required with recent versions of tcpdf) 
$tcpdf->AddPage(); 

// Now you position and print your page content 
// example:  
$tcpdf->SetTextColor(0, 0, 0); 
$tcpdf->SetFont($textfont,'B',20); 
$tcpdf->Cell(0,14, "Hello World", 0,1,'L'); 
// ... 
// etc. 
// see the TCPDF examples  

echo $tcpdf->Output('filename.pdf', 'D'); 

?>

据我所知,它应该创建一个pdf文件,里面写着“hello world”。

在遵循关于过时的建议之后,我使用了github的链接,并将其保存在我的控制器中

 public function view($id = null) {
    $this->set('title_for_layout', 'Invoices');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.png');
    $this->layout='home_layout';
            $this->Invoice->id = $id;
            if (!$this->Invoice->exists()) {
                throw new NotFoundException(__('Invalid invoice'));
            }
            $this->pdfConfig = array(
                'orientation' => 'potrait',
                'filename' => 'Invoice_' . $id
            );

            $this->set('invoice', $this->Invoice->read(null, $id));

        }
    }

还有我的view.ctp

<html>
<head></head>
<title></title>
<body>
<?php $this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf') ?>
<?php echo $invoice; ?>
</body>
</html>

它打印出数组,但不将其呈现为pdf文件

这是github页面的链接

https://github.com/ceeram/CakePdf


共2个答案

匿名用户

使用此插件,安装程序位于github页面;)

https://github.com/ceeram/CakePdf

匿名用户

这是一个过时的教程,这是CakePHP1.2的教程,您正在使用2.1。

这比Matus Edko给你的GitHub链接中的代码要好得多。

顺便说一句,最好在开发和测试时显示所有错误和通知,方法是将de“debug”设置为3。

Configure::write('debug',3);