我已经阅读并尝试了多个教程,每次遇到障碍都无法通过。
我目前有一个包含59个字段的数据库,我有一个表单代理每天填写销售。每天从15-50销售额被捕获。目前我有一个HTML模板,其中我填充了数据库字段的变量,我通过循环显示所有这些销售在一个单独的表中,然后我打印到pdf每个销售表在一个单独的页面上。你可以想象每天都是如此劳动密集型。
我现在正在尝试将每个循环运行转换为自己的pdf,并根据数据库表中的变量混合命名pdf文件。
我无法在PDF生成器中使用php变量。我可以在表格中显示数据,并从我的模板中生成pdf,但是一旦我添加了php mysql数据库字段变量,我就会一直收到有关变量的错误,并且pdf gereator会失败。
这就是我所做的:
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页面无效
localhost目前无法处理此请求。 HTTP ERROR 500
答案 0 :(得分:0)
你已经把这一行:
$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);
在你的mysql while循环中,这意味着你要为每个sql结果创建一个新的PDF,这可能不是你想要的。浏览器会抱怨因为它无法处理多个pdf文件的请求。把需要运行一次的东西放在你的while循环之上,比如在你的while循环上面创建pdf变量和作者的东西......
答案 1 :(得分:0)
我经常发现生成PDF最简单的方法就是生成HTML(您已经做过的),然后将其提供给像wkhtml2pdf这样的工具,将其转换为PDF。
你唯一需要添加的是一些CSS,使每个表都出现在新页面上;
table {
page-break-after: always;
}
现在您可以将URL传递给命令;
wkhtml2pdf http://example.com/reports.php reports.pdf
首先将其保存到临时文件中,然后将其传递给命令。
答案 2 :(得分:0)
感谢大家的建议和帮助。我现在能够使用wkhtmltopdf将整个表输出到一个pdf中。
我现在需要为我的第二个解决方案修改此代码,以便将页面分隔成单独的文件。我现在在stackexchange上找到了这个部分,看起来几乎是正确的,只需要能够将它与我的mysql数据库集成:
PHP TCPDF Create Multiple PDFS With One Command
require_once('eng.php');
require_once('tcpdf.php');
$pageLayout = array(750, 800);
$content=Array(
'<html><body>Document A</body></html>',
'<html><body>Document B</body></html>',
'<html><body>Document C</body></html>'
);
foreach($content as $i=>$html){
$pdf =new TCPDF('P', 'pt', $pageLayout, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('filename_' . $i . '.pdf', 'D');
}
我需要生成每个都带有唯一名称的pdf文件,而不是在浏览器中显示它们。
我需要举例来做这样的事情:
$pdf->Output('$clientname-$clientsurname-$dateosfale.pdf', 'F');
然后,我可以根据相同的模式生成链接,并将它们显示在表格中,以便能够下载。
如果我错了,请纠正我,这是我的php循环和tcpdf生成代码的基本结构。
我想这样做。当我使用wkhtmltopdf一次生成所有内容时,我需要能够对每个页面执行相同操作,因此我在循环内添加了与pdf生成相关的所有内容,以便为每个循环生成pdf。但它并不那么容易因为我注意到了:
<?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)) {
require_once('tcpdf_include.php');
// create new PDF document
$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();
$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>
<tr>
<td class="subheading">Name:</td>
<td class="data"><?php echo $search_rs["per_firstname"]; ?></td>
<td></td>
<td class="subheading">Surname:</td>
<td class="data"><?php echo $search_rs["rel_surn"]; ?></td>
<td></td>
<td class="subheading">Rates, Water, Electricity:</td>
<td class="data">R <?php echo $search_rs["exp_rates"]; ?></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["per_firstname"]; ?>-<?php echo $search_rs["per_lastname"]; ?>-<?php echo $search_rs["dateofsale"]; ?>.pdf, 'F');
}
}
?>