Home >Backend Development >PHP Tutorial >Why Am I Getting the 'FPDF error: Some data has already been output, can't send PDF' Error in Drupal?
In Drupal, extending modules using the FPDF library occasionally encounters an error message stating "FPDF error: Some data has already been output, can't send PDF." This error arises due to incompatible formatting.
To resolve this issue, ensure that no output occurs before utilizing FPDF. Consider the following code, which correctly avoids the error:
<?php $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, 'Hello World!'); $pdf->Output(); ?>
In contrast, this code will generate the error due to a leading space before the opening PHP tag:
<?php $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, 'Hello World!'); $pdf->Output(); ?>
Additionally, any non-FPDF output, such as an echo statement, will cause the error:
<?php echo "About to create pdf"; $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial', 'B', 16); $pdf->Cell(40, 10, 'Hello World!'); $pdf->Output(); ?>
Remember, for FPDF to function correctly, it is imperative that zero non-FPDF output precedes its use.
The above is the detailed content of Why Am I Getting the 'FPDF error: Some data has already been output, can't send PDF' Error in Drupal?. For more information, please follow other related articles on the PHP Chinese website!