Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF error: Some data has already been output, can't send PDF

Tags:

php

drupal

fpdf

I am using the fpdf library for my project, and I'm using this to extend one of the drupal module. These lines

$pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); 

give me an error: FPDF error: Some data has already been output, can't send PDF

I tried creating this in a separate file outside the drupal area name test.php and when viewed it worked. Anyone here know why this don't work? Or anyone here can point me a right pdf library which I can use in drupal to view HTML to PDF format.

like image 558
Wondering Coder Avatar asked Feb 28 '12 02:02

Wondering Coder


People also ask

How to fix TCPDF error some data has already been output can t send pdf file?

Solution. The first and most common solution, is to search on your code what is the line or code that is generating some output before TCPDF and remove it (mentioned methods as print_r, var_dump, echo etc).

What is Fpdf error?

The FPDF Error Message will point you to the PHP Line that is sending some content. If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.

What does TCPDF error mean?

Any error stating 'TCPDF' means that there was a problem with the generation of the PDF file.


2 Answers

For fpdf to work properly, there cannot be any output at all beside what fpdf generates. For example, this will work:

<?php $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?> 

While this will not (note the leading space before the opening <? tag)

 <?php $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?> 

Also, this will not work either (the echo will break it):

<?php echo "About to create pdf"; $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(); ?> 

I'm not sure about the drupal side of things, but I know that absolutely zero non-fpdf output is a requirement for fpdf to work.

like image 114
Gordon Bailey Avatar answered Sep 21 '22 08:09

Gordon Bailey


add ob_start (); at the top and at the end add ob_end_flush();

<?php     ob_start();     require('fpdf.php');     $pdf = new FPDF();     $pdf->AddPage();     $pdf->SetFont('Arial','B',16);     $pdf->Cell(40,10,'Hello World!');     $pdf->Output();     ob_end_flush();  ?> 
like image 34
Behlum Noman Avatar answered Sep 21 '22 08:09

Behlum Noman