Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the title of a pdf file in browser?

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

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. Setting meta tag of pdf file as "sample"

Any help will be much appreciated.

like image 861
hkasera Avatar asked Mar 05 '15 16:03

hkasera


People also ask

Why is the title of my PDF different than the file name?

Reason: The browser is simply reading the metadata that is saved in the actual PDF file as the document title, which may be different from the document file name. You may confirm this by opening the PDF file in Adobe Acrobat Reader > navigate to File > Properties.

How do I change PDF settings in Chrome?

In Chrome, click the three dots in the upper right corner of your browser window. Navigate to Settings › Advanced › Privacy and Security. Click Site Settings › PDF Documents.


1 Answers

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.

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.

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.

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

like image 54
Chev Avatar answered Sep 20 '22 23:09

Chev