2012-10-17 46 views
2

大家好我试图创建一个发票系统,但是当我转到相关的网址我得到一个空白页面和标题只是网址。我已经按照下面的教程[http://bakery.cakephp.org/articles/kalileo/2010/06/08/creating-pdf-files-with-cakephp-and-tcpdf]cakephp 2.1和创建pdf

这里的发票控制器

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功能是我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'); 

?> 

据我所知,应该创建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 pagw

https://github.com/ceeram/CakePdf

回答

0

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

Matus Edko给你的GitHub链接的代码好多了。

顺便说一句,始终是更好地显示所有的错误和通知,当你正在开发和测试,以德“调试”设置为3

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

当使用一个Matus打印时,如何写一个视图 – user1393064

+0

您是否阅读过https://github.com/ceeram/CakePdf#usage?我正在尝试示例中的第一种方法,但我也遇到了一个奇怪的问题,没有显示任何pdf,甚至没有任何错误。如果我完成了某件事,我会让你知道 – elpeter