gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:48:41 25 4
gpt4 key购买 nike

这里有点麻烦。基本前提是我需要点击一个按钮,生成 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com