views:

38

answers:

1

I have a web application that can display a generated PDF file to the user using the following Java code on the server:

@Path("MyDocument.pdf/")
@GET
@Produces({"application/pdf"})
public StreamingOutput getPDF() throws Exception {
    return new StreamingOutput() {
        public void write(OutputStream output) throws IOException, WebApplicationException {
            try {
                PdfGenerator generator = new PdfGenerator(getEntity());
                generator.generatePDF(output);
            } catch (Exception e) {
                logger.error("Error getting PDF file.", e);
                throw new WebApplicationException(e);
            }
        }
    };
}

This code takes advantage of the fact that I only need so much data from the front end in order to generate the PDF, so it can easily be done using a GET function.

However, I now want to return a PDF in a more dynamic way, and need a bunch more information from the front end in order to generate the PDF. In other areas, I'm sending similar amounts of data and persisting it to the data store using a PUT and @FormParams, such as:

@PUT
@Consumes({"application/x-www-form-urlencoded"})
public void put(@FormParam("name") String name,
                @FormParam("details") String details,
                @FormParam("moreDetails") String moreDetails...

So, because of the amount of data I need to pass from the front end, I can't use a GET function with just query parameters.

I'm using Dojo on the front-end, and all of the dojo interactions really don't know what to do with a PDF returned from a PUT operation.

I'd like to not have to do this in two steps (persist the data sent in the put, and then request the PDF) simply because the PDF is more "transient" in this uses case, and I don't want the data taking up space in my data store.

Is there a way to do this, or am I thinking about things all wrong?

Thanks.

A: 

I can't quite understand what do you need to accomplish - looks like you want to submit some data to persist it and then return pdf as a result? This should be straightforward, doesn't need to be 2 steps, just submit, on the submit save the data and return PDF. Is this your problem? Can you clarify?

P.S. Ok, you need to do the following in your servlet:

response.setHeader("Content-disposition", "attachment; filename=" + "Example.pdf" );

response.setContentType( "application/pdf" );

Set the "content-length" on the response, otherwise the Acrobat Reader plugin may not work properly, ex. response.setContentLength(bos.size());

If you provide output in JSP you can do this:

<%@ page contentType="application/pdf" %>

Romario
To clarify, that is what I'm trying to do. However, with my first request, I don't post any data except what is in the query parameters, so it's really just going to a URL, and letting the browser deal with the fact that it's returning a PDF. With a PUT (or a POST), how do I deal with the return type of a PDF?
Dante617
I have the back end generating the PDF correctly, and it's going back to the front-end. The difficulty I'm having is in dealing with it through JavaScript. Usually, when I do a POST or a PUT (using dojo.xhr* commands), I get back a response that has text or xml or JSON. Dojo deals with that and gives me back a nice object I can do something with. With PDF, I get an error on the response that handleAs isn't defined for PDF. Looking at the Dojo doc, there aren't options for handling responses as PDF.
Dante617
You could use dojo.io.bind, you can read more here: http://dojo-toolkit.33424.n3.nabble.com/export-table-data-to-save-in-excel-format-td168638.html and here http://o.dojotoolkit.org/forum/support/general-support/can-io-bind-handle-mimetypes-application-vnd-ms-excel . It is about excel files, just change the filetype to pdf
Romario
You can use either a separate window to read the PDF into or you can create an IFrame and set that as the target.
Romain Hippeau