简体   繁体   English

如何在iFrame的jsf页面中显示pdf文档

[英]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? 任何人都可以帮我在iframe的JSF页面中显示PDF文档吗?

Thanks in advance, 提前致谢,

Suresh 苏雷什

Just use <iframe> the usual way: 只需按常规方式使用<iframe>

<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: 如果您的问题不是PDF不在WebContent ,而是位于磁盘文件系统中的其他位置,甚至位于数据库中,那么您基本上需要一个Servlet来获取它的InputStream并将其写入到OutputStream 。响应:

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 :) Eg: 这样你只需指向这个servlet :)例如:

<iframe src="/path/to/servlet/file.pdf"></iframe>

You can find a complete example of a similar servlet in this article . 您可以在本文中找到类似servlet的完整示例。

The <iframe> also works fine in JSF, assuming that you're using JSF 1.2 or newer. 假设您使用的是JSF 1.2或更新版本, <iframe>在JSF中也可以正常工作。 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: 在JSF 1.1或更早版本中,您必须在<f:verbatim>包含普通的HTML元素,例如<iframe> <f:verbatim>以便将它们放入JSF组件树中,否则它们将在输出中脱位:

<f:verbatim><iframe src="/path/to/servlet/file.pdf"></iframe></f:verbatim>

I recommend you to have a look at http://www.jpedal.org/ . 我建议你看看http://www.jpedal.org/ You can convert each of the pdf pages to images and deliver them separately to the browser. 您可以将每个pdf页面转换为图像,并将它们单独传送到浏览器。

This approach is more secure for your application, since the pdf is never send to the client. 这种方法对您的应用程序更安全,因为pdf永远不会发送到客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM