简体   繁体   中英

FPDF error: Some data has already been output

Whenever I try to create a PDF using FPDF, I get the following error:

FPDF error: Some data has already been output, can't send PDF file (output started at path:15).

When there is nothing but the PDF script in my file, it works, but I need to collect some data out of my database.

I've added ob_clean(); to the function Output(); , but unfortunately, it's still not working.

Below is my code:

<?php
include "connect.php";

$orderId = $_GET['OrderId'];
$findOrders = mysql_query("SELECT * FROM orderLines WHERE OrderId = $orderId ");
if (!$findOrders) { 
    die('Invalid query: ' . mysql_error());
}

while($row=mysql_fetch_array($findOrders)){
    $article = $row['article'];
    $quantity = $row['quantity'];
    $price = $row['price'];
}

require('fpdf17/fpdf.php');

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

When this error is shown, it represents that some data is being send to your browser aside of PHP file, that is, the FPDF sends the header to the file be a PDF,

header('Content-Type: application/pdf');

however you're entering some data, like a character or even a whitespace before the header specification, which leads to the error.

Two options:

1) Make require('fpdf17/fpdf.php'); on the script's beginning. So, the header cannot be changed and there'll be not an entrance before the header's setup;

2) If you say the script works without the database search, so the error may be at the connect.php file. Take a look on it to ensure it doesn't send nothing to the browser (inside <?php ?> and outside it also!

PS: FPDF already does ob_clean() when invoked.

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