Renaming PDF passed to Java Servlet
Using the right combination of XSLT, XSL-FO and Apache FOP, I can send the PDF to the browser window.
In fact, I am sending the content of the file like this:
response.setContentType ("application / pdf"); response.setContentLength (out.size ()); response.getOutputStream (). write (out.toByteArray ()); response.getOutputStream (). flush ();
As expected, the browser displays the PDF content in a tab named "pdf" and if I save the file locally, the name will also default to pdf.pdf
How do I force the filename?
I've tried the following, among others:
response.setHeader ("Content-Disposition", "inline; filename =" + filename + ".pdf");
For clarity, I want to display the content in the browser (as opposed to direct download)
source to share
I found a solution here
Basically it is a matter of changing the url pattern in web.xml
The browser will use what the page name looks like I used to have
<servlet-mapping>
<servlet-name>PDF</servlet-name>
<url-pattern>/pdf</url-pattern>
</servlet-mapping>
which I replaced with
<servlet-mapping>
<servlet-name>PDF</servlet-name>
<url-pattern>/pdf/*</url-pattern>
</servlet-mapping>
Calling the servlet with http://wherever/pdf/filename.pdf?param1=va1...
changes the filename to "filename.pdf"
source to share