2012-10-17 49 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