html - 如何在 JSF 页面上显示 PDF

标签 html jsf pdf

我想在我的 JSF 页面上显示一个 PDF 文件,我已经检查了这个 how to display a pdf document in jsf page in iFrame ,但我不想在 iframe 上显示它(因为它会生成滚动条)。我只想将 pdf 显示到像图像一样的页面上,并能够为其提供宽度和高度。

编辑 你好 BalusC。我仍然无法内联显示 pdf。这是我的代码。

@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();
        }
    }
    ...
}

它仍然提示我下载 pdf 文件。下载到我电脑上的 pdf 文件是正确的 pdf 文件 btw。你能发现什么问题吗?

最佳答案

实际上并没有其他方法(期待 HTML <object> 标签会有同样的“问题”),但你可以只给出 <iframe>固定大小并禁用滚动,如下所示:

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

如果您还想隐藏(默认)边框,请添加 frameBorder="0"

关于html - 如何在 JSF 页面上显示 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5746283/

相关文章:

c# - 将 pdf 文件存储在数据库中

html - 如何让一个 CSS 类制作两个文本行,如标题和副标题?

java - web.xml 错误页面不工作,为什么?

python - 用python删除文件夹和子文件夹中的pdf文件?

html - Weasyprint 字体问题导致所有字符被框替换

java - map 的线程安全性,在应用程序范围的 bean 中私下使用,并且仅在构建后用于准备目的

javascript - 如何在chrome或android浏览器等移动网络上隐藏地址栏?

javascript - 如何从 HTML 中获取 Base64 字符串

html - CSS 在另一个 div 中 float div

java - Primefaces p :ajaxStatus onerror not called