will anybody please help me with this code..since it will give me this error:
FPDF error: Some data has already been output, can't send PDF file...

my php code below:

<form method="POST" action="">
    <input type="submit" name="print"value="PRINT">
</form>
<?php
if (isset($_POST['print']))
{
        require('fpdf/fpdf.php');
        class PDF extends FPDF
        {
        // Page header
            function Header()
            {
                $this->SetFont('Arial','B',14);
                $this->Cell(30,10,'Negros Oriental High School','C');
                $this->Ln();
                $this->SetFont('Arial','',12);
                $this->Cell(0,0,'Dumaguete City');
                $this->Ln();
                $this->SetFont('Arial','',14);
                $this->Cell(0,20,'OFFICIAL RECEIPT');
                $this->Ln();
            }


        }
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Output();
}
?>

Recommended Answers

All 4 Replies

The simplest solution: move the html part (the form) to the end. This way no HTML will be output before the pdf.

<?php
if (isset($_POST['print']))
{
        require('fpdf/fpdf.php');
        class PDF extends FPDF
        {
        // Page header
            function Header()
            {
                $this->SetFont('Arial','B',14);
                $this->Cell(30,10,'Negros Oriental High School','C');
                $this->Ln();
                $this->SetFont('Arial','',12);
                $this->Cell(0,0,'Dumaguete City');
                $this->Ln();
                $this->SetFont('Arial','',14);
                $this->Cell(0,20,'OFFICIAL RECEIPT');
                $this->Ln();
            }


        }
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Output();
}
?>
<form method="POST" action="">
    <input type="submit" name="print"value="PRINT">
</form>

thank you... what if ill put it in a separate php file..like this..

main.php

<?php
if (isset($_POST['pay']))
{
    //sql codes here..

    if ($result)
    {
       include 'receipt.php';
    }


 <form method="POST" action="">
 <input type="text" name="cash">
<input type="submit" name="print"value="PRINT">
</form>  
?>

receipt.php

<?php
require('fpdf/fpdf.php');
class PDF extends FPDF
{
// Page header
function Header()
{
$this->SetFont('Arial','B',14);
$this->Cell(30,10,'Negros Oriental High School','C');
$this->Ln();
$this->SetFont('Arial','',12);
$this->Cell(0,0,'Dumaguete City');
$this->Ln();
$this->SetFont('Arial','',14);
$this->Cell(0,20,'OFFICIAL RECEIPT');
$this->Ln();
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Output();
}
?>

will this work?

It will, when you correct a couple of mistakes:

  1. the ?> end tag goes before the <form> tag
  2. the if (isset($_POST['pay'])) should be changed to if (isset($_POST['print']))

I would (personaly) put the whole form processing in another file and cahnge the action attribute of the form to point to that file. Just my approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.