Print Renderer

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Print Renderer

Normen

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);

           }

    }

Reply | Threaded
Open this post in threaded view
|

Re: Print Renderer

Szeak (Register Man)
Hi,

What i use for this is the PipedInputStream and PipedOutputStream.

By Szeak

2017-12-14 09:18 keltezéssel, Normen Ruhrus írta:

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);

           }

    }


--------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Print Renderer

Szeak (Register Man)
In reply to this post by Normen
Hi,

Here is small detail from my source code:

            final InputStream inStream = new PipedInputStream();
            OutputStream outStream = new PipedOutputStream((PipedInputStream) inStream);

            logger.debug("Nyomtatási feladat megnyitása ...");
            AttributeSet paset = new HashAttributeSet();
            paset.add(new PrinterName(properties.getProperty(PrintProperties.PRINTER_NAME), null));
            paset.add(new JobOriginatingUserName(properties.getProperty(PrintProperties.LPR_USER_NAME), new Locale("hu", "HU")));
            final PrintService[] services = PrintServiceLookup.lookupPrintServices(null, paset);

            if (services.length == 0) {
                throw new Exception("Nyomtató nem található: " + properties.getProperty(PrintProperties.PRINTER_NAME));
            }

            final Thread printJobThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        PrintService printService = services[0];

                        //create a print job
                        DocPrintJob job = printService.createPrintJob();
                        //create print job attributes
                        PrintRequestAttributeSet pjaset = new HashPrintRequestAttributeSet(new PrintRequestAttribute[]{
                            new JobName(String.format("IPS2-OCR - %s (%s)", ((JPAContainer<Ugyfelek>) tableUgyfelek.getContainerDataSource()).getItem(((JPAContainer<Ugyfelek>) tableUgyfelek.getContainerDataSource()).getIdByIndex(0)).getEntity().getMunkak().getNev(), properties.getProperty(PrintProperties.LPR_USER_NAME)), new Locale("hu", "HU"))
                        });
                        //define the format of print document
                        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; //properties.getProperty(PrintProperties.PRINT_MIME).equals(MimeConstants.MIME_POSTSCRIPT) ? DocFlavor.INPUT_STREAM.POSTSCRIPT : DocFlavor.INPUT_STREAM.PCL;
                        //print the data
                        Doc doc = new SimpleDoc(inStream, flavor, null);

                        logger.debug("Start print job ...");
                        job.print(doc, pjaset);
                        logger.debug("End of print job");
                    } catch (PrintException ex) {
                        logger.fatal(ex, ex);
                    }
                }
            });
            printJobThread.start();

            logger.debug("Create Renderer");
            renderer = new Renderer(jobId, properties, servlet.getEmf(), outStream);


Important: the inőut stream writer and aoutputstream reader need to be in separate thread.

By, Szeak

2017-12-14 09:18 keltezéssel, Normen Ruhrus írta:

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);

           }

    }


--------------------------------------------------------------------- To unsubscribe, e-mail: [hidden email] For additional commands, e-mail: [hidden email]