// Access the <VO>Impl
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
AdfFacesContext context = AdfFacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
BindingContext ctx = DCUtil.getBindingContext(request);
DCDataControl dc = ctx.findDataControl("AppModuleDataControl");
ApplicationModule service = (ApplicationModule)dc.getDataProvider();
ApplicationModule am = service.findApplicationModule("AppModule");
AppModuleImpl amImpl = (AppModuleImpl)am;
ViewObjectImpl VOImlp = (EmpVOImpl)amImpl.findViewObject("EmpVO1");
HashMap viewDefMap = new HashMap();
// Print the XML
Node n = VOImlp.writeXML(XMLInterface.XML_OPT_ALL_ROWS, viewDefMap);
java.io.File file = new java.io.File(<path to save XML file>);
PrintWriter output = null;
try { output = new java.io.PrintWriter(file);
((XMLNode)n).print(output);
} catch (Exception e) { System.out.println(e.getMessage());
} finally {
try {output.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } }
// Generate PDF
File xmlfile = new File(<path to generated XML file>);
File xsltfile = new File(<path to styleSheet>);
File pdffile = new File(<path to save PDF file>);
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
TransformerFactory factory = new TransformerFactoryImpl();
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
transformer.setParameter("versionParam", "2.0");
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src, res);
} finally { out.close(); }
// download the PDF
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();
File file = new File(dir1, pdffile1);
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
response.reset();
response.setContentType("application/pdf");
response.setContentLength((int)file.length());
response.setHeader("Content-disposition","inline;filename=\""+pdffile1+"\"");
output = new BufferedOutputStream(response.getOutputStream(),DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length); }
output.flush(); } finally { close(output); close(input); }
facesContext.responseComplete();