Example usage for javax.print Doc getStreamForBytes

List of usage examples for javax.print Doc getStreamForBytes

Introduction

In this page you can find the example usage for javax.print Doc getStreamForBytes.

Prototype

public InputStream getStreamForBytes() throws IOException;

Source Link

Document

Obtains an input stream for extracting byte print data from this doc.

Usage

From source file:org.apache.camel.component.printer.PrinterOperations.java

public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType) throws PrintException {
    LOG.trace("Print Service: " + this.printService.getName());
    LOG.trace("About to print " + copies + " copy(s)");

    for (int i = 0; i < copies; i++) {
        if (!sendToPrinter) {
            LOG.debug(/*  w  w w.jav  a  2 s . c  o m*/
                    "Print flag is set to false. This job will not be printed until this setting remains in effect."
                            + " Please set the flag to true or remove the setting.");

            File file;
            if (mimeType.equalsIgnoreCase("GIF") || mimeType.equalsIgnoreCase("RENDERABLE_IMAGE")) {
                file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".gif");
            } else if (mimeType.equalsIgnoreCase("JPEG")) {
                file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".jpeg");
            } else if (mimeType.equalsIgnoreCase("PDF")) {
                file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".pdf");
            } else {
                file = new File("./target/TestPrintJobNo" + i + "_" + UUID.randomUUID() + ".txt");
            }

            LOG.debug("Writing print job to file: " + file.getAbsolutePath());
            try {
                InputStream in = doc.getStreamForBytes();
                FileOutputStream fos = new FileOutputStream(file);
                IOHelper.copyAndCloseInput(in, fos);
                IOHelper.close(fos);
            } catch (Exception e) {
                throw new PrintException("Error writing Document to the target file " + file.getAbsolutePath());
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Issuing Job " + i + " to Printer: " + this.printService.getName());
            }
            print(doc);
        }
    }
}