Home » Php » php – How to integrate TCPDF?

php – How to integrate TCPDF?

Posted by: admin July 12, 2020 Leave a comment


I wanted to add TCPDF to codeigniter so i downloaded TCPDF from TCPDF Download and created a file in /libraries/Pdf.php as such

require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';

class Pdf extends TCPDF
    function __construct()

and used it as such in controller

function pdfTest(){

        $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
            // Add a page
            $html = "<h1>Test Page</h1>";
            $pdf->writeHTML($html, true, false, true, false, '');

But it gives me below error

TCPDF ERROR: Some data has already been output, can't send PDF file

What could be the reason of the error and how can I solve it? I have searched allot but couldn’t find a solution.
Thank you guys in advance.

How to&Answers:

Finally found the answer I placed ob_clean(); before $pdf->Output('test.pdf','I'); That solved the issue. Thank you all for the help.


make sure not to have whitespace before <?php and after ?>
Just use ob_start(); at the top of the page.


This error indicates that already some error or output is displayed or echoed before creating pdf. Please check if you have printed anything.