使用header()重写动态pdf的URL中的文件名 [英] using header() to rewrite filename in URL for dynamic pdf

查看:168
本文介绍了使用header()重写动态pdf的URL中的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成pdf报告的php脚本。当我们去保存pdf文档时,Acrobat建议的文件名是report_pdf,因为php脚本名为report_pdf.php。我想动态命名pdf文件,因此每次保存时都不必为报告键入适当的名称。

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.

询问新闻组,有人建议这样做,其中 filename =July Report.pdf是预期的报告名称

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"');

但它不起作用。我做错了,还是会这样做?这是mod_rewrite的工作吗?

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"');

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();

?>


推荐答案

尝试:

header('Content-Disposition: attachment; filename="July Report.pdf"');

header('Content-Disposition: inline; filename="July Report.pdf"');

另一种选择是使用 $ _ SERVER ['PATH_INFO'] 传递July Report.pdf - 示例链接可能是:

Another option would be to use the $_SERVER['PATH_INFO'] to pass your "July Report.pdf" - an example link might be:

<a href="report_pdf.php/July%20Report.pdf?month=07">

该文件默认保存为July Report.pdf - 并且应该与您的旧版本完全相同php脚本没有,只需更改生成pdf链接的代码。

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.

这篇关于使用header()重写动态pdf的URL中的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆