You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Normen Ruhrus <n....@alphasoft.biz> on 2017/12/14 08:18:00 UTC

Print Renderer

Hello everyone,

 

for some older printers we use the FOP direct printing via the
PageableRenderer. Usually we use PostScript printing and there we do not
stream directly to the printer but make the transformation via an
ByteArrayOutputStream to a byte array.

This is possible because the created printouts are quite small so there is
no issue with large data and additionally it gets us better traceability for
problems cause we can better differntiate between FOP issues and Windows
Printer issues.

 

The question is if there is a way to direct print to a ByteArrayOutputstream
also, or if there is some similar way of doing this. Below i have put in our
Print Postscript Function that wroks the way we want to handle direct
printing, too.

 

We use FOP2.0 on Tomcat 8.0 on Windows Server 2012 R2 with Java 8. 

 

Thanks a lot for any help; it is greatly appreciated!

 

Normen

 

 

    public void printPS(String xml, String xslt, String printer)

             throws Exception

    {

       try

       {

             C_Tools.log("Entering printPS().",LogLevel.DEBUG);

             

             // configure fopFactory as desired

               FopFactory fopFactory = getInstance().getFopFactory();

               C_Tools.log("FOP Factory set.",LogLevel.DEBUG);

               

               ByteArrayOutputStream out = new ByteArrayOutputStream();

               C_Tools.log("Buffer set.",LogLevel.DEBUG);

               

               FOUserAgent userAgent = fopFactory.newFOUserAgent();

               userAgent.getEventBroadcaster().addEventListener(new
FOPEventListener(versionFOP));

               C_Tools.log("User Agent and EventListener
set.",LogLevel.DEBUG);

               

               // Construct FOP with desired output format

               Fop fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT,
userAgent, out);        

               C_Tools.log("FOP Object created.",LogLevel.DEBUG);

               

               TransformerFactory factory =
TransformerFactory.newInstance();

               Transformer transformer = factory.newTransformer(new
StreamSource(new StringReader(xslt)));

               C_Tools.log("XSLT (" + xslt.length() + " bytes) set
up.",LogLevel.DEBUG);

               

               try

               {

                    transformer.setErrorListener(getErrorListener());

                    C_Tools.log("Transformer Error Listener
set.",LogLevel.DEBUG);

               }

               catch (Exception ex)

               {

 
C_Tools.log("C_Render_FOP_2_0.printPS().setErrorListener():" +
ex.getMessage(),LogLevel.ERROR);

 

                    StackTraceElement[] stack = ex.getStackTrace();

                   for (int y=0 ; y < stack.length; y++)

                   {

 
C_Tools.log("C_Render_FOP_2_0.printPS().setErrorListener()." +
renderer.toString() + "["+(y+1)+"/"+stack.length+"]:" + stack[y].toString(),
LogLevel.ERROR);

                   }

               }

               

               // Setup input stream

               Source src = new StreamSource(new StringReader(xml));

               C_Tools.log("XML (" + xml.length() + " bytes) set
up.",LogLevel.DEBUG);

               

               // Resulting SAX events (the generated FO) must be piped
through to FOP

               Result result = new SAXResult(fop.getDefaultHandler());

               C_Tools.log("SAX Result Object created.",LogLevel.DEBUG);

               

               // Start XSLT transformation and FOP processing

               transformer.transform(src, result);

               C_Tools.log("Transformation completed.",LogLevel.DEBUG);

               

               out.flush();

               

               //Set up DocPrintJob instance

               DocPrintJob printJob = C_Renderer.createDocPrintJob(printer);


               Doc doc = new SimpleDoc(out.toByteArray(),
DocFlavor.BYTE_ARRAY.AUTOSENSE, null);

               C_Tools.log("Print Job and Document set.",LogLevel.DEBUG);

               

               printJob.print(doc, null);

               C_Tools.log("Document printed.",LogLevel.DEBUG);

               

               out.close();

           }

           catch(Throwable e)

           {

                    C_Tools.log("C_Render_FOP_2_0.printPS(" +
e.getClass().getName() + "):" + e.getMessage(), LogLevel.ERROR);

                    

                    StackTraceElement[] stack = e.getStackTrace();

            for (int y=0 ; y<stack.length; y++)

            {

                C_Tools.log("C_Render_FOP_2_0.printPS()." +
renderer.toString() + "["+(y+1)+"/"+stack.length+"]:" + stack[y].toString(),
LogLevel.ERROR);

            }

               

               throw new Exception(e);

           } 

    }