MonoThreaded
MonoThreaded

Reputation: 12033

Renaming a PDF streamed by Java Servlet

Using the right mix of XSLT, XSL-FO and Apache FOP, I am able to send a PDF into some browser window.
In fact, I am sending the file content as follows:

           response.setContentType("application/pdf");
            response.setContentLength( out.size());
            response.getOutputStream().write( out.toByteArray());
            response.getOutputStream().flush();

As expected, the browser shows the PDF content in a tab named "pdf", and if I save the file locally, the name also defaults to pdf.pdf
How can I force the file name?

I tried the following among other things that didn't work:

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

For clarity I want to display the content in the browser (as opposed to a straight download)

Upvotes: 3

Views: 2210

Answers (1)

MonoThreaded
MonoThreaded

Reputation: 12033

I found the solution here

Basically, it's a matter of changing the URL pattern in web.xml
The browser will use whatever 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>

Invoking the servlet using http://wherever/pdf/filename.pdf?param1=va1... changes the file name to "filename.pdf"

Upvotes: 8

Related Questions