Discussions

Web tier: servlets, JSP, Web frameworks: opening pdf file in servlet

  1. opening pdf file in servlet (3 messages)

    Hi
    I am opening a PDF file using servlet (output stream).
    It opens a separate application in the acrobat reader7.0.
    But the file name is randomly gnerated. i don't know why it gives random names (like AcrCD.tmp.pdf).

    My code

    res.setHeader("Content-length",fileLength);
    res.setHeader("Content-type","application/pdf");
    res.setHeader("Content-disposition","inline; filename=" + fileName );

    res.setContentType("application/pdf");
    res.setContentLength((int)file.length());
    OutputStream outstream = res.getOutputStream();
    while((len=inputstream.read(buffer))>0)
    {
    outstream.write(buffer,0,len);
    }
    inputstream.close();
    outstream.flush();
    outstream.close();

    It is not opening with the filename.pdf, it opens with acr01.tmp.pdf
    Can any one help regarding this one.

    Threaded Messages (3)

  2. Check the file name. The file name must conform to the target machine's local file system conventions. After all this is only a suggestion, it does not guarantee that the file name you give will be used. If the target system decides that the filename represents some security problems (for whatever reasons) it may decide to rename the file. You will have better luck by using "attachment" instead of "inline" (it may not fit your requirements, though).

    This is what the RFC (rfc 2183) that describes this header says:

    "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."
  3. I don't know what browser you're using and wonder why it opens separate application (not embedded in browser) when Content-disposition is inline, butt ...

    i. use rather: res.setHeader("Content-disposition","inline; filename=\"" +fileName+"\"");

    without eg. in Firefox names containing whitespaces are cut to first word + extension

    ii. try change inline to attachment

    thus browser will ask what to do with the file (what imho is correct behaviour - eg. pdf is 18 megs)

    For embedded Acrobat I've noticed that it gives file the name of service + extension when saved.
    eg. http://localhost:8080/StreamData.xml?sp=l23&sp=Satt to StreamData.pdf

    Service roughly is equivalent for servlet in Tapestry.

    I know it is not THE answer :)

    --pawel
  4. Checkout following blog post that contains a good example on streaming pdf using servlet.

    Checkout this link