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

imcml3010's avatar

Problems with redirect after downloading pdf using FPDF

Is there any approach to redirect to certain route/ refresh current page after download the pdf? I've tried to return an URL but it just goes to the url without generating the file. If I use the code below, it will generate the file and direct to a blank page. I am using laravel and fpdf here.

Route: Route::post('alumni/labels','AlumniController@labels')->name('alumni/labels');

Controller:

public function labels(){
        $alumni = Alumni::whereIn('id', Session::get('alumniId'))->get();
        $pdf = new PDF_Label('TJ103');
        $pdf->AddPage();
        foreach ($alumni as $alumnus) {
            $text = sprintf("%s %s\n%s\n%s %s", $alumnus->firstName, $alumnus->lastName, $alumnus->address, $alumnus['hp1'], $alumnus['phone']);
            $pdf->Add_Label($text);
        }
        return $pdf->Output('d');
}

View:

<form method="post" action="labels">
                {!! csrf_field() !!}
                <button class="btn btn-primary" id="btn-submit">Generate Label</button>
    </form>
0 likes
2 replies
exorion's avatar

I think there is no way - you should open this new window (link should be with target=_blank, window checks a headers and start downloading (or showing) file) - and making a redirect by javascript at current page. You have a ways to popup a div and show it there with a close button

Snapey's avatar

I don't have any issue, the file just downloads and the browser stays on the same page.

I have Output type 'D' though and not 'd'

http://www.fpdf.org/en/doc/output.htm

Call the FPDF output and then just return; from the controller or redirect to a new page (don't have the output and the return on the same statement)

Please or to participate in this conversation.