0
votes

so I am working on a school project in which we have designed a web application that takes in much user info and creates a pdf then should display that pdf to the user so they can print it off or save it. We are using Play! Framework 2.1.3 as our framework and server and Java for the server side. I create the pdf with Apache's PDFbox library. Every thing works as it should in development mode ie launching it on a localhost with plays run command. the issue is when we put it up to the server and launch with plays start command I it seems to take a snapshot of the directory (or at least the assets/public folder) which is where I am housing the output.pdf file/s (i have attempted to move the file elsewhere but that still seems to result in a 404 error). Initially I believed this to be something with liunx machine we were deploying to which was creating a caching problem and have tried many of the tricks to defeat the browser from caching the pdf like using javascript to append on a time stamp to the filename,

using this cache-control directive in the play! documentation, "assets.cache./public/stylesheets/output.pdf"="max-age=0",

then I tried to just save the pdf as a different filename each time and pass back the name of that file and call it directly through the file structure in the HTML which also works fine with the run command but not the start.

finally I came to the conclusion that when the start command is issued it balls up the files so only the files that are there when the start command is issued can be seen.

I read the documentation here http://www.playframework.com/documentation/2.1.x/Production

which then I noticed this part

When you run the start command, Play forks a new JVM and runs the default Netty HTTP server. The standard output stream is redirected to the Play console, so you can monitor its status.

so it looks like the fact that it forks a new JVM is what is causing my pain.

so my question really is can this be gotten around in some way that a web app can create and display a pdf form? (if I cannot get this to work my only solution that I can see is that I will have to simulate the form with HTML and fill it out from there) --which I really think is a bad way to do this.

this seems like something that should have a solution but I cannot seem to find or come up with one please help.

i have looked here:

http://www.playframework.com/documentation/2.1.x/JavaStream

the answer may be in there but Im not getting it to work I am pretty novice with this Play! Framework still

1

1 Answers

1
votes

You are trying to deliver the generated PDF file to the user by placing it in the assets directory, and putting a link to it in the HTML. This works in development mode because Play finds the assets in the directory. It won't work in production because the project is wrapped up into a jar file when you do play dist, and the contents of the jar file can't be modified by the Play application. (In dev mode, Play has a classpath entry for the directory. In production, the classpath points to the jar file).

You are on the right lines with JavaStream. The way forward is:

  1. Generate the PDF somewhere in your local filesystem (I recommend the temp directory).
  2. Write a new Action in your Application object that opens the file you generated, and serves it instead of a web page.

Check out the Play docs for serving files. This approach also has the advantage that you can specify the filename that the user sees. There is an overloaded function Controller.ok(File file, String filename) for doing this. (When you generate the file, you should give it a unique name, otherwise each request will overwrite the file from a previous request. But you don't want the user to see the unique name).