java - Liferay - 创建 PDF 并输出到流

标签 java jsf liferay

这里有点麻烦。基本前提是我需要点击一个按钮,生成 HTML,创建 PDF,然后放入输出流以供下载:

<ice:commandButton title="Download"
    image="/images/dl.png"
    value="Download"
    action="#{bean.downloadPDF}">        
</ice:commandButton>

public void downloadPDF() throws IOException {

    PD4ML pdf = new PD4ML();
    pdf.setPageSize(PD4Constants.LETTER);
    pdf.setPageInsets(new Insets(0, 0, 0, 0));
    pdf.setHtmlWidth(1000);
    pdf.enableImgSplit(false);
    pdf.generateOutlines(false);

    File pdfFile = new File("tmp.pdf");
    FileOutputStream fos = new FileOutputStream(pdfFile);

    StringReader sr = new StringReader("<p>Testing Download</p>");
    pdf.render(sr, fos);

    FacesContext facesContext = FacesContext.getCurrentInstance();
    PortletResponse portletResponse= (PortletResponse)facesContext.getExternalContext().getResponse();
    ResourceResponse portletResourceResponse = (ResourceResponse) portletResponse;
    portletResourceResponse.setContentType("application/pdf");

    OutputStream out = portletResourceResponse.getPortletOutputStream();
    out.flush();
    facesContext.responseComplete();
}

我遇到的问题是在 pdf.render() 之后,当我尝试根据当前上下文生成响应并转换为 ResourceResponse 时:

java.lang.ClassCastException: com.liferay.portlet.RenderResponseImpl cannot be cast to javax.portlet.ResourceResponse

获取该文件并将其输出到 Liferay/portlet 中的正确方法是什么?

最佳答案

你得到的异常,java.lang.ClassCastException: com.liferay.portlet.RenderResponseImpl cannot be cast to javax.portlet.ResourceResponse 听起来你有一些类(例如 portlet.jar)在你的类路径上两次。这通常位于全局类路径中,您不得将其包含在您的 Web 应用程序中。

当你有像 subclass cannot be cast to superclass 这样的异常时,几乎总是这样

关于java - Liferay - 创建 PDF 并输出到流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14839713/

相关文章:

jsf - 如何调用 p :commandButton in the error page shown by FullAjaxExceptionHandler?

css - 主题更改未显示在 liferay 中

jsf - 从 JBoss 更改为 Tomcat 后,Liferay portlet 未部署

java - Maven 构建抛出 java.lang.NoClassDefFoundError

java - 正确显示带线程的 Sprite

java - 使用LdapContextSource的Spring LDAP为com.sun.jndi.ldap.ctl.ResponseControlFactory提供NoClassDefFoundError

javascript - 当 JavaScript 将 disabled 属性更改为 false 时,Commandbutton 不会调用操作

java - 使用 Java 和 Jasper 生成 Excel 文件

java - 如何在 portlet 中使用 Liferay 内部代码?

Java - 从多个对象中读取