2

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

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Good idea on the output buffer. Lets you leave the code intact if you like with echos and not stuffing it all into a variable.
the content is not being displayed. i am getting ablank .pdf file. please help me.
@rohit, are you able to get any pdf working. Meaning if you do "$html='Hello';" and get tcpdf to create it, do you see the results. The reason I am asking is that I have both Adobe and Nitro pdf. Sometimes my generated pdf files show up blank in my Nitro. If you get "Hello" to print, then it's something with the script. If not, it might be an issue with your setup.
@rohit, also you have this line: echo "<tr><td colspan='" . ($i+1) . "'>No Results found!</td></tr>"; in the else statement for mysql_num_rows. $i is not defined at that point and you don't need it. Get rid of the colspan altogether. It might be that your query gives you no result, you then go to ELSE block and pdf fails on bad html
@AlexeyGerasimov, Actually my query is working properly without pdf, and even by using pdf it's showing few data improperly..
|
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.

Comments

1

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

7 Comments

I have added a code example using your code as a starting point.
Thanks but I'm still facing errors. Such as:Notice: Undefined index: trids in C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php on line 23722
Invalid argument supplied for foreach() in C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php on line 23722
Undefined index: trids in C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php on line 23751
Undefined index: trids in C:\Users\Rohit\Documents\NetBeansProjects\exmp\tcpdf.php on line 23754
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.