问题描述
实际上我是 TCPDF 的新手.谁能帮我将数据显示为 .pdf
文件.我正在使用以下代码,数据显示为一般网页.我想以 .pdf
格式显示它.
Actually I'm new to TCPDF. Can anyone help me to display the data as a .pdf
file. I am using the following code and the data is being displayed as a general webpage. I want to display it in .pdf
format.
require_once('../config/lang/eng.php')
require_once('../tcpdf.php')
error_reporting(E_ALL) ; ini_set('display_errors', '1');
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ef_kabaadkhana");
$result = mysql_query("SELECT form_id,partner_name FROM ef_form_master_v1");
if (($result))
{
echo "<table width='100%'><tr>";
if (mysql_num_rows($result)>0)
{
$i = 0;
while ($i < mysql_num_fields($result))
{
echo "<th>". mysql_field_name($result, $i) . "</th>";
$i++;
}
echo "</tr>";
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
}else{
echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";
}
echo "</table>";
}else{
echo "Error in running query :". mysql_error();
}
推荐答案
您也可以这样做:
就在 if (($result)) 之前插入
Right before if (($result)) insert
ob_start();
在 php 脚本的末尾,插入
At the end of your php script, insert
$html = ob_get_contents();
ob_end_clean();
它的作用是开始捕获输出缓冲区并抓取您在屏幕上回显的所有内容,然后将其存储在 $html 变量中.
What this does is start output buffer capturing and grabs everything you echo to the screen and then stores it in $html variable.
然后您只需将 $html 变量传递给 tcpdf 中的 writeHTML() 函数.我相信您可以查找 tcpdf 的文档以了解基本的 pdf 创建示例.另外,我偏爱 mPDF.我认为它对样式的支持要好得多
You then simply pass $html variable to writeHTML() function in tcpdf. I am sure you can look up tcpdf's documentation for basic pdf creation example. Also, I am partial to mPDF. I think it has much better support for styling
这篇关于使用带有 TCPDF 的 PHP 从数据库中检索数据(使用 mysql)并将其显示为 .pdf 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!