gpt4 book ai didi

PHP 从 MySQL 数据库转换为 PDF

转载 作者:行者123 更新时间:2023-11-29 20:22:34 25 4
gpt4 key购买 nike

我已经阅读并尝试了多个教程,但每次遇到障碍时我都无法通过。

我目前有一个包含 59 个字段的数据库,我有一个代理商每天填写其销售额的表格。每天捕获 15-50 笔销售。目前,我有一个 HTML 模板,其中填充了数据库字段的变量,并通过循环运行它,在单独的表中显示所有这些销售,然后在单独的页面上将每个销售表打印为 pdf。可想而知,每天这是多么密集的劳动强度。

我现在正在尝试将每个循环运行(将作为销售记录)转换为自己的 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&nbsp;<?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&nbsp;<?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

最佳答案

您已经输入了这一行:

$pdf = new TCPDF('L', PDF_UNIT, 'A4', true, 'UTF-8', false);

在你的mysql while循环中,这意味着你正在为每个sql结果创建一个新的PDF,这可能不是你想要的。浏览器会提示,因为它无法处理多个 pdf 文件的请求。将需要运行一次的内容(例如创建 pdf 变量和作者内容等)放在 while 循环上方...

关于PHP 从 MySQL 数据库转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com