Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate TCPDF?

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.

like image 763
Cryptos Avatar asked Feb 13 '15 12:02

Cryptos


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

like image 65
Cryptos Avatar answered Oct 15 '22 23:10

Cryptos


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

like image 22
SO-user Avatar answered Oct 16 '22 00:10

SO-user