FPDF error: Some data has already been output, can

2019-01-03 03:27发布

I am using the fpdf library for my project, and I'm using this to extend one of the drupal module. These lines

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

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

I tried creating this in a separate file outside the drupal area name test.php and when viewed it worked. Anyone here know why this don't work? Or anyone here can point me a right pdf library which I can use in drupal to view HTML to PDF format.

标签: php drupal fpdf
11条回答
2楼-- · 2019-01-03 03:38

add ob_start (); at the top and at the end add ob_end_flush();

<?php
    ob_start();
    require('fpdf.php');
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    ob_end_flush(); 
?>
查看更多
戒情不戒烟
3楼-- · 2019-01-03 03:40

First step check the permissions on the folders second step put this

ob_start(); 

before the line

$pdf->Output();
查看更多
做自己的国王
4楼-- · 2019-01-03 03:44

I used the following and it worked for me

require_once ('pdf/fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output(F,'/var/www/html/PATH/filename.pdf');
ob_end_flush();
查看更多
趁早两清
5楼-- · 2019-01-03 03:51

The FPDF Error Message will point you to the PHP Line that is sending some content.

If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files.

For me

  • fpdf.php was ANSI-encoded,
  • my pdf-generator.php was UTF-8-encoded and
  • my database-connect-inlude was UTF-8-encoded (this UTF-8-encoding did raise the FPDF error. I had to switch it back to ANSI)
查看更多
何必那么认真
6楼-- · 2019-01-03 03:52

give me an error as below:
FPDF error: Some data has already been output, can't send PDF

to over come this error: go to fpdf.php in that,goto line number 996

function Output($name='', $dest='')

after that make changes like this:

function Output($name='', $dest='') {   
    ob_clean();     //Output PDF to so
查看更多
成全新的幸福
7楼-- · 2019-01-03 03:53

Try to save the file without the option: "BOM comment", i.e. in Adobe Dreamweaver, you Save File As..., uncheck the box below the filename that says, "Include Unicode signature(BOM)".

On Notepad++ you should select the menu: Encoding, "Encode in UTF-8 without BOM".

And make it default for other files you create, it will spare you a lot of headaches in future.

查看更多
登录 后发表回答