使用 header() 重写动态 pdf URL 中的文件名
我有一个可以生成 pdf 报告的 php 脚本。当我们去保存pdf文档时,Acrobat建议的文件名是report_pdf,因为php脚本名为report_pdf.php。我想动态命名 pdf 文件,这样我就不必每次保存报告时都输入适当的名称。
在新闻组上询问时,有人建议这样做,其中 filename="July Report.pdf"
是报告的预期名称,
<?
header('Content-Type: application/pdf');
header('Content-disposition: filename="July Report.pdf"');
但它不起作用。我做错了吗,或者这会起作用吗?这是 mod_rewrite 的工作吗?
So I've tried both
header('Content-disposition: inline; filename="July Report.pdf"');
和
header('Content-disposition: attachment; filename="July Report.pdf"');
(不是同时)并且都不适合我。这是我的虚拟主机的问题吗?对于这个 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();
?>
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.
Asking on a news group, someone suggested this, where filename="July Report.pdf"
is the intended name of the report
<?
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?
So I've tried both
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:
<?
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();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
或
另一种选择是使用
$_SERVER['PATH_INFO']
传递您的“July Report.pdf” - 示例链接可能是:该文件应默认保存为“July Report” .pdf” - 其行为应该与旧的 php 脚本完全相同,只需更改生成 pdf 链接的代码即可。
Try:
or
Another option would be to use the
$_SERVER['PATH_INFO']
to pass your "July Report.pdf" - an example link might be: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.
应该是:
Should be:
基于 https://github.com/rospdf/pdf-php/ raw/master/readme.pdf(第19页)
流方法接受一个数组作为参数:
这段代码对我来说效果很好
在我的例子中我使用
ezStream()
但我认为< code>stream() 应该给出相同的结果。Based on https://github.com/rospdf/pdf-php/raw/master/readme.pdf (Page 19)
The stream-method accepts an array as parameter:
This code works well for me
In my case I use
ezStream()
but I thinkstream()
should give the same result.对于浏览器标题选项卡上显示的名称,
我遇到了同样的问题,然后我发现我的 .pdf 中缺少元数据。
我使用了一个工具(“debenu PDF 工具”)来编辑 pdf 属性,如作者、标题等......
我只需将标题从空字段更改为我想要的标题,上传新的 pdf,现在,使用相同的代码、相同的脚本,浏览器会显示正确的名称!
当您要求保存文档时,名称
是您在头文件名=中指定的名称
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!
For the name when u ask to save document
is what u specify in header filename=
您是否尝试使用连字符从文件名中删除空格?所以,我想它的名字一定是这样的;文件名=七月报告.pdf
Did you try to remove spaces from file name using hyphens? So, I think its name must be like this; filename=July-Report.pdf