Liferay-创建PDF并输出到流 [英] Liferay - Creating PDF and outputing to stream

查看:135
本文介绍了Liferay-创建PDF并输出到流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有点麻烦.基本前提是我需要按下一个按钮,生成HTML,创建PDF,并放入输出流进行下载:

Having a little trouble here. Basic premise is that I need to hit a button, generate HTML, create PDF, and throw into output-stream for download:

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

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

Problem I am having is after the pdf.render(), when I attempt to generate the response based on the current context and the convertion to ResourceResponse:

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

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

What is the proper way to take that file and output it in Liferay/portlet?

推荐答案

您收到的异常java.lang.ClassCastException: com.liferay.portlet.RenderResponseImpl cannot be cast to javax.portlet.ResourceResponse听起来像您在类路径中有两次类(例如portlet.jar).这通常在全局类路径中,并且您一定不能在Web应用程序中使用它.

The exception that you get, java.lang.ClassCastException: com.liferay.portlet.RenderResponseImpl cannot be cast to javax.portlet.ResourceResponse sounds like you have some classes (e.g. portlet.jar) twice on your classpath. This typically is in the global classpath and you must not have it in your web application.

当您遇到像subclass cannot be cast to superclass

这篇关于Liferay-创建PDF并输出到流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆