这里有点麻烦。基本前提是我需要点击一个按钮,生成 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 的正确方法是什么?