在内存中创建PDF文件 [英] Create a PDF file in memory

查看:162
本文介绍了在内存中创建PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在内存中使用iText创建PDF文件,然后在对象中显示对话框素数,用户可以根据需要下载报告。
这是我的代码:

How to create a PDF file using iText in memory and then show in a object into dialog primefaces, the user can download the report if wanted. This is my code:

private StreamedContent file;

public StreamedContent getFile() {
    return file;
}

public void setFile(StreamedContent file) {
    this.file = file;
}



public void memoryReport() throws DocumentException {
    try {
        Document document = new Document(PageSize.LETTER);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer;
        writer = PdfWriter.getInstance(document, baos);
        document.addTitle("This is a title.");
        document.open();
        document.add(new Paragraph("This document was created successful"));
        document.close();
        writer.close();
        //String fileName = "Andy";
        InputStream stream = new ByteArrayInputStream(baos.toByteArray());
        //file = new DefaultStreamedContent(stream, "application/pdf", fileName);
        file=new DefaultStreamedContent(stream, "application/pdf");
    } catch (Exception ex) {
        ex.getMessage();
    }
}

XHTML:

<f:facet name="footer">
    <p:commandButton  value="valor" icon="ui-icon-check" oncomplete="reportModal.show()" action="#{reportMemory.memoryReport}" update="reportViewer"/>
</f:facet>
</h:panelGrid>
</div>
<p:dialog  resizable="false" closeOnEscape="true" appendTo="@(body)" modal="true" id="reportViewer" widgetVar="reportModal" width="1000px" height="630px">
<p:media cache="false" value="#{reportMemory.file}" width="100%"  height="600px" player="pdf"/>
</p:dialog> 


推荐答案

此解决方案,使用itext,primefaces media。

this solution, using itext, primefaces media.

 <p:dataTable widgetVar="tb1" id="tablaFact" var="item" selection="#{listadoFacturasMB.selectedFactura}" selectionMode="single"  paginator="true" 
   rows="20" rowKey="#{item.idFactura}" value="#{listadoFacturasMB.facturaUtilList}"> 

       <p:ajax event="rowSelect" update=":frm1:growl :frm1:dialog" oncomplete="PF('servDialog').show()" listener="#{listadoFacturasMB.createPDF}"/>                         

        <f:facet name="header">
            <h:outputText value="Listado de facturas (Cantidad: #{listadoFacturasMB.cantFact})"/>
        </f:facet>

        <p:column style="width:10%"  headerText="Nro" sortBy="noFactura" filterFunction="#{utilMB.filterByName}"  filterBy="noFactura" filterMatchMode="contains">                          
                        <h:outputText value="#{item.noFactura}"/>
        </p:column>                                                                                     
       </p:dataTable> 

             <p:dialog  resizable="false" closeOnEscape="true" appendTo="@(body)" modal="true" id="dialog" header="Detalles de la factura" widgetVar="servDialog" width="1000px" height="630px">  
                <p:media cache="false" value="#{listadoFacturasMB.fileDownload}" width="100%"  height="600px" player="pdf">
                    <f:param name="id" value="#{listadoFacturasMB.idFile}" />
                </p:media>                   
            </p:dialog> 

支持bean:

private StreamedContent fileDownload;

  public void createPDF() {
    try {       
        Document pdf = new Document(PageSize.LETTER);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter writer;
        writer = PdfWriter.getInstance(pdf, baos);
        if (!pdf.isOpen()) {
            pdf.open();
        }

       //Adding content to pdf

        pdf.close();
        String fileName = "factura No " + this.selectedFactura.getNoFactura();

        InputStream stream = new ByteArrayInputStream(baos.toByteArray());
        fileDownload = new DefaultStreamedContent(stream, "application/pdf", fileName);

    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: createPDF() " + e.getMessage());
    }
}

这篇关于在内存中创建PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆