Can I set the “HTML” Title of a PDF file served by my Apache Web server

风流意气都作罢 提交于 2019-12-10 13:39:06

问题


I have HTML pages that contain <a> tags with hrefs that point to PDF files. My Apache Web server serves them just fine, but the title, as shown in the Browser history, is of the file name. I would like to be able to set that title.

Perhaps there's a Header than can be set?

I don't want to write a script to serve the files as the server can handle Content-Encoding negotiation (e.g., for gzip), and do flow control, none of such do I want to re-create.


回答1:


Here is an http header you can set.

Content-Disposition:inline; filename="*File name you want*";



回答2:


I suspect the issue you are having is that the client browser is storing the file name in the history, which you cannot fix.

Last I checked, the title in the history came from the setting of the HTML page (not a header), so there should be no HTTP header field for the title.

I am no HTTP expert and do not know all the fields, but I do not remember there being a setting in any server that I have ever worked with to set the page's title (just the status code, protocol, etc.)




回答3:


Very old question, I know. But I had exactly the same problem today and found a solution.

I created a PHP-Skript (e.g. download.php, filename doesn't matter) like this:

<?php
header("Content-Type: application/pdf");
header('Content-Disposition: inline; filename="'.addslashes($_REQUEST['title']).'"'); //filename doesn't set the browser title here, only when page is saved/downloaded
$content = file_get_contents($_REQUEST['filename']);
header('Content-Length: '.strlen($content));
echo $content;

In my setup (Windows 10; Apache 2.4.37; PHP 7.1.25) you can now do a request like this: http://..../download.php/browser_title?filename=test.pdf

I tested this successfully in Chrome and Firefox. Internet Explorer was not able to display a PDF-Document inline :-/. Microsoft Edge also included the URL-Parameter in the title bar :-(.



来源:https://stackoverflow.com/questions/5667576/can-i-set-the-html-title-of-a-pdf-file-served-by-my-apache-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!