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

18,687

Solution 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

Solution 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.

Solution 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();  
}
Share:
18,687
rohit
Author by

rohit

Updated on June 17, 2022

Comments

  • rohit
    rohit almost 2 years

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