使用 header() 重写动态 pdf URL 中的文件名

发布于 2024-08-16 16:24:26 字数 1122 浏览 2 评论 0原文

我有一个可以生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我纯我任性 2024-08-23 16:24:26

尝试:

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

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

另一种选择是使用 $_SERVER['PATH_INFO'] 传递您的“July Report.pdf” - 示例链接可能是:

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

该文件应默认保存为“July Report” .pdf” - 其行为应该与旧的 php 脚本完全相同,只需更改生成 pdf 链接的代码即可。

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:

<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.

离不开的别离 2024-08-23 16:24:26

应该是:

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

Should be:

header('Content-disposition: attachment;filename="July Report.pdf"'); 
恍梦境° 2024-08-23 16:24:26

基于 https://github.com/rospdf/pdf-php/ raw/master/readme.pdf(第19页)
流方法接受一个数组作为参数:

stream([数组选项])
用于输出,这将设置所需的标题并输出 pdf 代码。
选项数组可用于设置有关输出的许多内容:

<块引用>

'Content-Disposition'=>'filename' 设置文件名,...

这段代码对我来说效果很好

$ezOutput = $pdf->ezStream(array("Content-Disposition"=>"YourFileName.pdf"));

在我的例子中我使用 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:

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, ...

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.

酷到爆炸 2024-08-23 16:24:26

对于浏览器标题选项卡上显示的名称,

我遇到了同样的问题,然后我发现我的 .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=

栖竹 2024-08-23 16:24:26

您是否尝试使用连字符从文件名中删除空格?所以,我想它的名字一定是这样的;文件名=七月报告.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文