Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

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

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

Short 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.

  • Applies to FPDF 1.86, PHP 8.3, PHP 8.4, also applies to TCPDF and mPDF
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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();
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2headers_sent() points at a line with echo, print, an HTML block or a debug dumpYour own code prints something before Output()Fix
    3. 3The 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

What does the error look like?

  • FPDF error: Some data has already been output, can't send PDF file
  • Some 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 3

2Fix

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.php

3Verify

headers_sent() no longer reports that file.

php -l some-included-file.php

Confirmed by fpdf.org

Did this fix it?

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.php

2Fix

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.pdf

Confirmed by php.net

Did this fix it?

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 . | head

2Fix

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 8

Confirmed by fpdf.org

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.