Using PHP with TCPDF to retrieve data(using mysql) from a database and show it as a .pdf file

一曲冷凌霜 提交于 2019-12-07 21:00:03

问题


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();  
}  

回答1:


You can also do this:

Right before if (($result)) insert

ob_start();

At the end of your php script, insert

$html = ob_get_contents();
ob_end_clean();

What this does is start output buffer capturing and grabs everything you echo to the screen and then stores it in $html variable.

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




回答2:


Take a look on demos from official site. http://www.tcpdf.org/examples.php

You just need too replace all 'echo' to some variable like this :

echo "<table width='100%'><tr>";  //old
$html_text .= "<table width='100%'><tr>"; //new

And after that just equate to TCPDF $html variable

$html = $html_text;

Remeber, there should be no output (print, echo etc) before

$pdf->Output('example_006.pdf', 'I');

Because You will see error.




回答3:


Change all your echo statements to instead put your HTML markup in a variable. Then use the writeHTML function in TCPDF to output that markup to PDF. Be aware that tcpdf doesn't handle all markup very well. Table based layouts like yours usually work pretty well but I have found that all you td cells in every row usually need explicit width settings to work properly.

EDIT:

Here is your code reworked for 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();  
}


来源:https://stackoverflow.com/questions/8153305/using-php-with-tcpdf-to-retrieve-datausing-mysql-from-a-database-and-show-it-a

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!