FPDF stops with: Some data has already been output, can't send PDF file
FPDF error: Some data has already been output, can't send PDF fileShort answer
A PDF response must contain nothing but the PDF. FPDF raises this error when anything, even one space, went to the browser before Output(). Find where output began with headers_sent(), or read the (output started at file:line) part of the message, then remove the stray text, blank or echo. Output buffering is the last resort.
Fix it fast
1Find out which cause you have
Ask PHP where output began, right before you call Output(). The file and line it prints are the culprit.
<?php if (headers_sent($file, $line)) { exit("Output started in $file on line $line"); } $pdf->Output();2Match what you see, then jump to its fix
- 1
The message ends with (output started at some-included-file.php:N), and N is the last line of that fileBlank space after the closing tag of an included fileFix - 2
headers_sent() points at a line with echo, print, an HTML block or a debug dumpYour own code prints something before Output()Fix - 3
The location in the message is not in your file; it is inside a theme, a template or another layerA framework or CMS wrote output before your PDF code runsFix
- 1
What does the error look like?
FPDF error: Some data has already been output, can't send PDF fileSome data has already been output, can't send PDF file (output started at script.php:12)
Cause 1 of 3
Blank space after the closing tag of an included file
1Check
The FPDF FAQ says you must send nothing to the browser except the PDF, no HTML, no space, no carriage return, and calls out a blank at the end of an included script as a common case.
# last bytes of the file named in the message
tail -c 20 some-included-file.php | od -c | tail -n 32Fix
Remove the blank lines or spaces that follow the last closing tag, so the file ends right after ?> or after its last PHP line.
# strip trailing blank lines from the file (GNU sed)
sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' some-included-file.php3Verify
headers_sent() no longer reports that file.
php -l some-included-file.phpConfirmed by fpdf.org
Cause 2 of 3
Your own code prints something before Output()
1Check
Any echo, var_dump, HTML outside the PHP tags, or printed warning counts as output. headers_sent() puts the file name and line where output started into the two variables.
grep -n "echo \|print \|var_dump\|print_r" generate-pdf.php2Fix
Remove or move that output so the script sends only the PDF. Do not print anything on the same request, including debug lines.
// before
echo "Generating...";
$pdf->Output();
// after
$pdf->Output();3Verify
The PDF opens in the browser.
curl -sS -o out.pdf 'http://localhost/generate-pdf.php' && head -c 5 out.pdfConfirmed by php.net
Cause 3 of 3
A framework or CMS wrote output before your PDF code runs
1Check
In a page already controlled by a CMS or framework, something earlier in the request may have printed. The FAQ's advice for finding it is to add ob_end_clean() at the very beginning of the script.
grep -rn "ob_start" --include=*.php . | head2Fix
Run the PDF code from its own entry point, before the theme prints anything. If you cannot, call ob_end_clean() at the very beginning of the script, which the FAQ suggests for this case.
ob_end_clean(); // at the very start of the PDF script, as the FAQ suggests
$pdf->Output('D', 'report.pdf');3Verify
The browser gets a download that opens as a PDF.
curl -sS -D - -o /dev/null 'http://localhost/generate-pdf.php' | head -n 8Confirmed by fpdf.org
How do I stop it coming back?
- Keep blank lines and spaces out of included files, especially after the last closing tag.
- Make the PDF script the only thing that runs on that request, with no HTML around it.
- Log debug output to a file, not to the response.
- Call headers_sent() while debugging, since it names the file and line.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Warning: Cannot modify header information - headers already sent
Warning: session_start(): Session cannot be started after headers have already been sent
PHP sent output to the browser before your header(), setcookie() or session_start() call, and headers must go first.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix