如何使用tcpdf将mysql表内容输出到pdf?

时间:2012-02-12 14:21:36

标签: php mysql tcpdf

我需要创建pdf文档,它输出mysql表中的所有行,但是在下面附带的代码中,它只输出该表中的最后一行。有人有时间检查我的代码并建议我更正,或者有人为此类报告提供更好的代码吗?

3 个答案:

答案 0 :(得分:3)

我的问题的答案已通过以下链接正确回答:TCPDF - printing table from mysql showing repeated first row

代码在这里:

答案 1 :(得分:0)

一种方法:

the while loop
(
  if($number_of_rows > 0) { 

    while($row = mysql_fetch_assoc($result)) 
    { 
        $RN = $row['rnBr']; 
        $Tvrtka = $row['formTvrtka']; 
        $Poslovnica = $row['formPoslovnica']; 
        $Datum = $row['formDatum'];
        $Izvrsio = $row['formIzvrsio'];
        $Kontakt = $row['formKontakt'];
        $Napomene = $row['formNapomene'];
        $Izdavatelj = $row['formIzdavatelj']; 
     } 
  }
)

需要降低这个位置:

<tr>
    <td>$RN</td> 
    <td>$Datum</td>
    <td>$Izvrsio</td> 
    <td>$Tvrtka</td>
    <td>$Poslovnica</td>
    <td>$Napomene</td>
    <td>$Izdavatelj</td>
</tr>

所以你会得到像

这样的东西
if($number_of_rows > 0) { 

   while($row = mysql_fetch_assoc($result)) 
   { 
       echo '<tr>';
       echo '<td>'. $row['rnBr'].'</td>'; 
       echo '<td>'. $row['formTvrtka'].'</td>'; 
       echo '<td>'. $row['formPoslovnica'].'</td>';  
       echo '<td>'. $row['formDatum'].'</td>'; 
       echo '<td>'.  $row['formIzvrsio'].'</td>'; 
       echo '<td>'. $row['formKontakt'].'</td>'; 
       echo '<td>'. $row['formNapomene'].'</td>'; 
       echo '<td>'. $row['formIzdavatelj'].'</td>';  
    } 
 }

否则是,您只输出查询找到的最后一行。

或者更改每一行,如:

$RN = $row['rnBr']; to $RN[$n] = $row['rnBr']; (with $n++ at the end of your loop and $n=0; before the loop

然后在输出时可以执行以下操作:

for($u=0;$u<count($rn);$u++){
    <tr>
        <td>$RN[$u]</td> 
        <td>$Datum[$u]</td>
        <td>$Izvrsio[$u]</td> 
        <td>$Tvrtka[$u]</td>
        <td>$Poslovnica[$u]</td>
        <td>$Napomene[$u]</td>
        <td>$Izdavatelj[$u]</td>
    </tr>
}

答案 2 :(得分:0)

$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
$rnBr = mysql_real_escape_string(trim($_POST['rnBr']));  
$zahtjev = $_GET['rnBr'];
$upit = "SELECT * FROM radni where formDatum >= CURDATE() and formDatum < (CURDATE()+  interval 7 day) order by formDatum asc";
$result = mysql_query($upit);

require_once('../config/lang/hrv.php');
require_once('../tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('XXXXX');
$pdf->SetTitle('TCPDF Radni nalog');
$pdf->SetSubject('TCPDF Radni nalog');
$pdf->SetKeywords('TCPDF, PDF, radni, nalog, ispis');

//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('dejavusans', '', 14, '', true);
// add a page
$pdf->AddPage();

$tbl = <<<EOD
<table border=""  cellpadding="0" cellspacing="7" align="center" fontsize="14">
<tr>
<td></td><td><br /><br /><br /><br /></td>
</tr>
<tr>
<th colspan="2"><h1>Tjedni raspored djelatnika</h1></th>
</tr>
</table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, ''); 
$pdf->Ln();
$pdf->SetLineStyle(array('width' => 0.0, 'cap' => 'butt', 'join' => 'miter', 'dash' => 4, 'color' => array(255, 0, 0)));
$pdf->SetFillColor(255,255,128);
$pdf->SetTextColor(0,0,128);

$pdf->Ln();

$tbl = <<<EOD
<table border="1"  cellpadding="0" cellspacing="3" align="center" fontsize="12">
<tr>
<th colspan="1">Radni nalog:</th>
<th colspan="1">Datum:</th>
<th colspan="1">Izvršitelj:</th>
<th colspan="1">Tvrtka:</th>
<th colspan="1">Poslovnica:</th>
<th colspan="1">Napomena:</th>
<th colspan="1">RN izdao:</th>
</tr>
</table>
EOD;

$pdf->writeHTML($tbl, true, false, false, false, '');

$pdf->Ln();

$tbl = '<table border="1"  cellpadding="0" cellspacing="3" align="center" fontsize="10">';
 while($row = mysql_fetch_assoc($result)) {

  $tbl .="<tr>
<td>{$row['rnBr']}</td> 
<td>{$row['formDatum']}</td>
<td>{$row['formIzvrsio']}</td> 
<td>{$row['formTvrtka']}</td>
<td>{$row['formPoslovnica']}</td>
<td>{$row['formNapomene']}</td>
<td>{$row['formIzdavatelj']}</td>
</tr>";
 }

$tbl = '</table>';

$pdf->writeHTML($tbl, true, false, false, false, '');

// Set some content to print
$html = <<<EOD
<i>Ovaj popis generiran je računalnim programom!</i>
EOD;
$pdf->writeHTMLCell($w='', $h='', $x='', $y='', $html, $border=1, $ln=1, $fill=1, $reseth=true, $align='center', $autopadding=true);

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