开发者

Get output file with a .pdf extension in FPDF

This question is for those who have used PHP library FPDF (http://www.fpdf.org ) to generate PDF documents using PHP. I am generating a PDF file using the php file 'my_file.php'. I want users to be able to download that PDF file. But in the browser the see the file in the address bar as ..somepath..../my_file.php 开发者_Go百科. I want them to see it as a file with .pdf extension. Any idea how this can be done ?


when you create the object and then try to make output like this

$filePath = "files/cache/myPdf.pdf";
$pdf=new FPDF('p');
...
$pdf->Output($filePath,'I');

you can change and send the file name


To force download:

$pdf->Output('D');  //Force download and set filename as 'doc.pdf'

or setting your own filename:

$pdf->Output('MyFilename.pdf','D');  

Your browser shall not open another tab whit yourpath/my_file.php


You can't change the browser address bar, but you can change the address on your server. For example if you're using Apache, there's mod_rewrite which allows you to do such things.

If your problem is that when downloading the file, the browser wants to save it as .php, you could use those headers to force the download and a filename.

header('Content-Type: application/octet-stream');
header('Content-Length: ' . FILESIZE_HERE);
header('Content-Disposition: attachment; filename=' . FILENAME.pdf_HERE);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜