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?

Community
  • 1
  • 1
Thang Pham
  • 38,125
  • 75
  • 201
  • 285
  • 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? – Alba Mendez Apr 21 '11 at 17:28

4 Answers4

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 '11 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 '11 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 '11 at 17:12
  • yup. I switch to chrome, then it behave as expected. Dealing with pdf is actually a pain. – Thang Pham Apr 21 '11 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).

Divyesh Kanzariya
  • 3,629
  • 3
  • 43
  • 44
Adam
  • 5,045
  • 1
  • 25
  • 31
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.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
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.

fdreger
  • 12,264
  • 1
  • 36
  • 42
  • Thank you. Any recommendation on Java open sources API that convert pdf to image? – Thang Pham Apr 21 '11 at 17:21
  • PDF renderer library at http://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 '11 at 17:29
  • Where do you download the executable, the jar file? All I see is the source code. – Thang Pham Apr 21 '11 at 18:40
  • 1
    Uhmpf... it has been moved to the new infrastructure, and looks different. I think you will find the binaries here: http://swinglabs.org/downloads.jsp – fdreger Apr 22 '11 at 23:32
  • 1
    Here's an article that got me started with it: http://www.javaworld.com/javaworld/jw-06-2008/jw-06-opensourcejava-pdf-renderer.html – fdreger Apr 22 '11 at 23:38