Liferay - Creating PDF and outputing to stream

我怕爱的太早我们不能终老 提交于 2020-01-14 04:12:19

问题


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

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

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


回答1:


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.

This is almost always the case when you have an exception like subclass cannot be cast to superclass




回答2:


Try calling serveResource() in a portlet. when you hit a button, add ajax and call resource url which will serveResource.

HTH




回答3:


I would recommend that you look at the jsf2-export-pdf-portlet demo. It uses a JSF 2.x ResourceHandler in order to return a PDF using the RESOURCE_PHASE of the portlet lifecycle.



来源:https://stackoverflow.com/questions/14839713/liferay-creating-pdf-and-outputing-to-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!