Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php和样式化html表转换为pdf_Php_Html_Css_Pdf - Fatal编程技术网

php和样式化html表转换为pdf

php和样式化html表转换为pdf,php,html,css,pdf,Php,Html,Css,Pdf,我有这段代码生成一个样式表,但使用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>

我有这段代码生成一个样式表,但使用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'];
?>

Pt。用户名 Pt。数字 全名 加上
我试图抓住这个表,并提供给用户下载为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中的表

这意味着$GLOBALS['table']未定义,不保存任何数据。当您将writeHTML行更改为类似“test”而不是$GLOBALS['table']时会发生什么情况

您确定变量首先是用HTML表填充的吗?尝试转到mypdf.php的顶部,编写var_dump($GLOBALS['table']);出口在初始<?php标记之后。它输出什么吗

TCPDF错误:某些数据已输出,无法发送PDF文件

这是因为如果您已经在文档中打印了某些内容,则无法生成和输出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)。由于我在name字段中使用了[],php将识别许多可以使用此名称的字段,并将结果设置为数组。在mypdf.php中,您现在可以通过神奇变量$u POST['patients']访问这些数据。您可以访问此数据或单独访问数据,如下所示:$\u POST['patients'][0]。。。[1] 等等

您也可以通过同样的方法获取用户名、名称和加入的数据,也可以使用ID再次从数据库中获取数据。如果你走到这一步,我相信你会自己解决剩下的问题

这是我的第一个回复,所以如果我没有帮助你,请告诉我,或者如果你有什么不明白的,请告诉我