I am generating PDF file by using FPDF library with PHP. PDF file getting generated and showing in browser perfectly.
However instead of it showing in browser I want forcefully save option instead of showing it.
I am generating PDF file by using FPDF library with PHP. PDF file getting generated and showing in browser perfectly.
However instead of it showing in browser I want forcefully save option instead of showing it.
FPDF has a download function build in, use the D
argument:
// At the end of your PDF generation code:
$pdf->Output('file.pdf', 'D');
To do this, you need to send the appropriate content header:
<?php
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename=MyFile.pdf');
readfile('MyFile.pdf');
To implement this, simply cause the user's browser to open a page with the above headers, where MyFile.pdf refers to your pdf file's location (you can use cookies, sessions, form submissions, or whatever to figure out that file location). In this example I left off the closing ?>
tag; this may or may not be neccessary.
Yeah, you can do this with .htaccess
as well.
Just add the following line:
AddType application/octet-stream .pdf