1 25 package org.ofbiz.widget.screen; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.StringWriter ; 30 import java.io.Writer ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.apache.avalon.framework.logger.Log4JLogger; 36 import org.apache.avalon.framework.logger.Logger; 37 import org.apache.fop.apps.Driver; 38 import org.apache.fop.image.FopImageFactory; 39 import org.apache.fop.messaging.MessageHandler; 40 import org.apache.fop.tools.DocumentInputSource; 41 import org.ofbiz.base.util.Debug; 42 import org.ofbiz.base.util.UtilXml; 43 import org.ofbiz.webapp.view.ViewHandlerException; 44 import org.w3c.dom.Document ; 45 import org.xml.sax.InputSource ; 46 47 55 public class ScreenFopPdfViewHandler extends ScreenWidgetViewHandler { 56 57 public static final String module = ScreenFopPdfViewHandler.class.getName(); 58 59 62 public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException { 63 Writer writer = new StringWriter (); 65 try { 66 ScreenRenderer screens = new ScreenRenderer(writer, null, htmlScreenRenderer); 67 screens.populateContextForRequest(request, response, servletContext); 68 screens.render(page); 69 } catch (Throwable t) { 70 throw new ViewHandlerException("Problems with the response writer/output stream", t); 71 } 72 if (Debug.verboseOn()) { 73 } 75 76 Logger logger = new Log4JLogger(Debug.getLogger(module)); 78 MessageHandler.setScreenLogger(logger); 79 80 Driver driver = new Driver(); 82 driver.setRenderer(Driver.RENDER_PDF); 83 driver.setLogger(logger); 84 85 95 Document xslfo = null; 97 try { 98 xslfo = UtilXml.readXmlDocument(writer.toString()); 99 } catch (Throwable t) { 100 throw new ViewHandlerException("Problems reading the parsed content to XML Document", t); 101 } 102 103 ByteArrayOutputStream out = new ByteArrayOutputStream (); 105 driver.setOutputStream(out); 106 107 InputSource is = new DocumentInputSource(xslfo); 109 driver.setInputSource(is); 110 try { 111 driver.run(); 112 FopImageFactory.resetCache(); 113 } catch (Throwable t) { 114 throw new ViewHandlerException("Unable to generate PDF from XSL-FO", t); 115 } 116 117 response.setContentType("application/pdf"); 119 response.setContentLength(out.size()); 120 121 try { 123 out.writeTo(response.getOutputStream()); 124 response.getOutputStream().flush(); 125 } catch (IOException e) { 126 throw new ViewHandlerException("Unable write to browser OutputStream", e); 127 } 128 } 129 } 130 | Popular Tags |