7
votes

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.

2
If you're sending the PDF to the client as a download then it's up to the client's browser how it displays it. If you want more control then you need to serve a page with the PDF embedded.Ben Fortune
No I am not sending it as a download.hkasera

2 Answers

7
votes

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/

3
votes

//hack alert

You could trick the browser by setting the last fragment of the url to whatever you like . i.e. change sample in your example to the desired title.

(tested in chrome 58.0.3029.110)

HTH