[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Printing a PDF document  XML
Forum Index -> Development - Code Snippets
Author Message
Anonymous



First integrate a download component to your JSP, e.g. t:filedownloadbutton
Code:
<t:filedownloadbutton
 	fileextensions="pdf" 
 	filename="report.pdf" 
 	openimmediately="true" 
 	opensupported="true" 
 	text="Print PDF" 
 	url="#{d.PrintUI.printReportUrl}"
 />


In the referenced managed bean PrintUI, coding could look like that
Code:
public class PrintUI extends WorkpageDispatchedBean implements Serializable {
 	DefaultBufferedContent report;
 	...
 
 	// Constructor
 	public PrintUI(IWorkpageDispatcher dispatcher) {
 		super(dispatcher);
 		...
 		// add print functionallity
 		report = new MyReport();
 		// set close operation - to remove report from memory
 		getWorkpage().addLifecycleListener(
 				new WorkpageDefaultLifecycleListener() {
 					public void reactOnDestroyed() {
 						super.reactOnDestroyed();
 						if(report!=null) BufferedContentMgr.remove(report);
 					}
 				});		
 
 	}
 
 	public String getPrintReportUrl() { return report.getURL(); }
 
 	private static class MyReport extends DefaultBufferedContent {
 		@Override
 		public byte[] getContent() {
 			ByteArrayOutputStream out = new ByteArrayOutputStream();
 			// build PDF with iText			
 			Document document = new Document();
 			PdfWriter writer = PdfWriter.getInstance(document, out);
 			document.open();
 			// create content
 			...
 			document.close();
 			return out.toByteArray();				
 		}
 
 		@Override
 		public String getContentType() {
 			return "application/pdf";
 		}
 	}
 }


I didn't test the coding, but I hope there's no fatal error
 
Forum Index -> Development - Code Snippets
Go to:   
Powered by JForum 2.1.6 © JForum Team