001: /*
002: * $Id: FopPdfViewHandler.java,v 1.3 2003/09/08 20:42:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.view;
026:
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.StringWriter;
030: import java.io.Writer;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.avalon.framework.logger.Log4JLogger;
036: import org.apache.avalon.framework.logger.Logger;
037: import org.apache.fop.apps.Driver;
038: import org.apache.fop.messaging.MessageHandler;
039: import org.apache.fop.tools.DocumentInputSource;
040: import org.ofbiz.base.util.Debug;
041: import org.ofbiz.base.util.UtilXml;
042: import org.w3c.dom.Document;
043: import org.xml.sax.InputSource;
044:
045: /**
046: * Uses XSL-FO formatted templates to generate PDF views
047: * This handler will use JPublish to generate the XSL-FO
048: *
049: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
050: * @version $Revision: 1.3 $
051: * @since 3.0
052: */
053: public class FopPdfViewHandler extends JPublishViewHandler {
054:
055: public static final String module = FopPdfViewHandler.class
056: .getName();
057:
058: /**
059: * @see org.ofbiz.content.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
060: */
061: public void render(String name, String page, String info,
062: String contentType, String encoding,
063: HttpServletRequest request, HttpServletResponse response)
064: throws ViewHandlerException {
065: // render and obtain the XSL-FO
066: Writer writer = new StringWriter();
067: try {
068: wrapper
069: .render(page, request, response, writer, null,
070: false);
071: } catch (Throwable t) {
072: throw new ViewHandlerException(
073: "Problems with the response writer/output stream",
074: t);
075: }
076: if (Debug.verboseOn()) {
077: Debug.logVerbose("XSL-FO : " + writer.toString(), module);
078: }
079:
080: // configure logging for the FOP
081: Logger logger = new Log4JLogger(Debug.getLogger(module));
082: MessageHandler.setScreenLogger(logger);
083:
084: // load the FOP driver
085: Driver driver = new Driver();
086: driver.setRenderer(Driver.RENDER_PDF);
087: driver.setLogger(logger);
088:
089: // read the XSL-FO XML Document
090: Document xslfo = null;
091: try {
092: xslfo = UtilXml.readXmlDocument(writer.toString());
093: } catch (Throwable t) {
094: throw new ViewHandlerException(
095: "Problems reading the parsed content to XML Document",
096: t);
097: }
098:
099: // create the output stream for the PDF
100: ByteArrayOutputStream out = new ByteArrayOutputStream();
101: driver.setOutputStream(out);
102:
103: // set the input source (XSL-FO) and generate the PDF
104: InputSource is = new DocumentInputSource(xslfo);
105: driver.setInputSource(is);
106: try {
107: driver.run();
108: } catch (Throwable t) {
109: throw new ViewHandlerException(
110: "Unable to generate PDF from XSL-FO", t);
111: }
112:
113: // set the content type and length
114: response.setContentType("application/pdf");
115: response.setContentLength(out.size());
116:
117: // write to the browser
118: try {
119: response.getOutputStream().write(out.toByteArray());
120: response.getOutputStream().flush();
121: } catch (IOException e) {
122: throw new ViewHandlerException(
123: "Unable write to browser OutputStream", e);
124: }
125: }
126:
127: }
|