我已經閱讀並嘗試了多個教程,並且每次碰到一個塊時我都無法通過。來自MySQL數據庫的PHP到PDF
我有59場數據庫目前,我有一個表格填寫代理商在其銷售到每一天。每天從15-50個銷售額中獲取。目前我有一個HTML模板,其中填充了數據庫字段的變量,並通過一個循環顯示所有這些銷售在一個單獨的表中的每個銷售,然後我打印到每個銷售表在不同的頁面上。你可以想象這是每天的勞動密集程度。
我現在想給每個循環運行,這將是一個銷售到自己的PDF格式的記錄,並根據變量從數據庫表中的混合命名轉換PDF文件。
我不能讓PHP變量在PDF生成器中工作。我可以在表格中顯示數據,並從我的模板生成pdf,但只要我添加了php mysql數據庫字段變量,我總是收到關於他的變量的錯誤,並且PDF生成器失敗。
這裏是我做了什麼:
TCPDF:
<?php
$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Asimegroup');
$pdf->SetTitle('Asime Loan App');
$pdf->SetSubject('Asime Loan Application Form');
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins('5', '0', '0');
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, '0');
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) {
require_once(dirname(__FILE__).'examples\lang\eng.php');
$pdf->setLanguageArray($l);
}
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// Just including 2 rows here for example sake.
$html = '
<body>
<br/>
<table>
<tr>
<td height="40px"></td>
</tr>
<tr>
<td align="right" class="heading" width="175px">Personal</td>
<td align="left" class="heading" width="210px">Details:</td>
<td width="10px"></td>
<td align="right" class="heading" width="150px">Relative not</td>
<td align="left" class="heading" width="170px">living with you:</td>
<td width="10px"></td>
<td align="right" class="heading" width="185px">Expenses:</td>
<td width="100px"></td>
</tr>
<tr>
<td class="subheading">SA Citizen?:</td>
<td class="data"></td>
<td></td>
<td class="subheading">Name:</td>
<td class="data"></td>
<td></td>
<td class="subheading">Bond/Rent:</td>
<td class="data"></td>
</tr>
</table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('example_061.pdf', 'F');
?>
PHP數據庫循環:
<?php
// server info
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'debtdb';
// connect to the database
$conn = new mysqli($server, $user, $pass, $db);
// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
if(!isset($_POST['agentname'])) {
$search_sql="SELECT * FROM daily";
$search_query=mysqli_query($conn, $search_sql);
$search_rs=mysqli_fetch_assoc($search_query);
}
if(!empty($search_query)) {
while($search_rs = mysqli_fetch_array($search_query)) {
?>
<body>
// Just including 2 rows here for example sake.
<table>
<tr>
<td align="right" class="heading" width="175px">Personal</td>
<td align="left" class="heading" width="210px">Details:</td>
<td width="10px"></td>
<td align="right" class="heading" width="150px">Relative not</td>
<td align="left" class="heading" width="170px">living with you:</td>
<td width="10px"></td>
<td align="right" class="heading" width="185px">Expenses:</td>
<td width="100px"></td>
</tr>
<tr>
<td class="subheading">SA Citizen?:</td>
<td class="data"><?php echo $search_rs["per_citizen"]; ?></td>
<td></td>
<td class="subheading">Name:</td>
<td class="data"><?php echo $search_rs["rel_name"]; ?></td>
<td></td>
<td class="subheading">Bond/Rent:</td>
<td class="data">R <?php echo $search_rs["exp_bondrent"]; ?></td>
</tr>
</table>
<?php
}
}
?>
每所以這兩個例子的作品。以下是我卡住了,當我合併2:
<?php
// server info
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'debtdb';
// connect to the database
$conn = new mysqli($server, $user, $pass, $db);
// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
if(!isset($_POST['agentname'])) {
$search_sql="SELECT * FROM daily";
$search_query=mysqli_query($conn, $search_sql);
$search_rs=mysqli_fetch_assoc($search_query);
}
if(!empty($search_query)) {
while($search_rs = mysqli_fetch_array($search_query)) {
$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Asimegroup');
$pdf->SetTitle('Asime Loan App');
$pdf->SetSubject('Asime Loan Application Form');
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->SetMargins('5', '0', '0');
// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, '0');
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'examples\lang\eng.php')) {
require_once(dirname(__FILE__).'examples\lang\eng.php');
$pdf->setLanguageArray($l);
}
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// Just including 2 rows here for example sake.
$html = '
<table>
<tr>
<td align="right" class="heading" width="175px">Personal</td>
<td align="left" class="heading" width="210px">Details:</td>
<td width="10px"></td>
<td align="right" class="heading" width="150px">Relative not</td>
<td align="left" class="heading" width="170px">living with you:</td>
<td width="10px"></td>
<td align="right" class="heading" width="185px">Expenses:</td>
<td width="100px"></td>
</tr>
<tr>
<td class="subheading">SA Citizen?:</td>
<td class="data"><?php echo $search_rs["per_citizen"]; ?></td>
<td></td>
<td class="subheading">Name:</td>
<td class="data"><?php echo $search_rs["rel_name"]; ?></td>
<td></td>
<td class="subheading">Bond/Rent:</td>
<td class="data">R <?php echo $search_rs["exp_bondrent"]; ?></td>
</tr>
</table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('<?php echo $search_rs["agentname"]; ?> - <?php echo $search_rs["dateofsale"]; ?>.pdf', 'F');
}
}
?>
給我:
localhost的頁面無法正常工作
本地主機是目前無法處理此請求。 HTTP錯誤500
請問您可以修改問題的標題嗎? – Bikku
您試圖在mysql數據的循環內創建pdf。 '$ pdf = new ..'shoube在循環之前, – nogad
順便說一句,你純粹的html例子也是錯誤的;它可能看起來不錯,但如果你在瀏覽器中閱讀源代碼,你會看到你正在創建多個
標籤,而應該只有1. – Saskia