0

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();
}
?>
2
Contributors
4
Replies
17
Views
3 Years
Discussion Span
Last Post by lloydsbackyard
0

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>

Edited by broj1

0

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?

1

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.

Edited by broj1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.