Thang Pham
Thang Pham

Reputation: 38705

How do I display a PDF onto a JSF page

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?

Upvotes: 1

Views: 18786

Answers (4)

Adam
Adam

Reputation: 5070

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

Upvotes: 2

fdreger
fdreger

Reputation: 12495

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.

Upvotes: 1

maple_shaft
maple_shaft

Reputation: 10463

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1108762

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.

Upvotes: 5

Related Questions