Anyone experienced with TCPDF?


I am using TCPDF to create an application which generates PDF brochures dynamically from information stored in a MySQL database and using an HTML table as its basis.

I have created a PDF generation script with a hard coded HTML header and footer but I want the central part of the table based HTML to come from an include…ie Details are pulled from database and PHP writes the HTML file dynamically

In TCPDF I need to pull in the written HTML as an include.

If I hard code the HTML, everything works fine but if I try to pull the data in, the code is displayed, NOT the result of its execution.

$htmlmid = include(‘filename.html’);

In the hard code version the syntax would be $htmlmid = “html code”;

However, even if I ensure that all quotes are in the correct places and the HTML is well formed, the HTML code itself is displayed, not the table cells.

I have also tried file_get-contents to pull in the code but the result is the same.

Is there a way I can pull the code in and get TCPDF to display its results in the final PDF?


I just built an app doing the exact same thing. Here are a few things to keep in mind:

  1. Your HTML must be perfect else TCPDF will balk and likely stop displaying code around where your errors are. Clean up the HTML to a bare minimum, and don’t rely on CSS.

  2. I had to put my content into an HTML table to make all of the sections appear in the correct place on the page. DIV’s and positioning are very hit or miss.

  3. The secret to accomplishing your include dilemma is using output buffering. I kept the HTML for the different sections of my PDF in multiple files to be more manageable. Each time you dump the output buffer into a variable, then write that HTML. You then clear the buffer and start a new one, include a new file, and repeat to write the next page. Check the code below.

error_reporting(0); //Don't show errors in the PDF
ob_clean(); //Clear any previous output
ob_start(); //Start new output buffer

all of your PDF configuration here...

//Write page 1

$page1 = ob_get_contents(); //Dump output buffer to variable
ob_clean(); //Clear output buffer
$page1 = preg_replace("/\\s\\s+/", '', $page1); //Strip excess whitespace
$pdf->writeHTML($page1, true, 0, true, 0); //Write page 1

Thank you so much for that.

This has been driving me nuts for the past week, it worked like a charm.

Thanks and regards


Thanks alot it’s worked…

I’m relieve…

Thanks bro :slight_smile: