php 使用 header() 重写 URL 中的文件名以获取动态 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2015985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using header() to rewrite filename in URL for dynamic pdf
提问by user151841
I have a php script that generates a pdf report. When we go to save the pdf document, the filename that Acrobat suggests is report_pdf, since the php script is named report_pdf.php. I would like to dynamically name the pdf file, so I don't have to type the appropriate name for the report each time that I save it.
我有一个生成 pdf 报告的 php 脚本。当我们去保存pdf文档时,Acrobat建议的文件名是report_pdf,因为php脚本被命名为report_pdf.php。我想动态命名 pdf 文件,所以我不必在每次保存报告时键入适当的名称。
Asking on a news group, someone suggested this, where filename="July Report.pdf"is the intended name of the report
在新闻组上问,有人建议这个,filename="July Report.pdf"报告的预期名称在哪里
<?
header('Content-Type: application/pdf');
header('Content-disposition: filename="July Report.pdf"');
But it doesn't work. Am I doing it wrong, or will this work at all? Is this a job for mod_rewrite?
但它不起作用。我做错了吗,或者这会起作用吗?这是 mod_rewrite 的工作吗?
所以我都试过了
header('Content-disposition: inline; filename="July Report.pdf"');
and
和
header('Content-disposition: attachment; filename="July Report.pdf"');
( not at the same time ) and neither work for me. Is this a problem with my web host? For this url, here's my code:
(不是同时)而且都不适合我。这是我的网络主机的问题吗?对于这个url,这是我的代码:
<?
header('Content-disposition: inline; filename="July Report.pdf"');
// requires the R&OS pdf class
require_once('class.ezpdf.php');
require_once('class.pdf.php');
// make a new pdf object
$pdf = new Cpdf();
// select the font
$pdf->selectFont('./fonts/Helvetica');
$pdf->addText(30,400,30,'Hello World');
$pdf->stream();
?>
回答by gnarf
Try:
尝试:
header('Content-Disposition: attachment; filename="July Report.pdf"');
or
或者
header('Content-Disposition: inline; filename="July Report.pdf"');
Another option would be to use the $_SERVER['PATH_INFO']to pass your "July Report.pdf" - an example link might be:
另一种选择是使用$_SERVER['PATH_INFO']传递您的“July Report.pdf” - 示例链接可能是:
<a href="report_pdf.php/July%20Report.pdf?month=07">
That file should default to saving as "July Report.pdf" - and should behave exactly like your old php script did, just change the code that produces the link to the pdf.
该文件应默认保存为“July Report.pdf” - 并且应该与旧的 php 脚本完全一样,只需更改生成 pdf 链接的代码即可。
回答by Seva Alekseyev
Should be:
应该:
header('Content-disposition: attachment;filename="July Report.pdf"');
回答by Seelixh
Based on https://github.com/rospdf/pdf-php/raw/master/readme.pdf(Page 19) The stream-method accepts an array as parameter:
基于https://github.com/rospdf/pdf-php/raw/master/readme.pdf(Page 19) 流方法接受一个数组作为参数:
stream([array options])Used for output, this will set the required headers and output the pdf code. The options array can be used to set a number of things about the output:
'Content-Disposition'=>'filename'sets the filename, ...
stream([array options])用于输出,这将设置所需的标题并输出 pdf 代码。options 数组可用于设置有关输出的许多内容:
'Content-Disposition'=>'filename'设置文件名,...
This code works well for me
这段代码对我很有效
$ezOutput = $pdf->ezStream(array("Content-Disposition"=>"YourFileName.pdf"));
In my case I use ezStream()but I think stream()should give the same result.
在我的情况下,我使用ezStream()但我认为stream()应该给出相同的结果。
回答by JoTaRo
For the name shown on the title tab in the browser
对于浏览器标题选项卡上显示的名称
I had the same problem, then i found that it's metadata missing inside my .pdf. I used a tools ("debenu PDF tools") for edit pdf property like author, title, etc... I just change title from empty field to what title I want, upload the new pdf and now, with same code, same script the browser show the right name!
我遇到了同样的问题,然后我发现我的 .pdf 文件中缺少元数据。我使用了一个工具(“debenu PDF 工具”)来编辑 pdf 属性,如作者、标题等......我只是将标题从空字段更改为我想要的标题,上传新的 pdf,现在,使用相同的代码,相同的脚本浏览器显示正确的名称!
For the name when u ask to save document
对于您要求保存文档时的名称
is what u specify in header filename=
是您在头文件名中指定的内容=
回答by K-Gun
Did you try to remove spaces from file name using hyphens? So, I think its name must be like this; filename=July-Report.pdf
您是否尝试使用连字符从文件名中删除空格?所以,我想它的名字一定是这样的;文件名=七月报告.pdf

