Home » Php » php – How to integrate TCPDF?

php – How to integrate TCPDF?

Posted by: admin July 12, 2020 Leave a comment

Questions:

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()
    {
        parent::__construct();
    }
}

and used it as such in controller

function pdfTest(){

        $this->load->library("Pdf");
        $pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
        $pdf->SetCreator(PDF_CREATOR);
            // Add a page
            $pdf->AddPage();
            $html = "<h1>Test Page</h1>";
            $pdf->writeHTML($html, true, false, true, false, '');
            $pdf->Output();
        }

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.

Answer:

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

Answer:

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