$fpdf->Output( 'D', "filename.pdf") return pdf string to browser instead of forcing download

106 Views Asked by At

I merged multiple pdfs using fpdf, storing it locally to a path works fine. I want to skip storing and download the pdf straight from memory. I used the following in a service :

//All had the same outcome (returned pdf string to the browser *see imager)
$fpdf->Output( 'D', "filename.pdf")
$fpdf->Output( 'I', "filename.pdf")
$fpdf->Output( 'S')

Network Tab Response

Using:

  • PHP 7.4
  • Laravel 8
  • setasign/fpdf (1.8.6)
  • setasign/fpdi (v2.6.0)

I tried:

$fpdf->Output( 'F', "path/filename.pdf")
return response->download("path/filename.pdf")

$output =  $fpdf->Output( 'S');
        
return Response::make($output, 200, [
       'Content-Type' => 'application/pdf',
       'Content-Disposition' => 'attachment; filename="merged.pdf"',
       ]);

same issue, it returns the pdf string to the browser.

1

There are 1 best solutions below

0
mbwmd On
  1. Verify the response headers, not the content alone.
  2. Your Browser may also interfere, when being set as default pdf viewer.

When the response headers tell you,

Content-Disposition attachment;filename="xy.pdf"

then you are okay.