0

I want to create a PDF from a form, but im stuck. It says "Some data has already been output" and I know it's the $name. But any idea how I can solve this problem?

  <?php
    ob_end_clean();
    if(!empty($_POST['submit']))
    {
        $name= $_POST['name'];
        $date = $_POST['date'];
        $movie = $_POST['movie'];
    }

        require_once ('fpdf/fpdf.php');
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,$name);
        $pdf->Output();
        ob_end_flush(); 
    ?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user9554749
  • 53
  • 1
  • 5
  • Could you please paste in the full error message as it appears on the screen? – Dharman Jan 06 '19 at 18:20
  • Possible duplicate of [FPDF error: Some data has already been output, can't send PDF](https://stackoverflow.com/questions/9475686/fpdf-error-some-data-has-already-been-output-cant-send-pdf) – Dharman Jan 06 '19 at 18:21
  • @Dharman Notice: Undefined variable: name in C:\xampp\htdocs\movie\form.php on line 15 FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\movie\form.ph – user9554749 Jan 07 '19 at 09:02

1 Answers1

0
  1. There can't be any text output to page. Because of undefined variable there is this text:

    Notice: Undefined variable: name in C:\xampp\htdocs\movie\form.php on line 15

  2. Because of that error text, there is problem with exporting PDF.

You have two options.

Option 1: Just put your FPDF code into if clause. This way it will be defined.

<?php
        ob_end_clean();
        if(!empty($_POST['submit'] && !empty(&_POST['name'])
        {
            $name= $_POST['name'];
            $date = $_POST['date'];
            $movie = $_POST['movie'];
        
            require_once ('fpdf/fpdf.php');
            $pdf = new FPDF();
            $pdf->AddPage();
            $pdf->SetFont('Arial','B',16);
            $pdf->Cell(40,10,$name);
            $pdf->Output();
            ob_end_flush(); 
        }
     ?>

Option 2: Set value for $name, if not submited.

  <?php
    ob_end_clean();
    if(!empty($_POST['submit'])
    {
        $name= $_POST['name'];
        $date = $_POST['date'];
        $movie = $_POST['movie'];
    } else {
        $name= '';
    }
        require_once ('fpdf/fpdf.php');
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,$name);
        $pdf->Output();
        ob_end_flush(); 
 ?>
lemax
  • 39
  • 5