【问题标题】:Can't create a PDF with iText and JSF无法使用 iText 和 JSF 创建 PDF
【发布时间】:2011-09-30 08:26:15
【问题描述】:

我想在我的 JSF + Spring Web 应用程序中使用 iText 创建一个 pdf。 当我单击一个按钮时,应该会生成 pdf。被触发的方法:

public void createPDF() {
    log.debug("entered createPDF");
    FacesContext context = FacesContext.getCurrentInstance(); 
    HttpServletResponse response = (HttpServletResponse)context.getExternalContext().getResponse();  
    response.setContentType("application/pdf");  
    response.setHeader("Content-disposition",  "inline=filename=file.pdf");
    try {

        // Get the text that will be added to the PDF
        String text = "test";
        // step 1
        Document document = new Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph(text));
        // step 5
        document.close();

        // setting some response headers
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control",
            "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        // setting the content type
        response.setContentType("application/pdf");
        // the contentlength
        response.setContentLength(baos.size());
        // write ByteArrayOutputStream to the ServletOutputStream
        OutputStream os = response.getOutputStream();
        baos.writeTo(os);
        os.flush();
        os.close();
        log.debug("flushed and closed the outputstream");

    }
    catch(DocumentException e) {
        log.error("error: "+e);
    }
    catch (IOException e) {
        log.error("error: "+e);
    }
    catch (Exception ex) {
        log.debug("error: " + ex.getMessage());
    }
    context.responseComplete();
    log.debug("context.responseComplete()");
}

这是带有按钮的页面:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:s="http://jboss.org/seam/faces"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    template="/pages/layout/layout.xhtml">

<ui:define name="content">
        <h:form>
            <rich:panel style="width: 785px; height: 530px; ">
<a4j:commandButton value="Afdrukken" execute="@form"
                    action="#{huishoudinkomenAction.print}" style="float:right;" />

        </rich:panel>
    </h:form>
</ui:define>

我在日志中看到调试消息,但 Web 应用程序没有任何反应。我没有看到pdf。 我做错了什么?

问候,

德克

编辑: 当我将 &lt;a4j:commandButton /&gt; 更改为 &lt;h:commandButton /&gt; 时,它起作用了。

【问题讨论】:

    标签: spring jsf jsf-2 itext


    【解决方案1】:

    我从未使用过 RichFaces,但使用 Primefaces 控件,您可以设置属性 ajax="false"。

    <p:commandButton id="someid" value="Text for user" action="someConfiguredAction"  ajax="false"/>
    

    <h:commandButton id="someid" value="Text for user" action="someConfiguredAction"/>
    

    【讨论】:

    • (糟糕,这应该是对 bogdan.mustiata 答案的补充)
    【解决方案2】:

    当您使用&lt;a4j:commandButton&gt; 时,将在您的浏览器上创建一个新的 XmlHttpRequest,并通过 JS 调用您的服务器端方法。输出的 PDF 会写入输出流,但实际结果会从 XmlHttpRequest 中读出,并由jsf.ajax.response() javascript 函数解释。

    由于 JSF ajax 响应始终是根为 &lt;partial-response&gt; 的 XML,因此您基本上是将垃圾发送回 JSF ajax 处理程序。 (PDF != XML 与 &lt;partial-response&gt; 根)。显然,这无法解析,因此看起来“什么都没有发生”。

    所以你必须使用&lt;h:commandButton/&gt; 来做一个真正的请求。您还需要这样做:

    response.setHeader("Content-disposition", "attachment; filename=mycool.pdf");
    

    服务器端通知浏览器它收到了一个新文件,应该下载它,而不是显示它而不是页面。

    这将具有“ajax”调用的最终行为,您在其中进行调用,您会收到响应(并保存它),但您的页面内容会保留在那里。

    【讨论】:

      【解决方案3】:

      您不能使用 ajax 下载文件。 Ajax 由 JavaScript 代码触发和处理。但是,出于明显的安全原因,JavaScript 无法强制 Save As 与 JavaScript 变量中的任意内容(例如 ajax 请求的响应)进行对话。

      确保下载按钮触发同步(非 ajax)请求。使用普通的命令按钮或在 ajax 命令按钮中关闭 ajax。

      【讨论】:

        猜你喜欢
        • 2011-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-04
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 2012-11-03
        相关资源
        最近更新 更多