-3
如何使用iText在內存中創建一個PDF文件,然後將對象顯示在對話框的primefaces中,用戶可以根據需要下載報告。 這是我的代碼:在內存中創建一個PDF文件
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>
這個問題似乎是脫離主題,因爲它是一個「show me teh codez」問題。 – 2014-10-17 08:08:19