我一直在尝试使用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时,是否有更简单的工具可供使用?
答案 0 :(得分:0)
看来你的fetchData()函数没有返回任何内容。
如果我修改:
$obj_pdf->writeHTML($output);
阅读..
$obj_pdf->writeHTML('hello world');
然后在PDF中正确显示。这似乎可以确认您的TCPDF代码是正确的。
当我在本地运行你的代码时,我不会得到你的fetchData()函数返回的任何内容。显然我没有设置数据库,但我认为这就是你的问题所在。
在整个函数中添加一些回声,并确保返回您认为正在返回的内容。