简体   繁体   English

在FPDF中获取扩展名为.pdf的输出文件

[英]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. 这个问题适用于那些使用PHP库FPDF(http://www.fpdf.org)使用PHP生成PDF文档的人。 I am generating a PDF file using the php file 'my_file.php'. 我正在使用php文件“ my_file.php”生成PDF文件。 I want users to be able to download that PDF file. 我希望用户能够下载该PDF文件。 But in the browser the see the file in the address bar as ..somepath..../my_file.php . 但是在浏览器中,在地址栏中看到的文件是..somepath .... / my_file.php。 I want them to see it as a file with .pdf extension. 我希望他们将其视为具有.pdf扩展名的文件。 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 您的浏览器不得打开其他标签为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. 例如,如果您使用的是Apache,则可以使用mod_rewrite进行此类操作。

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. 如果您的问题是下载文件时,浏览器要将其另存为.php,则可以使用这些标头来强制下载和文件名。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM