1

I want to display a PDF file onto my JSF page, I have check this how to display a pdf document in jsf page in iFrame, but I dont want to display it on an iframe(since it will generate scroll bar). I just want to display the pdf onto a page like an image and able to give a width and height for it.

EDIT Hi BalusC. I still cant be able to display the pdf inline. Here is my code.

@WebServlet(name = "pdfHandler", urlPatterns = {"/pdfHandler/*"})
public class pdfHandler extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 10240;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String requestedFile = request.getPathInfo();
        File file = new File("/Users/KingdomHeart/Downloads/Test/pdf/" + requestedFile);
        response.reset();
        response.setContentType("application/pdf");
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setHeader("Content-Length", String.valueOf(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{
            output.close();
            input.close();
        }
    }
    ...
}

It still prompt me to download the pdf file. The pdf file that get downloaded to my computer is the correct pdf file btw. Can u spot anything wrong?

1
  • First, check you have the Adobe Reader plugin installed on your browser. Can you view PDF documents inside your browser, or it still asks for download? Apr 21, 2011 at 17:28

4 Answers 4

5

There's not really another way (expect from HTML <object> tag which would have the same "problems"), but you can just give the <iframe> a fixed size and disable the scrolling as follows:

<iframe src="foo.pdf" width="600" height="400" scrolling="no"></iframe>

If you also want to hide the (default) border, add frameBorder="0" as well.

4
  • When i display in an iframe, it does not display the pdf, it immediately prompt me to download the pdf file. Does convert pdf -> image is the only solution for this?
    – Thang Pham
    Apr 21, 2011 at 15:52
  • 3
    The Content-Disposition response header has to be set to inline (default) instead of attachment. See also the answer which you linked for the code line which does that.
    – BalusC
    Apr 21, 2011 at 15:53
  • 1
    As per your update: 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. Note that it does not do that by default, you must have ticked some remember option for that previously when getting a PDF file.
    – BalusC
    Apr 21, 2011 at 17:12
  • yup. I switch to chrome, then it behave as expected. Dealing with pdf is actually a pain.
    – Thang Pham
    Apr 21, 2011 at 17:22
2

You should take a look at ICEpdf, it creates an image on the server side, gives zooming, and other controls (demo).

2
  • Have u use this library before?
    – Thang Pham
    Apr 22, 2011 at 2:16
  • Not with the servlet part, only with the image conversion, but if you download the release it has a JSF (ICEfaces) webapp example.
    – Adam
    Apr 22, 2011 at 7:54
1

Try going into Adobe Reader, and under the Options dialog there are web settings where you can indicate that you always want PDF type documents to open within the browser.

This is unfortunately a client side fix and doesn't take into account other PDF readers.

1

What you want is impossible. Browsers are not magic, they just display different kinds of documents, some of which (HTML) can embed objects provided by plugins (flash, java) and other documents inside iframes (pdf, flash, html). If you want to show pdf miniatures, you will have to generate images on the server.

5
  • Thank you. Any recommendation on Java open sources API that convert pdf to image?
    – Thang Pham
    Apr 21, 2011 at 17:21
  • PDF renderer library at java.net/projects/pdf-renderer (from SwingLabs) used to work well (although I never used it to make thumbs, it should work).
    – fdreger
    Apr 21, 2011 at 17:29
  • Where do you download the executable, the jar file? All I see is the source code.
    – Thang Pham
    Apr 21, 2011 at 18:40
  • 1
    Uhmpf... it has been moved to the new infrastructure, and looks different. I think you will find the binaries here: swinglabs.org/downloads.jsp
    – fdreger
    Apr 22, 2011 at 23:32
  • 1
    Here's an article that got me started with it: javaworld.com/javaworld/jw-06-2008/…
    – fdreger
    Apr 22, 2011 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.