0

I learned that I can't just set a local URL to the object tag of the HTML, which led me to search a bit more. As a result, I saw this post which is really detailed and well documented. But I don't know if this is the proper approach for me to take and I really know nothing about JSF.

What I want to do is to send a PDF file in a web browser or simply embed a PDF file to the browser which is from the local path. Is there a way doing this in Spring? I used commons-fileupload in uploading the PDF to the local path as they say a good practice. Now the thing left is to display it.

By the way, I'm using Spring MVC if this will also help to clarify the approach I need.

Update:

What I learned so far is to use @ResponseBody to return the bytes, so I did try this:

@ResponseBody
@RequestMapping(value = "/get/pdf", produces="application/pdf")
public byte[] download( HttpServletResponse response ) throws IOException {

     response.setContentType( "application/pdf" );
     byte[] test = null;
     try
     {
          String path = new FileDAO().getFilePath( 1 );
          File file = new File( path );

          test = new byte[ ( int ) file.length() ];

          FileInputStream fileInputStream = new FileInputStream( file );
          fileInputStream.read( test );
     }
     catch( Exception e )
     {
          e.printStackTrace();
     }
     return test;
}

The result to the web page is like this.

result

I want to display the PDF file in object or iframe tag.

nikiforovpizza
  • 487
  • 1
  • 7
  • 13
user2785929
  • 985
  • 7
  • 13
  • 30
  • This smells like a incorrect Content-Type header. I updated my answer to set the Content-Type on the response. – Bart Oct 30 '13 at 08:16
  • I updated again the question. I added the content-type but it still producing same output. What I did is plainly access the URL through a link then return the bytes like you said. My guess is because it has no container? I want to display it in the `object` tag. How can I do that? – user2785929 Oct 30 '13 at 08:30
  • Can you post the response headers? Maybe the browser you're using does not understand PDF. In that case try [content-disposition](http://stackoverflow.com/questions/1012437/uses-of-content-disposition-in-an-http-response-header) – Bart Oct 30 '13 at 09:00
  • Is it the URL of the web? sorry for a noob question. I'm completely new to Spring and web development – user2785929 Oct 30 '13 at 09:06
  • I saw this [post](http://stackoverflow.com/questions/16232916/displaying-pdf-in-jsp) it looks like really close to what I need ( the answer of BalusC ). But I don't know how to integrate it on Spring. I think your approach and BalusC can be implemented together. – user2785929 Oct 30 '13 at 09:09
  • Just add the header `Content-Disposition: attachment; filename=my.pdf;` – Bart Oct 30 '13 at 09:14
  • Did I added it correctly `response.setHeader( " Content-Disposition: attachment;", " filename=Spring Tutorial.pdf" );`, It has the same result. – user2785929 Oct 30 '13 at 09:29

1 Answers1

0

It's a bit simpler with using Spring MVC then the suggestion in other post. With @ResponseBody you can write almost anything to the response by returning it from the controller.

@ResponseBody
@RequestMapping(value = "/get/pdf", produces="application/pdf")
public byte[] download(HttpServletResponse response) throws IOException {
    response.setContentType("application/pdf");
    return getPdfBytes();
}
Bart
  • 17,070
  • 5
  • 61
  • 80
  • Thank you for your response. Am I going to use JSF then? – user2785929 Oct 30 '13 at 06:36
  • No you can continue using spring. Where did you get that idea? – Bart Oct 30 '13 at 06:40
  • From the other post. It seems that he is using JSF. So what I'm going to do is: 1.access the controller just like the other normal controller, 2. get the bytes, and 3. return the bytes just like the answer you posted? – user2785929 Oct 30 '13 at 06:58
  • http://developers.itextpdf.com/question/how-can-i-serve-pdf-browser-without-storing-file-server-side – masber Mar 17 '17 at 13:50