Author: jonesde Date: Tue Nov 7 15:13:19 2006 New Revision: 472322 URL: http://svn.apache.org/viewvc?view=rev&rev=472322 Log: Added example print service that renders from a screen, sends it through FOP, and then send it to a printer configured on the server
Added: incubator/ofbiz/trunk/framework/example/src/org/ incubator/ofbiz/trunk/framework/example/src/org/ofbiz/ incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java (with props) Modified: incubator/ofbiz/trunk/framework/build.xml incubator/ofbiz/trunk/framework/example/build.xml Modified: incubator/ofbiz/trunk/framework/build.xml URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/build.xml?view=diff&rev=472322&r1=472321&r2=472322 ============================================================================== --- incubator/ofbiz/trunk/framework/build.xml (original) +++ incubator/ofbiz/trunk/framework/build.xml Tue Nov 7 15:13:19 2006 @@ -26,7 +26,7 @@ common/build.xml,workflow/build.xml, webapp/build.xml,guiapp/build.xml,widget/build.xml, testtools/build.xml, - appserver/build.xml,webtools/build.xml"/> + appserver/build.xml,webtools/build.xml,example/build.xml"/> <!-- For Shark add "shark/build.xml" to the list above, in the framework-builds list (after workflow, before webapp works fine) --> <filelist id="test-builds" dir="." files="base/build.xml,entity/build.xml"/> Modified: incubator/ofbiz/trunk/framework/example/build.xml URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/example/build.xml?view=diff&rev=472322&r1=472321&r2=472322 ============================================================================== --- incubator/ofbiz/trunk/framework/example/build.xml (original) +++ incubator/ofbiz/trunk/framework/example/build.xml Tue Nov 7 15:13:19 2006 @@ -42,6 +42,8 @@ <fileset dir="../../framework/service/lib" includes="*.jar"/> <fileset dir="../../framework/service/build/lib" includes="*.jar"/> <fileset dir="../../framework/minilang/build/lib" includes="*.jar"/> + <fileset dir="../../framework/widget/build/lib" includes="*.jar"/> + <fileset dir="../../framework/webapp/lib" includes="*.jar"/> </path> </target> Added: incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java URL: http://svn.apache.org/viewvc/incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java?view=auto&rev=472322 ============================================================================== --- incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java (added) +++ incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java Tue Nov 7 15:13:19 2006 @@ -0,0 +1,163 @@ +/* + * Copyright 2001-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package org.ofbiz.example; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Map; + +import javax.print.Doc; +import javax.print.DocFlavor; +import javax.print.DocPrintJob; +import javax.print.PrintException; +import javax.print.PrintService; +import javax.print.PrintServiceLookup; +import javax.print.SimpleDoc; +import javax.print.attribute.HashPrintRequestAttributeSet; +import javax.print.attribute.PrintRequestAttributeSet; +import javax.print.attribute.standard.Copies; +import javax.print.attribute.standard.Sides; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.avalon.framework.logger.Log4JLogger; +import org.apache.avalon.framework.logger.Logger; +import org.apache.fop.apps.Driver; +import org.apache.fop.image.FopImageFactory; +import org.apache.fop.messaging.MessageHandler; +import org.apache.fop.tools.DocumentInputSource; +import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.UtilXml; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.ServiceUtil; +import org.ofbiz.widget.html.HtmlScreenRenderer; +import org.ofbiz.widget.screen.ScreenRenderer; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class ExamplePrintServices { + public static final String module = ExamplePrintServices.class.getName(); + + private static HtmlScreenRenderer htmlScreenRenderer = new HtmlScreenRenderer(); + + public static Map printReportPdf(DispatchContext dctx, Map context) { + String screenLocation = "component://example/widget/example/ExampleReportScreens.xml"; + String reportScreenName = "ExampleReport"; + + // render a screen to get the XML document + Writer reportWriter = new StringWriter(); + ScreenRenderer reportScreenRenderer = new ScreenRenderer(reportWriter, null, htmlScreenRenderer); + reportScreenRenderer.populateContextForService(dctx, context); + + // put the exampleId in the screen context, is a parameter coming into the service + //reportScreenRenderer.getContext().put("exampleId", context.get("exampleId")); + + try { + reportScreenRenderer.render(screenLocation, reportScreenName); + } catch (GeneralException e) { + String errMsg = "General error rendering screen [" + screenLocation + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } catch (IOException e) { + String errMsg = "IO error rendering screen [" + screenLocation + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } catch (SAXException e) { + String errMsg = "SAX (XML parse) error rendering screen [" + screenLocation + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } catch (ParserConfigurationException e) { + String errMsg = "Parser configuration error rendering screen [" + screenLocation + "]: " + e.toString(); + Debug.logError(e, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + String reportXmlDocument = reportWriter.toString(); + + // configure logging for the FOP + Logger logger = new Log4JLogger(Debug.getLogger(module)); + MessageHandler.setScreenLogger(logger); + + // load the FOP driver + Driver driver = new Driver(); + driver.setRenderer(Driver.RENDER_PDF); + driver.setLogger(logger); + + // read the XSL-FO XML Document + Document xslfo = null; + try { + xslfo = UtilXml.readXmlDocument(reportXmlDocument); + } catch (Throwable t) { + String errMsg = "Problems reading the parsed content to XML Document: " + t.toString(); + Debug.logError(t, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + // create the output stream for the PDF + ByteArrayOutputStream out = new ByteArrayOutputStream(); + driver.setOutputStream(out); + + // set the input source (XSL-FO) and generate the PDF + InputSource is = new DocumentInputSource(xslfo); + driver.setInputSource(is); + try { + driver.run(); + FopImageFactory.resetCache(); + } catch (Throwable t) { + String errMsg = "Unable to generate PDF from XSL-FO: " + t.toString(); + Debug.logError(t, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + + /* + // set the content type and length + response.setContentType("application/pdf"); + response.setContentLength(out.size()); + + // write to the browser + try { + out.writeTo(response.getOutputStream()); + response.getOutputStream().flush(); + } catch (IOException e) { + throw new ViewHandlerException("Unable write to browser OutputStream", e); + } + */ + + DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF; + Doc myDoc = new SimpleDoc(out.toByteArray(), docFlavor, null); + PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); + aset.add(new Copies(1)); + //aset.add(MediaSize.A4); + aset.add(Sides.ONE_SIDED); + + PrintService[] services = PrintServiceLookup.lookupPrintServices(docFlavor, aset); + if (services.length > 0) { + DocPrintJob job = services[0].createPrintJob(); + try { + job.print(myDoc, aset); + } catch (PrintException pe) { + String errMsg = "Unable to print PDF from XSL-FO: " + pe.toString(); + Debug.logError(pe, errMsg, module); + return ServiceUtil.returnError(errMsg); + } + } + + return ServiceUtil.returnSuccess(); + } +} Propchange: incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java ------------------------------------------------------------------------------ svn:keywords = "Date Rev Author URL Id" Propchange: incubator/ofbiz/trunk/framework/example/src/org/ofbiz/example/ExamplePrintServices.java ------------------------------------------------------------------------------ svn:mime-type = text/plain