gpt4 book ai didi

php - 使用 TCPDF 从服务器中提取动态数据并显示为 PDF

转载 作者:行者123 更新时间:2023-11-28 02:38:52 25 4
gpt4 key购买 nike

我一直在尝试使用 PHP 创建一个可以下载的动态 PDF 文件。我已经包含了下面的代码。文档显示空白数据,PDF 文件上没有任何内容。

<?php
$output = '';
$quote = $_GET['quote'];
function fetchData(){
include 'config.php';
$quote = $_GET['quote'];
$output = '';
$query = "SELECT * FROM quote_list, quotation WHERE quote_list.quote_id = quotation.id AND quote_list.quote_id = '$quote'";
$result = mysqli_query($connect, $query);

if($row = mysqli_fetch_assoc($result)){

$companyName = $row['name'];
$email = $row['email'];

$countQuery = "SELECT COUNT(quotation.id) AS countRow FROM quotation WHERE company_name = '$companyName'";
$countRes = mysqli_query($connect, $countQuery);

while($countRow = mysqli_fetch_assoc($countRes)){
$count = $countRow['countRow'];
}

$quoteNum = $companyName . ' - ' .$count;

$sqlDate = $row['date'];
$date = strtotime($sqlDate);
$date = date('j F Y', $date);

$output .= "
<div class='row'>
<div class='col'>
<img class='logo' src='img/logo.jpg'>
</div>
</div>

<div class='row margin'>
<div class='col'>
<strong><h3>To:" . $companyName . "</h3>
Email:". $email."</strong>
</div>
</div>

<div class='row margin'>
<div class='col'>Quotation Number:". $quoteNum ."</div>
</div>

<div class='row'>
<div class='col'>Issue Date:". $date ."</div>
</div>

<div class='row margin'>
<div class='col'>
<p>Further to your enquiry, please find attached our quotation based on your discussed requirements. If you have any other queries, amendments or your requirements change please do not hesitate to contact me.</p>
<p>Thank you for the opportunity to quote on this project.</p>
</div>
</div>

<div class='row margin'>
<div class='col'>
<h1>Your quotation:</h1>
<table class='table table-bordered table-striped table-condensed table-responsive'>
<thead>
<tr>
<th>
Quote Details
</th>
<th class='float-right'>
Amount Quoted
</th>
</tr>
</thead>
<tbody>";

$query = "SELECT * FROM quote_list, quotation WHERE quote_list.quote_id = quotation.id AND quote_list.quote_id = '$quote'";
$result = mysqli_query($connect, $query);
$total = 0;
$discount = 0;

while($row = mysqli_fetch_assoc($result)){
$title = $row['title'];
$price = $row['price'];
$discount = $row['discount'];
$total += $price;

$ouput .=
"
<tr>
<td>".
$title."
</td>
<td class='float-right'>
£".$price.
"
</td>
</tr>
";
}

$final = $total-$discount;



$output .= "
<tr>
<td></td>
<td class='float-right'><em><strong>Total: </strong></em>£".$total."</br><em><strong>Discount: </strong></em>£".$discount."</br><em><strong>Quotation Price: </strong></em>£".$final."</td>
</tr>
</tbody>
</table>
</div>
</div>

<div class='row margin'>
<div class='col'>
<h1>Terms and Conditions</h1>
<h2>Our Quotation</h2>
<p>The above costs are inclusive of VAT, which will be charged where applicable. NI Flyers Print & Design quotations are valid for 30 days. Design costs are based on our understanding of your initial brief. Any additional amends/change to brief will be advised prior to invoicing. The right of design creations remains exclusively with NI Flyers Print & Design including economic and moral rights. Acceptance of this quotation includes a non-exclusive licence for the use of the works outlined above. An exclusive licence may be obtained at an additional cost.</p>

<h2>Additional photographic or illustration-based imagery</h2>
<p>Sourced photography or illustration this will be an additional cost to the design process. Imagery can be sourced either from stock library at £30.00 per image, or commissioned at cost, which will always be pre-quoted. If attendance to manage or art direct photoshoots is required this will be based on our standard hourly rate of £50 per hour. Additional photoshoot props or services will be charged accordingly.</p>

</div>
</div>
";
}
return $output;
}

if(isset($_GET['viewPDF'])){
require_once('tcpdf/tcpdf.php');
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");
$obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);
$obj_pdf->setPrintHeader(false);
$obj_pdf->setPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE, 10);
$obj_pdf->SetFont('helvetica', '', 11);
$obj_pdf->AddPage();
$output .= fetchData();
$obj_pdf->writeHTML($output);
$obj_pdf->Output('Quote_' . $quote . '.pdf', 'I');
}
?>

我在制作这份文件时是否做错了什么?我正在关注这方面的教程和文档,但似乎根本无法加载它。由于某种原因总是一个空白的 PDF 文档。通过 PHP 动态创建 PDF 时,是否可以使用更简单的工具?

最佳答案

您的 fetchData() 函数似乎没有返回任何内容。

如果我修改:

$obj_pdf->writeHTML($output); 

阅读..

$obj_pdf->writeHTML('hello world');  

然后这会正确显示在 PDF 中。这似乎可以确认您的 TCPDF 代码是正确的。

当我在本地运行您的代码时,您的 fetchData() 函数没有返回任何内容。显然我没有你的数据库设置,但我认为这就是你的问题所在。

尝试在整个函数中添加一些 echo ,并确保返回您认为要返回的内容。

关于php - 使用 TCPDF 从服务器中提取动态数据并显示为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275937/

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