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

问题描述:

有人可以帮我在iframe的JSF页面中显示PDF文档吗?

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

提前致谢,

Suresh

只需使用< iframe> 通常的方式:

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

如果您的问题不在于PDF不在 WebContent ,而是位于磁盘文件系统中的其他位置,甚至位于数据库中,那么你基本上需要一个 Servlet 获得它的InputStream 并将其写入响应的 OutputStream

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);
}

这样你只需指向这个servlet :)例如:

This way you can just point to this servlet instead :) E.g.:

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

您可以在这篇文章

< iframe> 在JSF中运行正常,假设您使用的是JSF 1.2或更新版本。在JSF 1.1或更早版本中,您必须在< f:verbatim> $ c中包含普通的HTML元素,例如< iframe> $ c>以便它们将被带入JSF组件树,否则它们将在输出中脱位:

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>