开发者

how to display a pdf document in jsf page in iFrame

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

Thanks in advance,

开发者_C百科

Suresh


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜