Search in sources :

Example 6 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class PSStreamPrintJob method pageableJob.

public void pageableJob(Pageable pageable, PrintRequestAttributeSet attributes) throws PrintException {
    try {
        synchronized (this) {
            if (job != null) {
                // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new PSPrinterJob();
            }
        }
        job.setPrintService(getPrintService());
        job.setPageable(pageable);
        job.print(attributes);
        notifyEvent(PrintJobEvent.JOB_COMPLETE);
        return;
    } catch (PrinterException pe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(pe);
    } finally {
        printReturned = true;
    }
}
Also used : PrintException(javax.print.PrintException)

Example 7 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class PSStreamPrintJob method printableJob.

public void printableJob(Printable printable, PrintRequestAttributeSet attributes) throws PrintException {
    try {
        synchronized (this) {
            if (job != null) {
                // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new PSPrinterJob();
            }
        }
        job.setPrintService(getPrintService());
        PageFormat pf = new PageFormat();
        if (mediaSize != null) {
            Paper p = new Paper();
            p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
            p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0);
            pf.setPaper(p);
        }
        if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
            pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        } else if (orient == OrientationRequested.LANDSCAPE) {
            pf.setOrientation(PageFormat.LANDSCAPE);
        }
        job.setPrintable(printable, pf);
        job.print(attributes);
        notifyEvent(PrintJobEvent.JOB_COMPLETE);
        return;
    } catch (PrinterException pe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(pe);
    } finally {
        printReturned = true;
    }
}
Also used : PrintException(javax.print.PrintException)

Example 8 with PrintException

use of javax.print.PrintException in project jdk8u_jdk by JetBrains.

the class PSStreamPrintJob method print.

public void print(Doc doc, PrintRequestAttributeSet attributes) throws PrintException {
    synchronized (this) {
        if (printing) {
            throw new PrintException("already printing");
        } else {
            printing = true;
        }
    }
    this.doc = doc;
    /* check if the parameters are valid before doing much processing */
    DocFlavor flavor = doc.getDocFlavor();
    Object data;
    try {
        data = doc.getPrintData();
    } catch (IOException e) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("can't get print data: " + e.toString());
    }
    if (flavor == null || (!service.isDocFlavorSupported(flavor))) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintJobFlavorException("invalid flavor", flavor);
    }
    initializeAttributeSets(doc, attributes);
    getAttributeValues(flavor);
    String repClassName = flavor.getRepresentationClassName();
    if (flavor.equals(DocFlavor.INPUT_STREAM.GIF) || flavor.equals(DocFlavor.INPUT_STREAM.JPEG) || flavor.equals(DocFlavor.INPUT_STREAM.PNG) || flavor.equals(DocFlavor.BYTE_ARRAY.GIF) || flavor.equals(DocFlavor.BYTE_ARRAY.JPEG) || flavor.equals(DocFlavor.BYTE_ARRAY.PNG)) {
        try {
            instream = doc.getStreamForBytes();
            printableJob(new ImagePrinter(instream), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (flavor.equals(DocFlavor.URL.GIF) || flavor.equals(DocFlavor.URL.JPEG) || flavor.equals(DocFlavor.URL.PNG)) {
        try {
            printableJob(new ImagePrinter((URL) data), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        }
    } else if (repClassName.equals("java.awt.print.Pageable")) {
        try {
            pageableJob((Pageable) doc.getPrintData(), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else if (repClassName.equals("java.awt.print.Printable")) {
        try {
            printableJob((Printable) doc.getPrintData(), reqAttrSet);
            return;
        } catch (ClassCastException cce) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(cce);
        } catch (IOException ioe) {
            notifyEvent(PrintJobEvent.JOB_FAILED);
            throw new PrintException(ioe);
        }
    } else {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException("unrecognized class: " + repClassName);
    }
}
Also used : PrintException(javax.print.PrintException) IOException(java.io.IOException) DocFlavor(javax.print.DocFlavor)

Example 9 with PrintException

use of javax.print.PrintException in project camel by apache.

the class PrinterOperations method print.

public void print(Doc doc, int copies, boolean sendToPrinter, String mimeType, String jobName) 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("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 {} to Printer: {}", i, this.printService.getName());
            }
            print(doc, jobName);
        }
    }
}
Also used : PrintException(javax.print.PrintException) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) PrintException(javax.print.PrintException)

Example 10 with PrintException

use of javax.print.PrintException in project camel by apache.

the class PrinterProducer method assignPrintService.

private PrintService assignPrintService() throws PrintException {
    PrintService printService;
    if ((config.getHostname().equalsIgnoreCase("localhost")) && (config.getPrintername().equalsIgnoreCase("default"))) {
        printService = PrintServiceLookup.lookupDefaultPrintService();
    } else {
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        String name;
        if (config.getHostname().equalsIgnoreCase("localhost")) {
            // no hostname for localhost printers
            name = config.getPrintername();
        } else {
            name = config.getHostname() + "/" + config.getPrintername();
            if (config.getPrinterPrefix() != null) {
                name = config.getPrinterPrefix() + name;
            }
        }
        log.debug("Using printer name: {}", name);
        setPrinter(name);
        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;
}
Also used : PrintException(javax.print.PrintException) Endpoint(org.apache.camel.Endpoint) PrintService(javax.print.PrintService)

Aggregations

PrintException (javax.print.PrintException)19 IOException (java.io.IOException)7 PrintService (javax.print.PrintService)6 java.awt.print (java.awt.print)4 DocFlavor (javax.print.DocFlavor)4 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)4 Copies (javax.print.attribute.standard.Copies)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 DocPrintJob (javax.print.DocPrintJob)3 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 URI (java.net.URI)2 Doc (javax.print.Doc)2 SimpleDoc (javax.print.SimpleDoc)2 Attribute (javax.print.attribute.Attribute)2