2

我只是尝试使用 mPDF 按数据库制作输出表 pdf,但仍然很难做到这一点。

<?php
$html = '

<center><h3>TITLE</h3></center>
<center>
<table border="1">
<tr>
    <th>COLUMN 1</th><th>COLUMN 2</th>
</tr>
<tr>
        <!--how to fetch this row from DB? -->
    <td>.$row[no1].</td><td>.$row[no2].</td>
</tr>
</table></center>

';
//==============================================================
//==============================================================
//==============================================================
include("../mpdf.php");
    include "conn.php";

    $res = mysql_query("select * from list");
    if (!$res)
        die("query error : ".mysql_error());
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
while($row = mysql_fetch_array($res))
    if (!$res)
        die("error fetch array : ".mysql_error());
$mpdf->WriteHTML($html,2);
$mpdf->Output('mpdf.pdf','I');
exit;
//==============================================================
//==============================================================
//==============================================================
?>

当我运行此脚本时,它可以生成 PDF,但行与数据库不匹配?

4

1 回答 1

5

像这样的东西:

<?php

//==============================================================
//==============================================================
//==============================================================
include("../mpdf.php");
include "conn.php";

$res = mysql_query("select * from list");
if (!$res)
    die("query error : ".mysql_error());
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$html = '

<center><h3>TITLE</h3></center>
<center>
<table border="1">
<tr>
<th>COLUMN 1</th><th>COLUMN 2</th>
</tr>
<tr>';

while($row = mysql_fetch_array($res)){
$html .= '<td>'.$row['no1'].'</td><td>' . $row['no2']. '</td>';
}
$html .= '</tr>
</table></center>
';
$mpdf->WriteHTML($html,2); 
$mpdf->Output('mpdf.pdf','I');
exit;
//==============================================================
//==============================================================
//==============================================================
?>
于 2013-01-04T15:41:59.100 回答