【发布时间】:2021-06-16 21:02:35
【问题描述】:
当我尝试加载包含 primefaces 媒体 pdf 的页面时,未加载 PDF。 我在我的 postconstruct 中生成 PDF,并将流媒体内容保存在单独的变量中。 在我的 JSF 中,我调用了 getStream 方法来返回流式内容。
JSF 页面:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:composition template="/templates/header.xhtml">
<ui:define name="content">
<f:metadata>
<f:viewParam name="invoiceID" value="#{invoiceBean.invoiceID}"/>
</f:metadata>
<ui:param name="invoiceID" value="#{invoiceBean.invoiceID}"/>
<h4 style="text-align: center;"><h:outputText
value="#{msgs['invoice.thankYou']}"/></h4>
<div class="card">
<p:media value="#{invoiceBean.stream}" player="pdf" width="100%" height="800px">
Your browser can't display pdf,
<h:outputLink
value="#{invoiceBean.streamedContent}">click
</h:outputLink>
to download pdf instead.
</p:media>
</div>
</ui:define>
</ui:composition>
</html>
豆子:
@Model
@Getter
@Setter
public class InvoiceBean {
@Inject
InvoiceService invoiceService;
@Inject
HttpServletRequest httpServletRequest;
private Invoice invoice;
private String invoiceID;
private StreamedContent streamedContent;
@PostConstruct
public void initInvoice() {
User user = getLoggedInUser();
invoiceID = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("invoiceID");
invoice = invoiceService.getInvoice(Long.parseLong(invoiceID));
PDFGenerator pdf = new PDFGenerator(invoice);
streamedContent = pdf.getStreamedContent();
}
public StreamedContent getStream() throws IOException{
FacesContext context = FacesContext.getCurrentInstance();
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
return new DefaultStreamedContent();
} else {
return streamedContent;
}
}
}
【问题讨论】:
-
你可以只下载流媒体内容吗?
-
@JasperdeVries 它向我展示了一个像这样的空元素
:<object type="application/pdf" data="" height="800px" width="100%"> Your browser can't display pdf, <a href="">click </a> to download pdf instead. </object> -
我不会使用 p:media 我会使用 DocumentViewer 而不是它适用于所有浏览器:primefaces.org/showcase-ext/sections/documentviewer/basic.jsf
-
@Melloware streamedcontent 仍然为空我的 PDF 没有呈现它只是一直在旋转
-
问题更多是生命周期问题,我认为它是在生成 PDF 之前渲染它
标签: primefaces