如何在浏览器中设置PDF文件的标题? [英] How to set the title of a pdf file in browser?

查看:522
本文介绍了如何在浏览器中设置PDF文件的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nodejs服务器提供静态文件.提供pdf文件后,浏览器将标题显示为pdf路径的URL.

I am using nodejs server to serve static files. When the pdf file is served, the browser displays the title as URL of the pdf path.

127.0.0.1:8080/docs/sample

127.0.0.1:8080/docs/sample

如何将其设置为自定义标题"Sample"(样品)

How can I set this to a custom title say "Sample"

我尝试了以下操作,但没有运气:

I have tried following things but no luck :

  1. res.setHeader('Content-Disposition','inline; filename ="sample.pdf"');
  2. 将pdf文件的元标记设置为样本"

任何帮助将不胜感激.

推荐答案

如果您使用的是静态文件服务器,那么可以将其作为下载文件使用.现代的浏览器通常包含一个内置的PDF查看插件,因此,浏览器无需询问用户将文件保存在何处,而是直接在浏览器选项卡中显示PDF.仍然会下载它,在这种情况下,它只是将其保存到计算机上的一些临时缓存中.

If you're using a static file server, then yes you are serving it as a download. Modern browsers often contain a built-in PDF viewing plugin, so instead of asking the user where to save the file, the browser will instead just display the PDF right in a browser tab. It still downloaded it, it just saved it to some temporary cache on your machine in that case.

我要说的是,在那种情况下,您无法控制浏览器标题,因为它只是浏览器在试图使用户变得方便并使其变得方便.PDF文件本身不知道是在浏览器的内置查看器中显示还是在桌面上的Adobe Reader中显示.由于浏览器希望使用实际网页上运行的HTML或JavaScript来设置页面标题,因此您无法发送HTTP标头来设置标题.

What I'm getting at is that you cannot control the browser title in that case because it's just the browser trying to be nice and make things convenient for the user. The PDF file itself would have no idea if it was being displayed in the browser's built-in viewer or in Adobe Reader on the desktop. There are no HTTP headers you could send down to set the title either because browsers expect page titles to be set from HTML or JavaScript running on an actual web page.

现在,如果要使用某种PDF查看器将PDF文件嵌入HTML页面,则可以使用简单的< title>某些标题</title> 控制页面标题.标记或从JavaScript调用 document.title ='some title'; .之所以可行,是因为浏览器正在呈现您控制的实际网页,并且该网页恰好在其上嵌入了PDF查看器.

Now, if you were to embed the PDF file in an HTML page with some kind of PDF viewer then you could control the page title with a simple <title>some title</title> tag or calling document.title = 'some title'; from JavaScript. That works because the browser is rendering an actual web page that you control, and that page just happens to have an embedded PDF viewer on it.

这是可嵌入PDF查看器的示例. http://pdfobject.com/

Here's an example of an embeddable PDF viewer. http://pdfobject.com/

这篇关于如何在浏览器中设置PDF文件的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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