2014-03-06 153 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再次从数据库中获取数据。如果你得到这么多,我相信你会自己找出其余的。

这是我的第一反应,请让我知道如果我没有帮助,或者让我知道,如果有什么你不明白。