[英]generating pdf using tcpdf library
Iam trying to generate a pdf from database data using tcpdf library but unfortunately it seems not to be working out.我正在尝试使用 tcpdf 库从数据库数据生成 pdf,但不幸的是它似乎没有解决。 I dont know where Iam going wrong .
我不知道我哪里出错了。 Someone kindly help.
有人好心帮助。 here is the code that I intend to output data in the table to the pdf.
这是我打算将表中的数据输出到pdf的代码。 The code is also generating a table which is working perfectly but when it comes to the pdf it is bringing a blank image as shown in the text.
该代码还生成了一个完美运行的表格,但是当涉及到 pdf 时,它会带来一个空白图像,如文本所示。 I wish to understand where Iam going wrong.
我想了解我哪里出错了。
<?php
ob_start();
ini_set('display errors', 1);
error_reporting('0');
session_start();
if (!isset($_SESSION['name'])) {
header('Location:login.php');
} elseif (isset($_SESSION['name'])) {
$cuser = $_SESSION['name'];
}
require 'logis.php';
if(isset($_POST["generate_pdf"]))
{
//get data into pdf
function fetch_data()
{
$year = $_POST['year'];
$month = $_POST['month'];
$output = '';
// $output.='<tr><td>Youuuuuuu<td></tr>';
include 'includes/config.php';
$sql = "SELECT DISTINCT item_name FROM SALES WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'";
$query = mysqli_query($conn, $sql);
$sales = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($sales as $sale) {
$item = $sale['item_name'];
$sql1 = "SELECT SUM(total_price) AS totalsum,SUM(quantity_sold) AS totalsold FROM sales WHERE item_name='$item' AND MONTH(date_sold)='$month' AND YEAR(date_sold)='$year' ";
$result1 = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result1);
$sumsold = $row['totalsold'];
$sumprice = $row['totalsum'];
$output .= '<tr><td>' . $item . '</td>
<td>' . $sumsold . '</td>
<td>' . $sumprice . '</td>
</tr>';
}
$resultsum = mysqli_query($conn, "SELECT SUM(total_price) AS totalsum FROM sales WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'");
$rowsum = mysqli_fetch_assoc($resultsum);
$sum = $rowsum['totalsum'];
$output .= '<tr><td colspan="2" style="text-align:center;">Total Amount: </td>
<td colspan="1" style="text-decoration:bold;color:blue;">' . $sum . '</td></tr>';
echo $output;
return $output;
}
require_once('includes/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 monthly summary");
$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();
$content = '';
$content .= '
<h4 align="center">MONTHLY SUMMARY </h4><br />
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="40%">Item Name</th>
<th width="30%">Amount Sold(Kgs)</th>
<th width="30%">Total Price(Kshs)</th>
</tr>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
ob_end_clean();
$obj_pdf->Output('summary.pdf', 'I');
}
Why you use ob_start()
?为什么使用
ob_start()
? May this is causing the error.可能这是导致错误的原因。 In the code below, i deleted the
ob function calls
and the echo of the $output
in the fetch_data()
function.在下面的代码中,我删除了
ob function calls
和fetch_data()
函数中$output
的回显。 Please try this out and show errors if appear.请尝试此操作并在出现错误时显示。 If this does not work, please show how you execute the table only with right results.
如果这不起作用,请展示您如何仅以正确的结果执行表格。
error_reporting('E_ALL');
session_start();
if (!isset($_SESSION['name'])) {
header('Location: login.php');
} elseif (isset($_SESSION['name'])) {
$cuser = $_SESSION['name'];
}
require 'logis.php';
if(isset($_POST["generate_pdf"]))
{
//get data into pdf
function fetch_data()
{
$year = $_POST['year'];
$month = $_POST['month'];
$output = '';
include 'includes/config.php';
$sql = "SELECT DISTINCT item_name FROM SALES WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'";
$query = mysqli_query($conn, $sql);
$sales = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($sales as $sale) {
$item = $sale['item_name'];
$sql1 = "SELECT SUM(total_price) AS totalsum,SUM(quantity_sold) AS totalsold FROM sales WHERE item_name='$item' AND MONTH(date_sold)='$month' AND YEAR(date_sold)='$year' ";
$result1 = mysqli_query($conn, $sql1);
$row = mysqli_fetch_assoc($result1);
$sumsold = $row['totalsold'];
$sumprice = $row['totalsum'];
$output .= '<tr><td>' . $item . '</td><td>' . $sumsold . '</td><td>' . $sumprice . '</td></tr>';
}
$resultsum = mysqli_query($conn, "SELECT SUM(total_price) AS totalsum FROM sales WHERE MONTH(date_sold)='$month' AND YEAR(date_sold)='$year'");
$rowsum = mysqli_fetch_assoc($resultsum);
$sum = $rowsum['totalsum'];
$output .= '<tr><td colspan="2" style="text-align:center;">Total Amount: </td><td colspan="1" style="text-decoration:bold;color:blue;">' . $sum . '</td></tr>';
return $output;
}
require_once('includes/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 monthly summary");
$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();
$content = '';
$content .= '
<h4 align="center">MONTHLY SUMMARY </h4><br />
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th width="40%">Item Name</th>
<th width="30%">Amount Sold(Kgs)</th>
<th width="30%">Total Price(Kshs)</th>
</tr>
';
$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output('summary.pdf', 'I');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.