【问题标题】:cakephp 2.1 and creating pdfcakephp 2.1 和创建 pdf
【发布时间】:2012-10-17 02:07:01
【问题描述】:

大家好,我正在尝试创建一个发票系统,但是当我转到相关的 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
}

这是我的 viewPdf 视图

<?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'); 

?>

据我所知,它应该创建一个包含“hello world”字样的 pdf 文件。

在遵循有关过时的建议后,我使用了指向 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));

        }
    }

还有我的观点.ctp

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

它会打印出 Array 但不会将其呈现为 pdf 文件

这里是github pagw的链接

https://github.com/ceeram/CakePdf

【问题讨论】:

    标签: cakephp pdf


    【解决方案1】:

    使用这个插件,设置在github页面上;)

    https://github.com/ceeram/CakePdf

    【讨论】:

    • 我正在尝试安装这个并且对配置有疑问,你知道我应该和谁说话或问谁吗?
    【解决方案2】:

    这是一个过时的教程,这是一个 Cakephp 1.2 的教程,你正在使用 2.1。

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

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

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

    【讨论】:

    • 在使用Matus打印的时候,怎么写视图
    • 你读过github.com/ceeram/CakePdf#usage??我正在尝试示例中的第一种方法,但我也遇到了一个奇怪的问题,没有显示没有 pdf 甚至没有错误。如果我完成了某事,我会通知你
    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 2012-04-21
    • 2012-07-25
    • 1970-01-01
    相关资源
    最近更新 更多