5

Can anyone help me in displaying PDF document in JSF page in iframe only?

Thanks in advance,

Suresh

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Suresh
  • 38,717
  • 16
  • 62
  • 66

2 Answers2

11

Just use <iframe> the usual way:

<iframe src="/path/to/file.pdf"></iframe>

If your problem is rather that the PDF is not located in the WebContent, but rather located somewhere else in disk file system or even in a database, then you basically need a Servlet which gets an InputStream of it and writes it to the OutputStream of the response:

response.reset();
response.setContentType("application/pdf");
response.setContentLength(file.length());
response.setHeader("Content-disposition", "inline; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
    input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
    output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
    int length;
    while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
    }
} finally {
    close(output);
    close(input);
}

This way you can just point to this servlet instead :) E.g.:

<iframe src="/path/to/servlet/file.pdf"></iframe>

You can find a complete example of a similar servlet in this article.

The <iframe> also works fine in JSF, assuming that you're using JSF 1.2 or newer. In JSF 1.1 or older you have to wrap plain vanilla HTML elements such as <iframe> inside a <f:verbatim> so that they will be taken into the JSF component tree, otherwise they will be dislocated in the output:

<f:verbatim><iframe src="/path/to/servlet/file.pdf"></iframe></f:verbatim>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • correct me if I am wrong, you dont have to register servlet inside web.xml any in servlet 3.0, correct? I did this `@WebServlet(name="pdfHandler", urlPatterns={"/pdfHandler"})`, and try ``, but the servlet did not get called. Weird. Any idea why it behave like this, BalusC? – Thang Pham Apr 21 '11 at 16:16
  • 1
    @Harry: Then the URL is plain wrong. Did you take context path into account? Your example expects that your webapp is deployed on ROOT. Edit: oh wait, your URL pattern has to be `/pdfHandler/*` in order to accept URL with extra path info instead of request parameters. – BalusC Apr 21 '11 at 16:16
  • Yeah I actually have to use the whole url `http://localhost:8080/MyNote-war/pdfHandler`, i thought `/pdfHandler` would be enough – Thang Pham Apr 21 '11 at 16:29
  • Another question, so I try to get the file name using `request.getPathInfo()` like you suggested. I run the servlet on my web browser like this `http://localhost:8080/MyNote-war/pdfHandler`, then I got `null` return for my path, which is correct, but if I append the file name, the servlet no longer invoke anymore. So like this `http://localhost:8080/MyNote-war/pdfHandler/file.pdf`, the I got `the requested resource is not available`. Any idea, BalusC? – Thang Pham Apr 21 '11 at 16:36
  • 1
    @Harry: The URL pattern must be `/pdfHandler/*`. Did you notice the edit in my previous comment? – BalusC Apr 21 '11 at 16:38
  • That fixed it. Thanks. Sorry I did not see the edit. I see it now :D – Thang Pham Apr 21 '11 at 16:40
  • BalusC, when we display pdf inside an iframe with `width` and `height`, it does not scale the `pdf` correct? It will clip the file, right? – Thang Pham Apr 21 '11 at 16:42
  • @Harry: No, it does not scale the file. The default Adobe Reader plugin however accepts URL fragments which you can use as "parameters", like `src="/pdfHandler/file.pdf#zoom=50"` for a 50% zoom. You can find them all [here](http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf). Please note that you're dependent on the plugin this way. – BalusC Apr 21 '11 at 16:58
  • I need cant get it to display inline. It still prompt me to download the pdf file. I got the correct pdf file downloaded to my computer btw. I post my code here http://stackoverflow.com/questions/5746283/how-do-i-display-a-pdf-onto-a-jsf-page. You think you can take a look at it, BalusC? – Thang Pham Apr 21 '11 at 17:08
  • @Harry: Then it's a browser setting that it is forced to always save the PDF when any retrieved. Do you use Firefox? *Tools > Options > Applications*. – BalusC Apr 21 '11 at 17:11
  • BalusC, it works, thank you too. But can you advise me, how to force overwrite existing previously downloaded file (in order to save disk space)? Because each next copy of opened file has a new indew, e.g. test.pdf, test(1).pdf and so on... – gaffcz Dec 16 '11 at 08:46
1

I recommend you to have a look at http://www.jpedal.org/. You can convert each of the pdf pages to images and deliver them separately to the browser.

This approach is more secure for your application, since the pdf is never send to the client.

Dani Cricco
  • 8,829
  • 8
  • 31
  • 35
  • well they are not free though :D Actually quite expensive as well – Thang Pham Apr 21 '11 at 15:56
  • **"This approach is more secure for your application, since the pdf is never send to the client"** How come?! Isn't everything that is shown in the browser is actually downloaded to the client machine at the first time? And this include all the css, js, images, html files ! – Nabeel Jul 16 '12 at 20:20
  • Every page is rendered to jpg and the pdf is not "directly" accessible by the end-user. Of course, he might take advantage of the "analog hole" and download every single page. However, note that the user will have to traverse the whole document, and this might take a while. Moreover, you are able to register this information on the server and take appropriate actions. – Dani Cricco Jul 16 '12 at 20:58