简体   繁体   中英

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

well I get this error trying to use php class FPDF:

FPDF error: Some data has already been output, can't send PDF file (output started at
/mnt/webc/e1/12/5691512/htdocs/adminpanel/fpdf/test.php:1)

My test.php

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

There are no blanks or anything... So where is the error???

Some editors adds BOM at the start of a file.

View your file in hex and remove it

it is becouse of two reason

1-there is no leading space before the opening 2-are some data has been output before

The solution of 2 reson is go to fpdf.inc.php

and find this funciton

function Output($name='', $dest='') {   

and then in the defination of this function write this at starting

ob_clean();  

this will clean the previous output.

For me, it was the deprecation warnings. I added & ~E_DEPRECATED to my error_reporting in my php.ini and rebooted Apache.

I got same problem in Live Server only. But its working on Local Machine. Adds BOM at the start of a file also its not working on Server. But working in local Machine. Some changes need in your Live code.

1. Place the ob_start() in first line of your file.

Example:

ob_start();    
$DOCROOTPATH = $_SERVER['DOCUMENT_ROOT'];   
$DOCROOTBASEPATH = dirname($_SERVER['DOCUMENT_ROOT']);    
include_once($DOCROOTPATH."/lib/commonarray.inc");    
include_once($DOCROOTPATH."/includes/class.payslip.php");    
include_once($_SERVER['DOCUMENT_ROOT']."/menucontrol.php");    

define('FPDF_FONTPATH','fpdf/font/');    
require('fpdf/fpdf.php');    

2. After the $pdf->Output(),you should place ob_end_flush() in Same File .

Example:

$pdf=new PDF();     
$pdf->Open();     
$pdf->AliasNbPages();     
$pdf->AddPage();     
$pdf->SetFont('Times','',12);     
$pdf->pdfAllPages();      
$pdf->Output();      
ob_end_flush();     

Now go to hit the browser and see the pdf which you want.

(OR) Some editors adds BOM at the start of a file.

View your file in hex and remove it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM