2014-03-06 78 views
0

我有這段代碼生成一個樣式表,但使用PHP從數據庫中獲取表的信息。PHP和風格的HTML表格到PDF

<form name="pt_list" action="classes/MYPDF.php" method="post"><br/> 
<input type="submit" name="pdf" value="Download as PDF"> 
    </form> 

<?php 
    ob_start(); 
?> 
<table id=patients> 
    <tr> 
     <th>Pt. username</th> 
     <th>Pt. number</th> 
     <th>Full Name</th> 
     <th>Added on</th> 
    </tr> 


    <?php $x=1; 
    foreach ($users as $patient) { 

    ?> <tr <?php if ($x % 2 == 0) {echo "class='alt'"; } ?>> 
     <td> <a href="profile.php?username=<?php echo $patient['username'];?>"><?php echo $patient['username'];?></a></td> 
     <td> <?php echo $patient['id'];?></td> 
     <td> <?php echo $patient['name'];?></td> 
     <td> <?php echo $patient['joined'];?></td> 
    </tr> 

    <?php 
     $x++; 
     } ?> 
</table> 
<?php 
    $GLOBALS['table'] = ob_get_clean(); 
$table = $GLOBALS['table']; 
?> 

我想抓住這張表,並提供給用戶下載爲PDF。我試過tcpdf和fpdf,但總是不斷地發送pdf,輸出已經發送。這裏是我的MYPDF.php代碼:

<?php 
//============================================================+ 
// File name : example_003.php 
// Begin  : 2008-03-04 
// Last Update : 2013-05-14 
// 
// Description : Example 003 for TCPDF class 
//    Custom Header and Footer 
// 
// Author: Nicola Asuni 
// 
// (c) Copyright: 
//    Nicola Asuni 
//    Tecnick.com LTD 
//    www.tecnick.com 
//    [email protected] 
//============================================================+ 

/** 
* Creates an example PDF TEST document using TCPDF 
* @package com.tecnick.tcpdf 
* @abstract TCPDF - Example: Custom Header and Footer 
* @author Nicola Asuni 
* @since 2008-03-04 
*/ 

// Include the main TCPDF library (search for installation path). 
require_once('../tcpdf/tcpdf.php'); 


// Extend the TCPDF class to create custom Header and Footer 
class MYPDF extends TCPDF { 

    //Page header 
    public function Header() { 
     // Logo 
     $image_file = K_PATH_IMAGES.'logo_example.jpg'; 
     $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false); 
     // Set font 
     $this->SetFont('helvetica', 'B', 20); 
     // Title 
     $this->Cell(0, 15, '<< TCPDF Example 003 >>', 0, false, 'C', 0, '', 0, false, 'M', 'M'); 
    } 

    // Page footer 
    public function Footer() { 
     // Position at 15 mm from bottom 
     $this->SetY(-15); 
     // Set font 
     $this->SetFont('helvetica', 'I', 8); 
     // Page number 
     $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M'); 
    } 
} 

// create new PDF document 
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); 

// set document information 
$pdf->SetCreator(PDF_CREATOR); 
$pdf->SetAuthor('Nicola Asuni'); 
$pdf->SetTitle('TCPDF Example 003'); 
$pdf->SetSubject('TCPDF Tutorial'); 
$pdf->SetKeywords('TCPDF, PDF, example, test, guide'); 

// set default header data 
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); 

// set header and footer fonts 
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); 
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); 

// set default monospaced font 
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

// set margins 
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); 
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER); 
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER); 

// set auto page breaks 
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); 

// set image scale factor 
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

// set some language-dependent strings (optional) 
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) { 
    require_once(dirname(__FILE__).'/lang/eng.php'); 
    $pdf->setLanguageArray($l); 
} 

// --------------------------------------------------------- 

// set font 
$pdf->SetFont('times', 'BI', 12); 

// add a page 
$pdf->AddPage(); 

// set some text to print 

// print a block of text using Write() 
$pdf->writeHTML($GLOBALS['table'], true, false, true, false, ''); 

// --------------------------------------------------------- 

//Close and output PDF document 
$pdf->Output('example_003.pdf', 'D'); 

//============================================================+ 
// END OF FILE 

這是我的錯誤:注意:未定義指數:表中/Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php線105 TCPDF ERROR :有些數據已經輸出,不能發送PDF文件。 任何輸入將不勝感激。

回答

0

Notice: Undefined index: table in /Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php on line 105

這意味着$ GLOBALS ['table']是未定義的,不包含任何數據。當您將writeHTML行更改爲'test'而不是$ GLOBALS ['table']時會發生什麼?

您確定該變量是首先填充表格HTML嗎?嘗試進入mypdf.php的頂部並寫入var_dump($ GLOBALS ['table']);出口;之後最初的<?php標籤。它輸出任何東西嗎?

TCPDF ERROR: Some data has already been output, can't send PDF file.

這是由於這樣的事實,如果你已經在文檔中打印出來的東西,你不能生成和輸出PDF。在這種情況下,我認爲這是錯誤後續,因爲錯誤已經輸出到文檔中,您不能再將其作爲pdf。在最初的<?php之前,它可能也需要做一些像空白字符一樣愚蠢的事情。

如果我理解正確的情況,我認爲你的問題是將表格數據從html文件移動到MYPDF.php。如果是這樣的話,我會考慮的替代方案:

<form name="pt_list" action="classes/MYPDF.php" method="post"><br/> 
<?php foreach ($users as $patient) { 
    echo '<input type="hidden" name="patients[]" value="'.$patient['id'].'">'; 
} ?> 
<input type="submit" name="pdf" value="Download as PDF"> 
</form> 

這會給你一個隱藏的輸入字段爲每一個病人,並讓您的數據發出到窗體目標(在這種情況下,mypdf。 PHP)。由於我在名稱字段中使用[],因此php會識別許多字段可以使用這個名稱,並將結果作爲一個數組。在mypdf.php中,您現在可以通過魔術變量$ _POST ['patients']來獲取這些數據。您可以foreach這些數據或單獨到達數據,如下所示:$ _POST ['patients'] [0] ... [1] etc ...

您可以這樣做來獲取用戶名,數據也可以通過,或者您可以使用該ID再次從數據庫中獲取數據。如果你得到這麼多,我相信你會自己找出其餘的。

這是我的第一反應,請讓我知道如果我沒有幫助,或者讓我知道,如果有什麼你不明白。