Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP和TCPDF从数据库中检索数据(使用mysql)并将其显示为.pdf文件_Php_Tcpdf - Fatal编程技术网

使用PHP和TCPDF从数据库中检索数据(使用mysql)并将其显示为.pdf文件

使用PHP和TCPDF从数据库中检索数据(使用mysql)并将其显示为.pdf文件,php,tcpdf,Php,Tcpdf,事实上,我是TCPDF新手。有人能帮我把数据显示为.pdf文件吗。我使用以下代码,数据显示为通用网页。我想以.pdf格式显示它 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)

事实上,我是TCPDF新手。有人能帮我把数据显示为
.pdf
文件吗。我使用以下代码,数据显示为通用网页。我想以
.pdf
格式显示它

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();  
}  
require_一次('../config/lang/eng.php')
需要一次('../tcpdf.php')
错误报告(E_全部);ini_集('display_errors','1');
$con=mysql_connect(“本地主机”、“根目录”、“根目录”);
如果(!$con)
{  
die('无法连接:'.mysql_error());
}   
mysql_select_db(“ef_kabaadkhana”);
$result=mysql\u query(“从ef\u form\u master\u v1中选择表单id、合作伙伴名称”);
如果($result))
{  
回声“;
如果(mysql_num_rows($result)>0)
{  
$i=0;
而($i
更改所有echo语句,将HTML标记放在变量中。然后使用TCPDF中的writeHTML函数将该标记输出到PDF。请注意,tcpdf并不能很好地处理所有标记。像您这样基于表格的布局通常工作得很好,但我发现每行中的所有td单元通常都需要显式的宽度设置才能正常工作

编辑:

以下是为writeHTML重新编写的代码:

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))  
{
    $html = '';  
    $html .= "<table width='100%'><tr>";  
    if (mysql_num_rows($result)>0)    
    {  

        $i = 0;  
        while ($i < mysql_num_fields($result))  
        {  
            $html .= "<th>". mysql_field_name($result, $i) . "</th>";  
            $i++;  
        }  
        $html .= "</tr>";  
        while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))  
        {  
            $html .= "<tr>";  
            foreach ($rows as $data)  
            {  
                $html .= "<td align='center'>". $data . "</td>";  
            }  
        }  
    }else{  
        $html .= "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>";  
    }
    $html .= "</table>";
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    // Set various pdf options
    $pdf->SetAuthor('John Doe');
    // etc.
    // Now output the html
    $pdf->AddPage();
    $pdf->writeHTML($html, true, 0);
    // Output the PDF to the browser
    $pdf->Output('somefile.pdf', 'D'); // the second option D forces the browser to download the PDF. Passing I will tell the browser to show it inline.
}else{  
    echo "Error in running query :". mysql_error();  
}
require_一次('../config/lang/eng.php')
需要一次('../tcpdf.php')
错误报告(E_全部);ini_集('display_errors','1');
$con=mysql_connect(“本地主机”、“根目录”、“根目录”);
如果(!$con)
{  
die('无法连接:'.mysql_error());
}   
mysql_select_db(“ef_kabaadkhana”);
$result=mysql\u query(“从ef\u form\u master\u v1中选择表单id、合作伙伴名称”);
如果($result))
{
$html='';
$html.=”;
如果(mysql_num_rows($result)>0)
{  
$i=0;
而($iSetAuthor('johndoe');
//等等。
//现在输出html
$pdf->AddPage();
$pdf->writeHTML($html,true,0);
//将PDF输出到浏览器
$pdf->Output('somefile.pdf','D');//第二个选项D强制浏览器下载pdf。通过此选项,我将告诉浏览器以内联方式显示它。
}否则{
echo“运行查询时出错:”.mysql_Error();
}
您也可以这样做:

就在if(($result))插入之前

在php脚本的末尾,插入

$html = ob_get_contents();
ob_end_clean();
这样做的目的是启动输出缓冲区捕获,并获取您回显到屏幕上的所有内容,然后将其存储在$html变量中


然后,只需将$html变量传递给tcpdf中的writeHTML()函数。我相信您可以查阅tcpdf文档,了解基本的pdf创建示例。此外,我偏爱强积金。我认为它对样式设计有更好的支持

看看官方网站上的演示

您只需将所有“echo”替换为以下变量:

echo "<table width='100%'><tr>";  //old
$html_text .= "<table width='100%'><tr>"; //new
记住,之前不应该有输出(打印、回音等)


因为您将看到错误。

关于输出缓冲区的好主意。如果您喜欢echos,可以让代码保持原样,而不将其全部填充到变量中。不显示内容。我正在获取ablank.pdf文件。请帮帮我。@rohit,你能让任何pdf工作吗。也就是说,如果您执行“$html='Hello';”并让tcpdf创建它,您会看到结果吗。我问的原因是我有Adobe和Nitro pdf。有时我生成的pdf文件在我的Nitro中显示为空白。如果要打印“Hello”,那么它与脚本有关。如果没有,可能是您的设置有问题。@rohit,您还有一行:echo“找不到结果!”;在mysql\u num\u行的else语句中$我在那一点上没有定义,你也不需要它。把科尔斯潘彻底除掉。可能是您的查询没有结果,然后转到ELSE块,pdf在bad上失败html@AlexeyGerasimov,实际上,我的查询在没有pdf的情况下运行正常,即使使用pdf,也很少显示不正确的数据。我添加了一个代码示例,使用您的代码作为起点。谢谢,但我仍然面临错误。例如:注意:未定义索引:为foreach()提供的第23722Invalid参数中的C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php中的trid在23722行的C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php中,在23751行的C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php中,未定义索引:在23754行的C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php中,未定义索引:trids
echo "<table width='100%'><tr>";  //old
$html_text .= "<table width='100%'><tr>"; //new
$html = $html_text;
$pdf->Output('example_006.pdf', 'I');