【问题标题】:php and styled html table to pdfphp和样式化的html表格到pdf
【发布时间】:2014-03-25 22:31:16
【问题描述】:

我有此代码生成样式表,但使用 php 从数据库中获取表的信息。

<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
//               info@tecnick.com
//============================================================+

/**
 * 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

这是我得到的错误:注意:未定义的索引:第 105 行 /Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php 中的表 TCPDF ERROR: 部分数据已输出,无法发送 PDF 文件。 任何意见将不胜感激。

【问题讨论】:

    标签: php html css pdf


    【解决方案1】:

    注意:未定义索引:/Users/Tika/PhpstormProjects/ooplr/classes/MYPDF.php 第 105 行中的表

    这意味着 $GLOBALS['table'] 未定义,不包含任何数据。当您将 writeHTML 行更改为 'test' 而不是 $GLOBALS['table'] 时会发生什么?

    您确定变量首先填充了表格 HTML 吗?尝试转到 mypdf.php 的顶部并编写 var_dump($GLOBALS['table']);出口;在初始

    TCPD 错误:一些数据已经输出,无法发送 PDF 文件。

    这是因为如果您已经在文档中打印了某些内容,则无法生成和输出 PDF。在这种情况下,我认为这是错误的后续,因为错误已输出到文档中,您不能再将其设为 pdf。它也可能与初始 之前的空白字符之类的愚蠢有关

    如果我正确理解情况,我认为您的问题是将表格数据从 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'] 访问这些数据。您可以像这样访问这些数据或单独访问数据:$_POST['patients'][0] ... [1] etc...

    您也可以通过相同的方式获取用户名、姓名、连接数据,或者您可以使用 ID 再次从数据库中获取数据。如果你能走到这一步,我相信你会自己解决剩下的问题。

    这是我的第一个回复,如果我没有提供帮助,请告诉我,或者如果您有任何不明白的地方,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2019-06-04
      • 2017-07-09
      相关资源
      最近更新 更多