Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adnan483's avatar

Using FPDF in Laravel 5.5?

Hi, I'm trying to use FPDF in my project, I use setasign/fpdf from Git, but every time when I try to open it, it shows tons of unknown data.

Here is my code:


$pdf = new \setasign\Fpdi\Fpdi();
$pdf->AddPage();
$pdf->setSourceFile('dokumenti/garancija.pdf');
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at position 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 5, 5, 205);

// now write some text above the imported page
$pdf->SetFont('Times');
$pdf->SetTextColor(0, 0, 0);
$pdf->SetXY(25, 75);
$pdf->Write(0, 'Test');
$pdf->SetXY(25, 80);
$pdf->Write(0, 'Test');
$pdf->SetXY(19, 55);
$pdf->Write(0, 'Test');
$pdf->Output();

0 likes
6 replies
annewhitman's avatar

I'm running into a similar issue, did you ever come across a solution?

adnan483's avatar

Yeah of course, the problem is FPDF is built for PHP 5.*, it doen't work in PHP 7, and in some cases it works from web.php(routes) not in controllers(show method).

1 like
hapona's avatar

I was getting a similar problem and found a possible answer. Try adding an exit(); under your $pdf->Output();. Not 100% why you need to explicitly exit, but it's worth a try.

cowboy74's avatar

If you make $pdf->Output(); the FPDF Class will create headers to show the pdf Inline. But Laravel overwrites these headers, thats why you get the content of the PDF on your screen.

There are ways to solve this. First way is to change the FPDF Class and add an exit right here:

            if(PHP_SAPI!='cli')
            {
                // We send to a browser
                header('Content-Type: application/pdf');
                header('Content-Disposition: inline; '.$this->_httpencode('filename',$name,$isUTF8));
                header('Cache-Control: private, max-age=0, must-revalidate');
                header('Pragma: public');
            }
            echo $this->buffer;
            exit;

In this case you should manually install FPDF not NOT install it via composer.

Second way is to tell Laravel to specifically use the Inline-Headers and not overwrite it. In this case you can do it the following way:


            $fpdf = new App\Classes\FPDF\FPDF;
            $fpdf->AddPage();
            $fpdf->SetFont('Courier', 'B', 18);
            $fpdf->Cell(50, 25, 'Hello World!');
            $fpdf->Output();

            $response = response($fpdf->Output('S'));
            $response->header('Content-Type', 'application/pdf');
            $response->header('Content-Disposition', 'inline; filename="output.pdf"');
            $response->header('Cache-Control:', 'private, max-age=0, must-revalidate');

            return $response;

Please or to participate in this conversation.