10

I have a Java webapp creating a pdf and streaming it back to the browser.

 byte[] pdf = report.exportPdfToArray(user);
response.setContentType("application/pdf");
response.setHeader("content-disposition", "inline; filename=\"My.pdf\"");
outStream = response.getOutputStream();
outStream.write(pdf);
outStream.flush();
outStream.close();

The report is executed and it is sent back to the browser, but I can not control the name of the file even though I set the content-disposition. I am using Jboss 4.2.1. Do you know what am I missing?

EDIT: So is there any way to set the filename when the content-disposition is inline?

Atticus
  • 1,622
  • 7
  • 32
  • 55
  • 1
    Some browser, namely IE6, act funny on this sometimes. I had to add something like ?f=/myfile.pdf at the end of the query string to make it work in IE6. – Spliffster Jan 28 '11 at 15:00

6 Answers6

9

I have tried a solution in java and it worked.

response.setHeader("Content-Disposition","inline; filename=\"MyFile.pdf\"");
response.setContentType("application/pdf; name=\"MyFile.pdf\"");
response.getOutputStream().write(pdfAsBytesArray);
kunal saxena
  • 398
  • 1
  • 6
  • 13
9

content-disposition: attachment ....

Spliffster
  • 6,959
  • 2
  • 24
  • 19
  • 3
    But I don't want to download the file. I want to display it in the browser. – Atticus Jan 28 '11 at 15:01
  • 3
    @Atticus: I must confirm that I have the same problem, add it does not work with inline 'inline' (IE and Firefox) – Ralph Jan 28 '11 at 15:12
  • The pdf receives the name of the servlet. Is there maybe a possibility to set the page's (in which the pdf is displayed) title? – Atticus Jan 28 '11 at 15:50
  • 2
    on some IEs (can't remember which version/OS exactly) you must also make sure to add the filename to the very end of your location or query string (whatever is last). example /myscript.cgi/My.pdf. In that case My.pdf is a fake file (myscript.cgi gets executed). This is possible if Apache is propperly configured. Or alternatively you may add a fake query string to the script like: /myscript.cgi?a=1&b=2&f=/My.pdf. With this hack applied it sould also work with content-disposition: inline ... – Spliffster Jan 28 '11 at 15:51
  • 2
    This is a bit old, but if you don't want to download the file (rather display in browser) you can just use response.setHeader("Content-Disposition", "filename=\"" + pdfFileName + "\""); (omitting the 'attachment; ') – werner Aug 17 '17 at 12:52
3

MSIE will use the last part of the path info of the request URL (the part after the last /) as the default filename of the Save As action. It ignores the filename attribute of the Content-Disposition header altogether. All other browsers treat that header properly.

You need to change the URL pattern of your PDF servlet to a path mapping. I.e. do not use /pdf with http://example.com/context/pdf, but rather use /pdf/* with http://example.com/context/pdf/report.pdf. This way MSIE will use "report.pdf" instead of "pdf" as the default filename for the Save As action.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

I can't detect a flaw. Did you check the behavior with other browsers/readers?

As of RFC, it is not defined what the client has to do do with the filename information if displayed inline...

mtraut
  • 4,720
  • 3
  • 24
  • 33
  • It is even more worse, RFC: The sender may want to suggest a filename to be used if the entity is detached and stored in a separate file. If the receiving MUA writes the entity to a file, the suggested filename should be used as a basis for the actual filename, where possible. It is important that the receiving MUA not blindly use the suggested filename. The suggested filename SHOULD be checked (and possibly changed) to see that it conforms to local filesystem conventions, does not overwrite an existing file, and does not present a security problem... – Ralph Jan 28 '11 at 15:13
  • 1
    there is now flaw in the above implementation but in various IE versions and how IE or the acrobat plugin handles HTTP response headers. I haven't had problems with other browsers. – Spliffster Jan 28 '11 at 16:00
0

It's weird but it can be useful for someone(maybe someone can tell what is wrong with it):

When I set two headers like:

response.addHeader("content-length", String.valueOf(((FileInputStream) is).getChannel().size()));
response.addHeader("Content-disposition", "attachment; filename=\"MyFileName.doc\"");

It doesn't work. But when I change the order it works as expected:

response.addHeader("Content-disposition", "attachment; filename=\"MyFileName.doc\"");
response.addHeader("content-length", String.valueOf(((FileInputStream) is).getChannel().size()));
-2

There is workaround to do so. We can use iframe where iframe will open in html page, iframe will hold the pdf report whereas the html page is independent of iframe. We can edit the title of html page that holds iframe.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
    <head>
        <title>${reportName}</title>
    </head>
    <body>
        <iframe src="/fcWeb/ReportPDFServlet" width="100%" height="100%"></iframe> 
    </body>
</html>
brenjt
  • 15,997
  • 13
  • 77
  • 118
  • There's something not quite write about your code markup here. Take a look [at this guide](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks) and then edit your answer to fix it perhaps? – Flexo Oct 22 '11 at 13:12