Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-mvc (portlet): how to return a pdf file in open file dialog?

In my @ActionMapping I create a PDF file for the user. Now I was wondering how I can return this pdf to the user in the form of a save/open file dialog box? I'd prefer this over showing a download link if the generation was succesful.

I'm using spring-mvc 3.0.5 in combination with portlets. But if anyone has some pointers for a normal application then I can probably figure it out from there. For 2.0 I read something about extending a pdfgenerator class and twidling in the web.xml but since nowadays we just need POJO's....


Edit: Code after Adeel's suggestion:

File file = new File("C:\\test.pdf");
        response.setContentType("application/pdf");

        try {
            byte[] b = new byte[(int) file.length()];
            OutputStream out = response.getPortletOutputStream();
            out.write(new FileInputStream(file).read(b));
            out.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "users/main";
like image 274
jack Avatar asked Jan 04 '11 10:01

jack


2 Answers

You can write that file directly to your response writer, and don't forget to change the contentType. For example,

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=something.pdf");
OutputStream out = response.getOutputStream();
out.write(pdfFileContentInBytes);
out.flush();                   

Well, I thought its a HttpServletResponse what you have, but its not the case. As you are working with Portlet, its a RenderResponse object. After searching over the Internet, I found few links which might be helpful to you in this regard.

  • First take an example of Lotus Form Server Portlet, its showing the way how to allow multiple mime-type while configuring the portlet using portlet.xml.

  • Here is Spring Portlet docs, its showing how we configure portlet using portlet.xml. It has an XML element about mime-type, see if you can give the value, application/pdf, there.

Another idea is to change your parameter to ActionResponse response, instead of RenderResponse response. I am a bit blur here, not sure what is your super class? what method is it? etc....

To me, it seems that the problem is allowed/not-allowed mime-types for the portlet response.

like image 91
Adeel Ansari Avatar answered Oct 06 '22 03:10

Adeel Ansari


in spring mvc, ResourceResponse response

response.reset();
response.setContentType("application/pdf");
response.setProperty("Content-disposition", "attachment; filename=\"" +"example.pdf" +"\"");

InputStream fontInputStream = request.getPortletSession()
                .getPortletContext()
                .getResourceAsStream("/WEB-INF/classes/arial.ttf");
Document document = new Document(PageSize.A4, 40, 40, 40, 50);
PdfWriter writer = PdfWriter.getInstance(document,
response.getPortletOutputStream());
document.addAuthor("XYZ");
document.addTitle("ASDF");
document.open();
like image 28
PeterR Avatar answered Oct 06 '22 05:10

PeterR