2

I am trying to set the Title of the Browser as the PDF document Title. Though the pdf generates correctly, i get the Title randomly.

response.setDateHeader("Expires", 0L);

response.setHeader("Content-disposition", "inline;filename=" + title + ".pdf");

    response.setContentType("application/pdf");
    response.setContentLength(bArray.length);
    try {
                   response.getOutputStream().write(bArray);
        response.getOutputStream().close();}

Could someone please help me out here to override the pdf title

Tiny
  • 683
  • 5
  • 18
  • 36
  • `I get the Title randomly` - what do you mean by "randomly". Can you provide examples? – Thomas Feb 26 '16 at 10:27
  • [w3.org](https://www.w3.org/Protocols/HTTP/Object_Headers.html#title) states there is a "Title" header field, did you try setting that? – Thomas Feb 26 '16 at 10:29
  • i get this pdf from an external third party tool. this title comes as per that tool. I want to override this title and set it as the document title, so that the browser gets the document title – Tiny Feb 26 '16 at 10:29
  • yes Title is a markup code ... im not sure how do i set it here in the response – Tiny Feb 26 '16 at 10:30
  • No, the page says there's a `Title: whatever` header which is "isomorphic" to the HTML tag. I didn't use it myself but it might be worth a try. – Thomas Feb 26 '16 at 10:35

4 Answers4

1

You cannot set the title while at the same time sending a file down to the browser. For the browser to change the "page" title, it needs to actually render an HTML document (ie. it needs to render a page).

But there is a workaround to achieve it. Redirect it to a page that renders out the title, . And from that page trigger the browser over to the download.

Anil
  • 1,735
  • 2
  • 20
  • 28
  • I am redirecting the response to a new window.. I need to set the title for this new window. Can u help with some code snippet – Tiny Feb 26 '16 at 10:31
-1

Try this !!!!

In html/jsp page:

<a href="pdf/filename.pdf">

In servlet:

 String filename = request.getPathInfo().substring(1);//filename.pdf

Also you cen remove .pdf from title by simple java operation.

P S M
  • 1,121
  • 12
  • 29
  • my_window = window.open('/test/servlet/PDFServlet?documentId=123'); My code is not in href ..Also my title needs to be set in response .. as it needs to be displayed on browser window – Tiny Feb 26 '16 at 12:48
-1
content-disposition: attachment

Similar question found here

Community
  • 1
  • 1
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48
-1

Replace your code with

response.setHeader("Content-disposition", "attachment;filename=" + title + ".pdf");

response.setContentType("application/octet-stream");
response.setContentLength(bArray.length);
try {
               response.getOutputStream().write(bArray);
    response.getOutputStream().close();}

Read answer description here set metadata response

Dheeraj Upadhyay
  • 336
  • 2
  • 12