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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

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

The error message "FPDF error: Some data has already been output, can't send PDF" is one of the most frustrating issues when generating documents in a web application. It appears deceptively simple, but resolving it requires understanding how HTTP requests and headers interact. The core issue is not with your FPDF logic itself, but with the state of the HTTP response buffer before you attempt to force an output redirect.

When you instantiate new FPDF() and call $pdf->Output(), the library attempts to send a header that tells the browser to download the file immediately. This header typically looks like Content-Type: application/pdf. However, this operation fails if any data has been written to the output buffer prior to that moment. In the context of Drupal or any PHP framework, "data" doesn't just mean visible HTML; it includes whitespace characters, BOMs (Byte Order Marks), or even invisible newlines at the top of your file.

Consider the scenario where you have a working standalone script named test.php. When you run that file directly in a browser, it works perfectly because no framework has initialized yet, and no hidden output is generated. The moment you move that same logic into a Drupal module, the environment changes significantly. Drupal's bootstrap process often includes rendering views, loading assets, or including debug messages before your specific code executes. Even if your function seems to be called early in the page lifecycle, any preceding echo, print, or whitespace in the file can corrupt the header generation.

To fix this, you must ensure that the output buffer is empty before calling $pdf->Output(). You can achieve this by flushing the output buffer or disabling buffering entirely for the response. However, simply adding ob_end_clean() isn't always sufficient if no buffer exists yet; it might throw a warning. A more robust approach in PHP is to use ob_implicit_flush(true) combined with ob_start() if you need to capture content later, but for PDF generation, direct flushing is often clearer:

// Ensure no output has occurred
if (function_exists('ob_end_clean')) {
    ob_end_clean();
} elseif (function_exists('ob_flush')) {
    ob_flush();
}

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

Even with this guard clause, the underlying cause often persists in Drupal modules. It is extremely common for developers to accidentally include a carriage return or newline at the very beginning of their PHP file, or for the framework to inject debugging output that gets captured by ob_get_contents(). If your module's main file (module_name.module) has any whitespace before the opening <?php tag, or if a preceding function in your code prints debug info, the header will be corrupted.

If you need to generate PDFs from HTML content within Drupal, sticking strictly to FPDF might become cumbersome because it requires manual parsing of HTML strings into text and layout commands. In such cases, libraries like mPDF or wkhtmltopdf are often better suited, as they handle complex HTML-to-PDF conversion more gracefully. However, switching libraries does not automatically solve the "output already sent" error; you still must manage the HTTP headers correctly regardless of the tool used.

For modern PHP applications built on Laravel, handling this is slightly different due to the framework's built-in buffering management. In a standard Laravel controller, you would typically return a Response object with the PDF content and set the correct header before sending it, ensuring no view rendering occurs in between:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

public function downloadPdf(Request $request) {
    $pdf = new FPDF();
    // ... populate PDF ...
    
    $content = $pdf->Output('S'); // 'S' returns content as string
    
    return Response::make($content, 200, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'attachment; filename="report.pdf"'
    ]);
}

This approach avoids the binary Output() method that forces an immediate redirect, giving you more control over headers. If you are extending a Drupal module, ensure your custom function is called only after the request has been fully prepared and before any rendering logic runs. Always inspect the top of your file for invisible characters and verify that no debug statements are echoing data before the PDF generation block.

Our network