当p:dataTable由p:dataExporter导出为PDF时,如何以PDF格式显示p:dataTable所持有的图片?

时间:2015-04-25 11:04:09

标签: image jsf pdf primefaces datatable

我想在PrimeFaces中的<h:form> <p:dataTable id="mydata" value="#{UserData.employees}" var="employee" > <f:facet name="header"> <p:graphicImage value="/images/icon/mellatlogo.png" style="height: 100px"/> </f:facet> <p:column> <f:facet name="header">Name</f:facet> <h:outputText value="#{employee.name}" /> </p:column> <p:column> <f:facet name="header">city</f:facet> <h:outputText value="#{employee.city}" /> </p:column> <p:column> <f:facet name="header">Date</f:facet> <h:outputText value="#{employee.date}" /> </p:column> </p:dataTable> <p:panel header="Export All Data"> <h:commandLink> <p:graphicImage value="/images/icon/pdf.jpg" /> <p:dataExporter type="pdf" target="mydata" fileName="mypdf" /> </h:commandLink> </p:panel> </h:form> 顶部显示一张图片。当我将其导出为PDF格式时,它在XHTML中工作但不起作用。反正有没有在PDF导出中显示图片和CSS样式?

这是我的XHTML页面。

{{1}}

1 个答案:

答案 0 :(得分:0)

将徽标添加到PDF 你必须在commandeButton中使用preProcessor attr

<h:commandLink>
            <p:graphicImage value="/images/icon/pdf.jpg" />
            <p:dataExporter type="pdf" target="mydata" fileName="mypdf" preProcessor="#{yourBean.yourPreProcFunction}" />
        </h:commandLink>

你的preProcess函数应该是,

        public void yourPreProcFunction(Object document) throws IOException, BadElementException, DocumentException{
    Document pdf = (Document) document;
ServletContext servletContext = (ServeletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String logo = serveletContext.getRealPath("")+File.separator+"images"+File.separator+"mellatlogo.png";
pdf.add(Image.getInstance(logo));

    }