<f:event listener="#{basicDocumentViewerController.onPrerender}" type="preRenderView"/> <p:outputPanel deferred="true"> <p:tabView> <p:tab title="URL"> <pe:documentViewer height="500" url="/sections/documentviewer/book.pdf"/> </p:tab> <p:tab title="Resource"> <pe:documentViewer height="500" library="books" name="hood.pdf" zoom="page-width"/> </p:tab> <p:tab title="StreamedContent"> <pe:documentViewer height="500" value="#{basicDocumentViewerController.content}" zoom="page-width"/> </p:tab> </p:tabView> </p:outputPanel>
@Named @SessionScoped public class BasicDocumentViewerController implements Serializable { private static final long serialVersionUID = 1L; private String downloadFileName = "pfe-rocks.pdf"; private StreamedContent content; public void onPrerender(final ComponentSystemEvent event) { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final Document document = new Document(); PdfWriter.getInstance(document, out); document.open(); for (int i = 0; i < 50; i++) { document.add(new Paragraph("All work and no play makes Jack a dull boy")); } document.close(); content = DefaultStreamedContent.builder().stream(() -> new ByteArrayInputStream(out.toByteArray())) .contentType("application/pdf").name("jack.pdf").build(); } catch (final Exception e) { } } public StreamedContent getContent() { return content; } public void setContent(final StreamedContent content) { this.content = content; } public String getDownloadFileName() { return downloadFileName; } public void setDownloadFileName(final String downloadFileName) { this.downloadFileName = downloadFileName; } }