Search code examples
pdffpdf

Passing variables to fpdf


I have a problem with passing variables to fpdf. First script is getting post text sent to filtering class, the class is returning filtered POST-s as a 2 element array. First script looks like this:

include('service.php');
include('pdf.php');

    $pdf_filter = new Pdf_filter;
    $filter = $pdf_filter->pdfFilter();
    var_dump($filter);
    extract($filter);

I'm extracting $filter array to get variables from it (filtering script is creating variables of the POST that are sent and I can echo them so I don't know if this is even necessary).

The second script looks like this:

require('E:\Xampp\php\fpdf181\fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(195,5, $tytul, 0,1,'C');
$pdf->Cell(195,5, $petycja, 0,1,'C');
$pdf->Output();

and I'm getting this error:

Notice: Undefined variable: tytul in E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php on line 10

Notice: Undefined variable: petycja in E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php on line 11

Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file' in E:\Xampp\php\fpdf181\fpdf.php:271 
Stack trace: #0 E:\Xampp\php\fpdf181\fpdf.php(1063): FPDF->Error('Some data has a...') 
#1 E:\Xampp\php\fpdf181\fpdf.php(999): FPDF->_checkoutput() 
#2 E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\pdf.php(12): FPDF->Output() 
#3 E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\test.php(3): include('E:\\Xampp\\htdocs...') 
#4 {main} thrown in E:\Xampp\php\fpdf181\fpdf.php on line 271

How should I pass the variables? Interesting: it works if I use the unfiltered $_POST with the following code:

require('E:\Xampp\php\fpdf181\fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(195,5, $_POST['tytul'], 0,1,'C');
$pdf->Cell(195,5, $_POST['petycja'], 0,1,'C');
$pdf->Output();  

EDIT: I will post the initial form and filtering function:
Form:

<form action="test.php" method="POST">
    Wpisz tytuł petycji (35 znaków):<br>
        <input type="text" name="tytul" maxlength="35" size="35" placeholder="Tytuł petycji" required><br>
    Wpisz treść petycji (500 znaków):<br>
        <textarea name="petycja" maxlength="500" rows="4" cols="50" placeholder="Treść petycji" required></textarea><br>
        <input type="submit" value="Napisz petycje">        
    </form>

Filtering function:

class Pdf_filter{
    protected $title;
    protected $text;
        public function pdfFilter(){
            if (isset($_POST)){

            foreach ($_POST as $key => $val) {

            $filterVal = strip_tags($val);
            $filterVal = htmlspecialchars($filterVal);
            $filterVal = stripslashes($filterVal);
            $filterVal = str_replace("\\", "", $filterVal);

            $filter = array($key => $filterVal);
                foreach ($filter as $key => $val) {
                    echo "[$$key]";
                    echo "$val<br>";
                    ${$key} = $val;
                    }
            }
        if(!preg_match("/^[\sa-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż0-9-_,.:\'?()]+$/", $tytul)){
            echo "Niedozwolone znaki $tytul!";
            exit();
            }
            elseif(!preg_match("/^[\sa-zA-ZĄĆĘŁŃÓŚŹŻąćęłńóśźż0-9-_,.:\'?()]+$/", $petycja)){
                echo "Niedozwolone znaki $petycja!";
                exit();
            }
            else{
                return $filter = array('tytul'=>$tytul,'petycja'=>$petycja);
            }
    }
    else{
        echo "Proszę wypełnić wszytskie pola!";
        }
    }
}

Solution

  • Well I am dumb. The problem was related with class variables. Code that happened to work for me:

    class Pdf extends FPDF{
            protected $filter;
            protected $tytul;
            protected $petycja;
            public function __construct($filter){
                $this->filter = extract($filter);
                $this->tytul = $tytul;
                $this->petycja = $petycja;
            }
            public function tytul(){
                return $this->tytul;
            }
            public function petycja(){
                return $this->petycja;
            }
            public function dokument(){
                parent::__construct();
                $this->AddPage();
                $this->SetFont('Arial','B',15);
                $this->Cell(195,5, $this->tytul, 0,1,'C');
                $this->Cell(195,5, $this->petycja, 0,1,'C');
                $this->Output();
            }
    }
    

    Now I need to think of a way to display polish symbols in fpdf and line breaks (but that maybe done with text editor isntead of just textbox).