contentType "application/pdf" yields damaged pdf

1,471 views
Skip to first unread message

Jean Yang

unread,
Mar 5, 2012, 1:51:58 PM3/5/12
to scalat...@googlegroups.com
Hello Scalatra users,

  I am trying to serve a PDF file as follows in a ScalatraFilter get method:

contentType = "application/pdf"
org.apache.commons.io.
FileUtils.readFileToByteArray(new File(fileLocation))

Variable fileLocation is the location of the file on disk.  The resulting PDF file seems to be damaged--I suspect it has to do with setting contentType, as writing the generated byteArray back to file yields a perfectly fine PDF.

  Could someone tell me what I am doing wrong?  (Is there a better idiom for serving PDF files from disk that I should be using?)

Thanks,
Jean


--
Jean Yang
academic website | twitter


Ross A. Baker

unread,
Mar 5, 2012, 2:21:29 PM3/5/12
to scalat...@googlegroups.com
Nothing jumps out at me in terms of corruption, but there's a more
succinct and efficient way of rendering a file:

contentType = "application/pdf"
new File(fileLocation)

If an action returns a File, then Scalatra will render it via zero-copy.

--
Ross A. Baker
ba...@alumni.indiana.edu
Indianapolis, IN, USA

Jean Yang

unread,
Mar 5, 2012, 2:29:53 PM3/5/12
to scalat...@googlegroups.com
Hi Ross,

  Thanks for the quick response.  When I do this I still get the same error--broken PDF.  The file seems to be loading off disk fine since I'm able to read and write it as a well-formed PDF...  Could there be a permissions issue with displaying the file on the browser?

Thanks,
Jean

Ross A. Baker

unread,
Mar 5, 2012, 5:06:42 PM3/5/12
to scalat...@googlegroups.com
Is the file local? Does it render properly in the browser from a file:// URL?

What if it gets served by the default servlet? Put the file in
src/main/webapp/foo.pdf and make a request /foo.pdf. Assuming no
route matches, it should slide through the ScalatraFilter and be
served by the default servlet.

John Snow

unread,
Oct 1, 2013, 2:14:23 PM10/1/13
to scalat...@googlegroups.com
I am having a similar problem. It appears that when returning a File or byte array from an action, Scalatra is appending ";charset=UTF-8" to the content-type header. This is a problem for any non-text files or responses. If the file is placed in src/main/webapp and is served by the default servlet, the charset parameter is not appended to the content-type header. Is there any way around this?

Ross A. Baker

unread,
Oct 1, 2013, 4:44:54 PM10/1/13
to scalat...@googlegroups.com
Scalatra sets the default response encoding to UTF-8 to work around the servlet specification's unfortunate default of ISO-8859-1.  You can suppress this behavior:

    before() { request.characterEncoding = None }

Note that if you do this, the servlet container will append ";charset=ISO-8859-1" to text MIME types unless you explicitly set your own encoding for text responses -- but it will leave your appplication/* alone.





--
You received this message because you are subscribed to the Google Groups "scalatra-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalatra-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages