【问题标题】:How to show generated PDF in JSP page?如何在 JSP 页面中显示生成的 PDF?
【发布时间】:2018-09-12 09:56:36
【问题描述】:

我有一个 Spring MVC 应用程序,部署到 Apache Tomcat。其中一页必须显示使用 itext pdf 库生成的 PDF 文件。

所以我在 JSP 文件中添加了object 标签:

<object data="<c:url value="/view-pdf" />"></object>

我在控制器内部有处理这个 URL 的方法:

@RequestMapping(value = "/view-pdf", method = RequestMethod.GET)
protected void viewPdf(HttpServletResponse response) {

    ServletOutputStream out = response.getOutputStream();

    //generate pdf here
    Document document = new Document();
    PdfWriter.getInstance(document, out);
    document.setPageSize(PageSize.A4);
    document.open();
    document.add(new Paragraph("Hello, World"));
    document.close();

    out.close();    
}

现在,当我打开应该显示 PDF 的页面时,它不显示 PDF 文件。 Chrome 控制台显示此错误:

Refused to display 'http://localhost:8080/MyApp/view-file' in a frame because it set 'X-Frame-Options' to 'deny'.

直接在地址栏中输入http://localhost:8080/MyApp/view-pdf URL 即可访问PDF。所以生成PDF没有问题。

这里的一些答案建议将这些行添加到web.xml 文件中:

    <filter>
        <filter-name>httpHeaderSecurity</filter-name>
        <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>antiClickJackingEnabled</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>antiClickJackingOption</param-name>
            <param-value>ALLOW-FROM</param-value>
        </init-param>
        <init-param>
            <param-name>antiClickJackingUri</param-name>
            <param-value>http://localhost:8080/MyApp/*</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>httpHeaderSecurity</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

我这样做了,但完全没有效果。我在这里做错了什么?如何避免这个错误?

我的 Spring 版本是 5.0.4.RELEASE,Tomcat 版本是 8.0.48。

【问题讨论】:

    标签: spring-mvc pdf tomcat itext


    【解决方案1】:

    问题在于 Spring 安全性中的“X-Frame-Options”响应标头。 检查您的 spring 安全配置 - 因为出于安全原因,默认情况下它设置为拒绝 - 请参阅下面的链接以获取提供的选项。

    How to disable 'X-Frame-Options' response header in Spring Security?

    【讨论】:

    • 您先生,救了我几个小时的生命和一半的神经。谢谢!
    【解决方案2】:

    把 viewPdf 方法改成这样怎么样:

    @RequestMapping(value = "/view-pdf", method = RequestMethod.GET)
    protected void viewPdf(HttpServletResponse response) {
    
        ServletOutputStream out = response.getOutputStream();
        // The next line could fix your problem
        response.setHeader("X-Frame-Options", "SAMEORIGIN");
        //generate pdf here
        Document document = new Document();
        PdfWriter.getInstance(document, out);
        document.setPageSize(PageSize.A4);
        document.open();
        document.add(new Paragraph("Hello, World"));
        document.close();
    
        out.close();    
    }
    

    【讨论】:

    • 也许您必须在方法中设置 X-Frame-Options 标头,从而将用户引导至带有 &lt;object&gt;-Tag 的 js
    • 很遗憾,它没有用。我可以在 Chrome DevTools 中看到 X-Frame-Options 选项标头设置为 SAMEORIGIN。但它仍然给出同样的错误。
    • 我尝试在一个空的演示应用程序中显示 PDF。没有错误。所以看起来应用程序配置有问题。我会尝试更深入地挖掘。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2021-08-10
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多