How to show PDF on JSF page

I want to display a PDF file on my JSF page, I checked this how to display a pdf document on a jsf page in an iFrame , but I do not want to display it on an iframe (since it will generate a scroll bar). I just want to display pdf on the page as an image and give it a width and height.

EDIT Hi BalusC. I still cannot display 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();
        }
    }
    ...
}

He still offers me to download the pdf file. The pdf file that is downloaded to my computer is the correct pdf file. Can you have something wrong?

+1
source share
4 answers

( HTML <object>, "" ), <iframe> :

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

( ), frameBorder="0".

+4

ICEpdf, , ().

+2

Adobe Reader, "" -, , , PDF .

, , PDF.

+1

What you want is impossible. Browsers are not magical, they simply display various types 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 thumbnails, you will have to create images on the server.

+1
source

Source: https://habr.com/ru/post/1726233/


All Articles