Example usage for javax.print PrintException printStackTrace

List of usage examples for javax.print PrintException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:PrintImage.java

static public void main(String args[]) throws Exception {
    try {/*from   w  w  w .ja  v  a  2  s.  co m*/
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);

        if (pss.length == 0)
            throw new RuntimeException("No printer services available.");

        PrintService ps = pss[0];
        System.out.println("Printing to " + ps);

        DocPrintJob job = ps.createPrintJob();

        FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
        Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

        job.print(doc, pras);

        fin.close();
    } catch (IOException ie) {
        ie.printStackTrace();
    } catch (PrintException pe) {
        pe.printStackTrace();
    }
}

From source file:ru.codemine.pos.service.printer.PrinterServiceTest.java

@Test
public void testPrinterService() {

    try {/*from  www.  j a  v a 2 s .co m*/
        Cheque cheque = new Cheque();
        cheque.addItem(
                new Product("123456", " ? ?", "123", 1200.0), 1);
        cheque.addItem(new Product("1234576", " ? ", "1237", 2400.0),
                1);
        printerService.printPlainText(new SimpleChequeTemplate(cheque).toString(), "Star-TSP100");
        log.info("Printing: ok");
    } catch (PrintException ex) {
        log.error(ex.getLocalizedMessage());
        ex.printStackTrace();
    }
}

From source file:org.openmrs.module.jembiregistration.api.impl.JembiRegistrationServiceImpl.java

/**
 * Prints a barcode for a given patient.
 * /*www  .  j  a v  a 2 s.  c om*/
 * @param patient
 *            The patient for whom the barcode should be printed
 * @return
 */
public void printPatientBarcode(Patient patient) {
    String label = LABEL_TEMPLATE;

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Context.getLocale());

    try {
        /* handle null case */
        if (patient == null) {
            throw new APIException("No patient passed to printPatientBarcode method.");
        }

        log.info("Printing Barcode for patient: " + patient.getId());

        /* Get service and initialize some parameters from global properties */
        String printerName = Context.getAdministrationService()
                .getGlobalProperty("jembiregistration.PrinterName");
        Integer copies, idType;

        try {
            copies = Integer.parseInt(
                    Context.getAdministrationService().getGlobalProperty("jembiregistration.LabelPrintCount"));
            idType = Integer.parseInt(
                    Context.getAdministrationService().getGlobalProperty("jembiregistration.LabelIdType"));
        } catch (NumberFormatException n) {
            log.error("Global property is not valid: " + n.getMessage());

            copies = 1;
            idType = 0;
        }

        /* Name (Only print first and last name */
        String name = (patient.getPersonName().getGivenName() != null ? patient.getPersonName().getGivenName()
                : "") + " "
                + (patient.getPersonName().getFamilyName() != null ? patient.getPersonName().getFamilyName()
                        : "");
        /* Name (Only print first and last name */

        /* Birthdate */
        String birthDateLabel = Context.getMessageSourceService()
                .getMessage("jembiregistration.birthDateShort");
        String birthDate = (df.format(patient.getBirthdate()) + " "
                + (patient.getBirthdateEstimated() ? "(*)" : " "));

        /* Gender */
        String genderLabel = Context.getMessageSourceService().getMessage("Patient.gender");
        String gender = (patient.getGender().equalsIgnoreCase("M")
                ? Context.getMessageSourceService().getMessage("Patient.gender.male")
                : Context.getMessageSourceService().getMessage("Patient.gender.female"));

        /* Primary identifier */
        PatientIdentifier primaryIdentifier = (idType == 0) ? patient.getPatientIdentifier()
                : patient.getPatientIdentifier(Context.getPatientService().getPatientIdentifierType(idType));

        if (primaryIdentifier != null) {
            String nidLabel = primaryIdentifier.getIdentifierType().getName();
            String nid = primaryIdentifier.getIdentifier();

            label = label.replace("*NIDL*", nidLabel);
            label = label.replace("*NID*", nid);
        }

        /* Populate template */
        label = label.replace("*PN*", name);
        label = label.replace("*BDL*", birthDateLabel);
        label = label.replace("*BD*", birthDate);
        label = label.replace("*GL*", genderLabel);
        label = label.replace("*G*", gender);

        try {
            /* Find printer */
            PrintService psZebra = null;
            String sPrinterName = null;
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

            for (int i = 0; i < services.length; i++) {
                PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class);

                sPrinterName = ((PrinterName) attr).getValue();

                log.info("Looking for printer: " + sPrinterName);

                if (sPrinterName.toLowerCase().indexOf(printerName.toLowerCase()) >= 0) {
                    psZebra = services[i];
                    break;
                }
            }

            if (psZebra == null) {
                log.error(
                        "Zebra printer is not found. Barcode could not be printed. Check your global properties to make sure printer names match.");

            }

            /* Send label data to printer */
            for (int i = 1; i <= copies; i++) {

                DocPrintJob job = psZebra.createPrintJob();

                byte[] by = label.getBytes();
                DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
                Doc doc = new SimpleDoc(by, flavor, null);
                job.print(doc, null);
            }
        } catch (PrintException e) {
            log.error("Unable to find or initialise printer.");
            e.printStackTrace();
        }
    } catch (Exception e) {
        log.error("An error occured while attempting to print a barcode.");
        e.printStackTrace();
    }
}