PHP 中设置的内容处置标头不适用于 Firefox

发布于 2024-08-16 03:36:43 字数 1541 浏览 7 评论 0原文

我有一个表单按钮,用于将数据提交到通用脚本(表单加载的同一页面,index.php),如果将正确的 POST 变量提交到该脚本,它会返回一个 PDF 供用户打印(使用加载的相关表单信息,以及其他数据库信息等)。

问题是,无论我在标头中设置内容类型和内容处置,Firefox 都会提示用户保存或打开文档。

如果 Firefox 提供自己的 Adob​​e Reader 插件作为“打开方式”选项,情况还不错,但它只提供实际的 Adob​​e Reader 应用程序(由于身份验证和默认浏览器设置超出我的控制范围等原因,这会导致问题) )。

那么,我需要在服务器端更改一些内容(在 .htaccess 文件中或在实际标头中),以使 Firefox 收到消息,响应既是具有 application/pdf MIME 类型的 PDF,因此应该将文件视为它是 PDF 的硬链接,而不是服务器生成的流?

最后一件事...

如果我将 Content-Disposition 设置为“内联”,Firefox 会在 Firefox 中打开文件,但仅在首先将页面加载为“index.php”之前。一旦用户访问 PDF,如果他们尝试单击后退按钮,他们会点击加载 PDF 的 index.php,而不是他们提交数据的 index.php。在 Mac 方面,Firefox 不会加载 PDF,而是提示用户保存或打开“index.php”,就好像它是附件一样。

谢谢!


目前我尝试过的代码(按此顺序):

这个代码被视为文件附件,因此文件扩展名规则会忽略 Firefox 的 Mime-Type 规则:

    header("Content-Type: application/pdf");
    header('Content-Disposition:attachment; filename="testing.pdf"');

这个代码可以工作,但会加载 index.php< /code> 首先,然后加载文件,这没有吸引力,令人困惑,并在 pdf 和原始页面之间提出了额外的请求(因此是一个额外的页面):

    header("Content-Type: application/pdf");
    header('Content-Disposition:inline; filename="testing.pdf"');

这个被视为与附件 (在 Firefox 中),因此仅提示在 Adob​​e Reader 中保存或打开,而不是默认使用 Adob​​e Reader 插件:

    header("Content-Type: application/pdf");
    header('Content-Disposition:pdf; filename="testing.pdf"');

另请注意,在最后一个示例中,我尝试了 pdfapplication\pdf 作为内容配置。没有区别。

我唯一还没有尝试过的是将类型设置为 ocet/stream。我会交叉手指,但不会屏住呼吸。

I have a form button that submits the data to generic script (the same page that the form is loaded from, index.php) and if the right POST variables are submitted to that script, it returns a PDF for the user to print (with the relevant form info loaded, and other DB info, etc).

The problem is that no matter what I set the content-type and the content-disposition to in the header, Firefox prompts the user to either save or open the document.

This wouldn't be so bad if Firefox offered it's own Adobe Reader plugin as an "Open With" option, but it only offers the actual Adobe Reader App (which causes problems due to authentication and default browser settings being out of my control, etc).

So is there something server side I need to change (either in the .htaccess file or in the actual header) to have Firefox get the message that response is both a PDF with the application/pdf MIME Type and thus should treat the file as though it were a hard link to a PDF, not a server-generated stream?

One last thing...

If I set the Content-Disposition to "inline" Firefox opens the file in Firefox, but only before loading the page as "index.php" first. Once the user gets to the PDF, if they try clicking the back button, they hit the index.php that loaded the PDF, not the one that they submitted the data. On the Mac side of things, Firefox doesn't load the PDF but instead prompts the user to save or open "index.php" as though it were an attachment.

Thanks!


Currently the code I've tried (in this order):

This one gets treated as a file attachment, thus the Mime-Type rule for Firefox is ignored for the file extension rule:

    header("Content-Type: application/pdf");
    header('Content-Disposition:attachment; filename="testing.pdf"');

This one works but loads index.php first and then loads the file, which is unattractive, confusing, and puts an extra request (and thus an extra page) between the pdf and the original page:

    header("Content-Type: application/pdf");
    header('Content-Disposition:inline; filename="testing.pdf"');

This one gets treated the same as attachment (in Firefox) and thus only prompts to save or open in Adobe Reader rather than defaulting to Adobe Reader plugin:

    header("Content-Type: application/pdf");
    header('Content-Disposition:pdf; filename="testing.pdf"');

Also notice that in the last example I have tried both pdf and application\pdf as the content disposition. No difference.

The only thing I haven't tried yet is setting the type as ocet/stream. I'll cross my fingers but won't hold my breath.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

蓬勃野心 2024-08-23 03:36:43

您可以尝试让链接以“.pdf”结尾,并使用 mod_rewrite 之类的内容将其映射到您的 PHP 脚本。

You could try having the link end in ".pdf", and use something like mod_rewrite to map it to your PHP script.

偏闹i 2024-08-23 03:36:43

我认为 Firefox 很难处理 PDF 作为对 POST 的响应,您可以尝试生成 PDF 链接并让他们单击该链接吗?该链接可能包含 GET 变量,例如 blah.php?VAR =123 因此您仍然可以动态生成 PDF。

I'm thinking that Firefox is having a hard time handling the PDF as a response to a POST, could you try generating a link to the PDF and having them click on the link, the link could contain GET variables like blah.php?VAR=123 so you can still dynamically generate the PDF.

木緿 2024-08-23 03:36:43

mime 类型 application/octet-stream 应强制浏览器下载它。我不知道你的帖子是否就是你想要的。

The mime-type application/octet-stream should force the browser to download it. I can't tell if that's what you want from your post.

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