Example usage for javax.print PrintException PrintException

List of usage examples for javax.print PrintException PrintException

Introduction

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

Prototype

public PrintException(Exception e) 

Source Link

Document

Construct a print exception chaining the supplied exception.

Usage

From source file:Main.java

public static void imprimirFactura(String factura) throws PrintException {
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();
    if (service == null) {
        throw new PrintException("No se encontro impresora conectada");
    }/*from  www.ja  va 2  s  .c  o m*/

    //Le decimos el tipo de datos que vamos a enviar a la impresora
    //Tipo: bytes Subtipo: autodetectado
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    DocPrintJob pj = service.createPrintJob();
    byte[] bytes;
    bytes = factura.getBytes();
    Doc doc = new SimpleDoc(bytes, flavor, null);

    pj.print(doc, null);

}

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

public PrinterOperations() throws PrintException {
    printService = PrintServiceLookup.lookupDefaultPrintService();
    if (printService == null) {
        throw new PrintException("Printer lookup failure. No default printer set up for this host");
    }//from  w w w.  j a va 2s  .c o m
    job = printService.createPrintJob();
    flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    printRequestAttributeSet = new HashPrintRequestAttributeSet();
    printRequestAttributeSet.add(new Copies(1));
    printRequestAttributeSet.add(MediaSizeName.NA_LETTER);
    printRequestAttributeSet.add(Sides.ONE_SIDED);
}

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  . j  a  v 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);
        }
    }
}

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

private PrintRequestAttributeSet assignPrintAttributes() throws PrintException {
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    if (config.getCopies() >= 1) {
        printRequestAttributeSet.add(new Copies(config.getCopies()));
    } else {//from  w ww  .j  a  v a  2 s . c  om
        throw new PrintException("Number of print copies should be greater than zero");
    }
    printRequestAttributeSet.add(config.getMediaSizeName());
    printRequestAttributeSet.add(config.getInternalSides());

    return printRequestAttributeSet;
}

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

private PrintService assignPrintService() throws PrintException {
    PrintService printService;//from  ww w  . jav  a2s .  c o m

    if ((config.getHostname().equalsIgnoreCase("localhost"))
            && (config.getPrintername().equalsIgnoreCase("/default"))) {
        printService = PrintServiceLookup.lookupDefaultPrintService();
    } else {
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        setPrinter("\\\\" + config.getHostname() + "\\" + config.getPrintername());
        int position = findPrinter(services, printer);
        if (position < 0) {
            throw new PrintException("No printer found with name: " + printer
                    + ". Please verify that the host and printer are registered and reachable from this machine.");
        }
        printService = services[position];
    }
    return printService;
}

From source file:org.openvpms.web.component.im.till.CashDrawer.java

/**
 * Opens the drawer./*from   w w w  .  j  a  v  a  2  s  .c  o  m*/
 *
 * @throws PrintException if the drawer cannot be opened
 */
public void open() throws PrintException {
    AttributeSet attrSet = new HashPrintServiceAttributeSet(new PrinterName(printer, null));
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, attrSet);
    if (printServices.length == 0) {
        throw new PrintException(Messages.format("till.drawerCommand.printernotfound", printer));
    }
    DocPrintJob job = printServices[0].createPrintJob();
    DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
    Doc doc = new SimpleDoc(command, flavor, null);
    job.print(doc, null);
}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

public static void printDirectly(final MasterReport report, PrintService printService)
        throws PrintException, ReportProcessingException {
    // with that method we do not use the PrintService UI ..
    // it is up to the user to supply a valid print service that
    // supports the Pageable printing.
    if (printService == null) {
        printService = lookupPrintService();
    } else {/* w w w  . j  a v  a 2 s .  co  m*/
        if (printService.isDocFlavorSupported(DocFlavor.SERVICE_FORMATTED.PAGEABLE) == false) {
            throw new PrintException("The print service implementation does not support the Pageable Flavor.");
        }
    }

    PrintRequestAttributeSet attributes = Java14PrintUtil.copyConfiguration(null, report);
    attributes = Java14PrintUtil.copyAuxillaryAttributes(attributes, report);

    final PrintReportProcessor reportPane = new PrintReportProcessor(report);
    final DocPrintJob job = printService.createPrintJob();
    final SimpleDoc document = new SimpleDoc(reportPane, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);

    try {
        job.print(document, attributes);
    } finally {
        reportPane.close();
    }

}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

private static PrintService lookupPrintService() throws PrintException {
    final PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    if (defaultService != null && defaultService.isDocFlavorSupported(DocFlavor.SERVICE_FORMATTED.PAGEABLE)) {
        return defaultService;
    }//w  ww  .  ja v  a2 s.c om

    final PrintService printService;
    final PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE,
            null);
    if (services.length == 0) {
        throw new PrintException("Unable to find a matching print service implementation.");
    }
    printService = services[0];
    return printService;
}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.java14print.Java14PrintUtil.java

public static boolean print(final MasterReport report, final ReportProgressListener progressListener)
        throws PrintException, ReportProcessingException {
    final PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE,
            null);//ww  w  .  j a v  a 2  s  .  c om
    if (services.length == 0) {
        throw new PrintException("Unable to find a matching print service implementation.");
    }
    PrintRequestAttributeSet attributes = Java14PrintUtil.copyConfiguration(null, report);
    attributes = Java14PrintUtil.copyAuxillaryAttributes(attributes, report);

    final PrintService service = ServiceUI.printDialog(null, 50, 50, services, lookupPrintService(),
            DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributes);
    if (service == null) {
        return false;
    }

    final PrintReportProcessor reportPane = new PrintReportProcessor(report);
    if (progressListener != null) {
        reportPane.addReportProgressListener(progressListener);
    }

    try {
        reportPane.fireProcessingStarted();

        final DocPrintJob job = service.createPrintJob();
        final SimpleDoc document = new SimpleDoc(reportPane, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);

        job.print(document, attributes);
    } finally {
        reportPane.fireProcessingFinished();
        reportPane.close();

        if (progressListener != null) {
            reportPane.removeReportProgressListener(progressListener);
        }
    }
    return true;
}