java - Primefaces InputStream generating corrupted PDF in fileDownload -


i need in solving issue of generating corrupted pdf file inputstream used in filedownload component. having pdf generated using itext , converting inputstream, can use input in filedownload component. after clicking on download commandbutton, download file , open it, show message:

the adobe reader can not open file "sample.pdf" because either not supported file type or damaged

the bean code is:

private streamedcontent file;  public void createpdf() {     try {         document doc = document(pagesize.a4, 50, 50, 50, 50);         bytearrayoutputstream out = new bytearrayoutputstream();         bytearrayinputstream in ;         pdfwriter writer;         writer = pdfwriter.getinstance(doc, out);         doc.open();         doc.add(new paragraph("hello world!"));         in = new bytearrayinputstream(out.tobytearray());         file = new defaultstreamedcontent(in, "application/pdf", "sample.pdf");     } catch (exception e) {         e.printstacktrace();     } } 

and jsf code is:

<p:commandbutton value="download" ajax="false"         onclick="primefaces.monitordownload(start, stop);"         icon="ui-icon-arrowthick-1-s" actionlistener="#{pdf.createpdf}">     <p:filedownload value="#{pdf.file}"/> </p:commandbutton> 

so how fix issue

shouldn't there doc.close() before start coping bytes out? file corrupted if save hard drive instead of sending browser?


Comments