My Java based webapp has a servlet which streams PDF content back to the browser based on request parameter.
e.g. user clicks on an A tag with an href of "myApp/FetchPDFServlet?id=123". Servlet mapping picks up request, streams PDF data to response as mime-type application/pdf, closes flushes buffers.
However the browser title bar for the page displaying the PDF reads "FetchPDFServlet?id=123"
How can I change the title the browser displays for the page that displays the PDF? So the browser title is "Here is the amazing PDF" not "FetchPDFServlet?id=123".
Is it possible at all? How best to do this?
Add this header to your HttpServletResponse:
response.setHeader("Content-Disposition","inline; filename=Here is the Amazing PDF");
I believe the browser will pick it up and use it as the title of the window.
You could display the PDF in an iframe.
Something like this:
<html>
<head>
<title>Here is the amazing PDF</title>
<style type="text/css">
html, body, div, iframe { margin:0; padding:0; height:100%; }
iframe { display:block; width:100%; border:none; }
</style>
</head>
<body>
<iframe width="100%" length="100%" src="myApp/FetchPDFServlet?id=123"/>
</body>
</html>
So instead of linking to a pdf document using myApp/FetchPDFServlet?id=123
, you would link to something that returns the above html. For example, a jsp page: myApp/ShowPDF.jsp?id=123&title=Here%20is%20the%20amazing%20PDF
I have hit this issue,and while my solution has quite a few restrictions I thought I would share it.
Baiscally I will open the pdf in a new tab, and then change its title from the original page.
$('#aa').click(function(){
ref = window.open('resume.pdf','mywindow');
ref.onload = function(){
ref.document.title="New Title";
}
return false;
});
});
Note that the parent and child pages must be in the same domain.
I have tested this in a few browsers, and here are the results:
- Chrome 10 - Works like a treat
- Safari 5 - Works, but by default opens in a new window not tab. User can change this to open all new windows in tabs.
- IE 8 - Doesn't work, as expected :op
- Firefox - Works like a treat
The tab title is taken from the PDF documents metadata, specifically the Title attribute. So if you generate the document in your app, your PDF library should have a way to set the documents metadata.
I have tried a solution in java and it worked. You can set given headers in other languages
response.setHeader("Content-Disposition","inline; filename=\"MyFile.pdf\"");
response.setContentType("application/pdf; name=\"MyFile.pdf\"");
response.getOutputStream().write(pdfAsBytesArray);
来源:https://stackoverflow.com/questions/1058959/how-to-change-the-title-of-a-browser-page-which-a-servlet-streamed-a-pdf-to