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.
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 ?>
or
Just use ob_start();
at the top of the page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With