Search in sources :

Example 1 with ZebraGraphics

use of com.zebra.sdk.common.card.graphics.ZebraGraphics in project openmrs-module-pihcore by PIH.

the class ZXPSeries3PrintHandler method print.

@Override
public void print(Printer printer, Map<String, Object> paramMap) throws UnableToPrintException {
    String name = (String) paramMap.get("name");
    String gender = (String) paramMap.get("gender");
    String birthdate = (String) paramMap.get("birthdate");
    Boolean birthdateEstimated = (Boolean) paramMap.get("birthdateEstimated");
    String patientIdentifier = (String) paramMap.get("patientIdentifier");
    List<String> addressLines = (paramMap.containsKey("addressLines") ? (List<String>) paramMap.get("addressLines") : null);
    String telephoneNumber = (paramMap.containsKey("telephoneNumber") ? (String) paramMap.get("telephoneNumber") : null);
    String issuingLocation = (paramMap.containsKey("issuingLocation") ? (String) paramMap.get("issuingLocation") : null);
    String issuedDate = (String) paramMap.get("issuedDate");
    String customCardLabel = (paramMap.containsKey("customCardLabel") ? (String) paramMap.get("customCardLabel") : null);
    ZebraCardPrinter zebraCardPrinter = null;
    ZebraGraphics graphics = null;
    Connection connection = null;
    List<GraphicsInfo> graphicsData;
    int nameFontSize = 16;
    // less-than-perfect attempt to lower font size for patients with large names
    if (name.length() > 28) {
        nameFontSize = 10;
    } else if (name.length() > 22) {
        nameFontSize = 12;
    }
    int retryCount = 0;
    boolean success = false;
    Integer jobId = null;
    while (!success && retryCount < MAX_RETRY) {
        log.info("Starting ID card print job for patient " + patientIdentifier + " on printer " + printer.getName());
        try {
            connection = new TcpConnection(printer.getIpAddress(), 9100);
            connection.open();
            zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
            graphicsData = new ArrayList<GraphicsInfo>();
            log.info("Connection opened for ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            GraphicsInfo grInfo = new GraphicsInfo();
            grInfo.side = CardSide.Front;
            grInfo.printType = PrintType.MonoK;
            grInfo.graphicType = GraphicType.BMP;
            graphics = new ZebraCardGraphics(zebraCardPrinter);
            graphics.setPrinterModel(PrinterModel.ZXPSeries3);
            graphics.initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.WHITE);
            // name
            graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, nameFontSize));
            graphics.drawText(name, 60, 20, Color.BLACK);
            // divider line
            graphics.drawLine(25, 100, 1000, 100, 3, Color.BLACK);
            // gender & birthdate
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Sèks", 60, 320, Color.BLACK);
            graphics.drawText("Dat ou fèt " + (birthdateEstimated != null && birthdateEstimated ? " (Estime)" : " "), 250, 320, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText((gender.equals("F") ? "Fi" : "Gason"), 60, 350, Color.BLACK);
            graphics.drawText(birthdate, 250, 350, Color.BLACK);
            // address
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            int verticalPosition = 110;
            if (addressLines != null && addressLines.size() > 0) {
                for (String addressLine : addressLines) {
                    graphics.drawText(addressLine, 60, verticalPosition, Color.BLACK);
                    verticalPosition = verticalPosition + 50;
                }
            }
            // telephone number
            if (StringUtils.isNotBlank(telephoneNumber)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
                graphics.drawText("Nimewo Telefòn", 600, 320, Color.BLACK);
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(telephoneNumber, 600, 350, Color.BLACK);
            }
            // divider line
            graphics.drawLine(25, 420, 1000, 420, 3, Color.BLACK);
            // bar code and identifier
            Code39Util barcode = ZebraBarcodeFactory.getCode39(graphics);
            barcode.setBarHeight(100);
            barcode.drawBarcode(patientIdentifier, 60, 450, Rotation.ROTATE_0);
            // custom card label, if specified
            if (StringUtils.isNotBlank(customCardLabel)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(customCardLabel, 420, 450, Color.BLACK);
            }
            // date and location issued
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Dat kat la fet la", 420, 520, Color.BLACK);
            graphics.drawText("Kote kat la fet la", 720, 520, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText(issuedDate, 420, 560, Color.BLACK);
            graphics.drawText((StringUtils.isNotBlank(issuingLocation) ? issuingLocation : ""), 720, 560, Color.BLACK);
            // do the actual printing
            grInfo.graphicData = graphics.createImage(null);
            graphics.clear();
            graphicsData.add(grInfo);
            log.info("Starting ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            jobId = zebraCardPrinter.print(1, graphicsData);
            log.info("Started ID card printing job " + jobId + " for patient " + patientIdentifier + " on printer " + printer.getName());
            success = pollJobStatus(zebraCardPrinter, jobId, printer);
        } catch (Exception e) {
            log.warn("Unable to print to printer " + printer.getName(), e);
        } finally {
            retryCount++;
            cleanUp(connection, zebraCardPrinter, jobId, graphics, printer, success);
        }
        // TODO remove
        if (success) {
            log.info("Success printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        } else {
            log.info("Failed printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        }
        if (!success && retryCount < MAX_RETRY) {
            try {
                Thread.sleep(TIME_BETWEEN_RETRIES_IN_MS);
            } catch (InterruptedException e) {
            // do nothing
            }
        }
    }
}
Also used : ZebraGraphics(com.zebra.sdk.common.card.graphics.ZebraGraphics) TcpConnection(com.zebra.sdk.comm.TcpConnection) Connection(com.zebra.sdk.comm.Connection) GraphicsInfo(com.zebra.sdk.common.card.containers.GraphicsInfo) TcpConnection(com.zebra.sdk.comm.TcpConnection) Font(java.awt.Font) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Code39Util(com.zebra.sdk.common.card.graphics.barcode.Code39Util) ZebraCardPrinter(com.zebra.sdk.common.card.printer.ZebraCardPrinter) ArrayList(java.util.ArrayList) List(java.util.List) ZebraCardGraphics(com.zebra.sdk.common.card.graphics.ZebraCardGraphics)

Example 2 with ZebraGraphics

use of com.zebra.sdk.common.card.graphics.ZebraGraphics in project openmrs-module-mirebalais by PIH.

the class ZXPSeries3PrintHandler method print.

@Override
public void print(Printer printer, Map<String, Object> paramMap) throws UnableToPrintException {
    String name = (String) paramMap.get("name");
    String gender = (String) paramMap.get("gender");
    String birthdate = (String) paramMap.get("birthdate");
    Boolean birthdateEstimated = (Boolean) paramMap.get("birthdateEstimated");
    String patientIdentifier = (String) paramMap.get("patientIdentifier");
    List<String> addressLines = (paramMap.containsKey("addressLines") ? (List<String>) paramMap.get("addressLines") : null);
    String telephoneNumber = (paramMap.containsKey("telephoneNumber") ? (String) paramMap.get("telephoneNumber") : null);
    String issuingLocation = (paramMap.containsKey("issuingLocation") ? (String) paramMap.get("issuingLocation") : null);
    String issuedDate = (String) paramMap.get("issuedDate");
    String customCardLabel = (paramMap.containsKey("customCardLabel") ? (String) paramMap.get("customCardLabel") : null);
    ZebraCardPrinter zebraCardPrinter = null;
    ZebraGraphics graphics = null;
    Connection connection = null;
    List<GraphicsInfo> graphicsData;
    int nameFontSize = 16;
    // less-than-perfect attempt to lower font size for patients with large names
    if (name.length() > 28) {
        nameFontSize = 10;
    } else if (name.length() > 22) {
        nameFontSize = 12;
    }
    int retryCount = 0;
    boolean success = false;
    Integer jobId = null;
    while (!success && retryCount < MAX_RETRY) {
        log.info("Starting ID card print job for patient " + patientIdentifier + " on printer " + printer.getName());
        try {
            connection = new TcpConnection(printer.getIpAddress(), 9100);
            connection.open();
            zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
            graphicsData = new ArrayList<GraphicsInfo>();
            log.info("Connection opened for ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            GraphicsInfo grInfo = new GraphicsInfo();
            grInfo.side = CardSide.Front;
            grInfo.printType = PrintType.MonoK;
            grInfo.graphicType = GraphicType.BMP;
            graphics = new ZebraCardGraphics(zebraCardPrinter);
            graphics.setPrinterModel(PrinterModel.ZXPSeries3);
            graphics.initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, Color.WHITE);
            // name
            graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, nameFontSize));
            graphics.drawText(name, 60, 20, Color.BLACK);
            // divider line
            graphics.drawLine(25, 100, 1000, 100, 3, Color.BLACK);
            // gender & birthdate
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Sèks", 60, 320, Color.BLACK);
            graphics.drawText("Dat ou fèt " + (birthdateEstimated != null && birthdateEstimated ? " (Estime)" : " "), 250, 320, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText((gender.equals("F") ? "Fi" : "Gason"), 60, 350, Color.BLACK);
            graphics.drawText(birthdate, 250, 350, Color.BLACK);
            // address
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            int verticalPosition = 110;
            if (addressLines != null && addressLines.size() > 0) {
                for (String addressLine : addressLines) {
                    graphics.drawText(addressLine, 60, verticalPosition, Color.BLACK);
                    verticalPosition = verticalPosition + 50;
                }
            }
            // telephone number
            if (StringUtils.isNotBlank(telephoneNumber)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
                graphics.drawText("Nimewo Telefòn", 600, 320, Color.BLACK);
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(telephoneNumber, 600, 350, Color.BLACK);
            }
            // divider line
            graphics.drawLine(25, 420, 1000, 420, 3, Color.BLACK);
            // bar code and identifier
            Code39Util barcode = ZebraBarcodeFactory.getCode39(graphics);
            barcode.setBarHeight(100);
            barcode.drawBarcode(patientIdentifier, 60, 450, Rotation.ROTATE_0);
            // custom card label, if specified
            if (StringUtils.isNotBlank(customCardLabel)) {
                graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
                graphics.drawText(customCardLabel, 420, 450, Color.BLACK);
            }
            // date and location issued
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 6));
            graphics.drawText("Dat kat la fet la", 420, 520, Color.BLACK);
            graphics.drawText("Kote kat la fet la", 720, 520, Color.BLACK);
            graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 9));
            graphics.drawText(issuedDate, 420, 560, Color.BLACK);
            graphics.drawText((StringUtils.isNotBlank(issuingLocation) ? issuingLocation : ""), 720, 560, Color.BLACK);
            // do the actual printing
            grInfo.graphicData = graphics.createImage(null);
            graphics.clear();
            graphicsData.add(grInfo);
            log.info("Starting ID card printing for patient " + patientIdentifier + " on printer " + printer.getName());
            jobId = zebraCardPrinter.print(1, graphicsData);
            log.info("Started ID card printing job " + jobId + " for patient " + patientIdentifier + " on printer " + printer.getName());
            success = pollJobStatus(zebraCardPrinter, jobId, printer);
        } catch (Exception e) {
            log.warn("Unable to print to printer " + printer.getName(), e);
        } finally {
            retryCount++;
            cleanUp(connection, zebraCardPrinter, jobId, graphics, printer, success);
        }
        // TODO remove
        if (success) {
            log.info("Success printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        } else {
            log.info("Failed printing ID card for patient " + patientIdentifier + " on printer " + printer.getName());
        }
        if (!success && retryCount < MAX_RETRY) {
            try {
                Thread.sleep(TIME_BETWEEN_RETRIES_IN_MS);
            } catch (InterruptedException e) {
            // do nothing
            }
        }
    }
}
Also used : ZebraGraphics(com.zebra.sdk.common.card.graphics.ZebraGraphics) TcpConnection(com.zebra.sdk.comm.TcpConnection) Connection(com.zebra.sdk.comm.Connection) GraphicsInfo(com.zebra.sdk.common.card.containers.GraphicsInfo) TcpConnection(com.zebra.sdk.comm.TcpConnection) Font(java.awt.Font) ConnectionException(com.zebra.sdk.comm.ConnectionException) ZebraCardException(com.zebra.sdk.common.card.exceptions.ZebraCardException) UnableToPrintException(org.openmrs.module.printer.UnableToPrintException) Code39Util(com.zebra.sdk.common.card.graphics.barcode.Code39Util) ZebraCardPrinter(com.zebra.sdk.common.card.printer.ZebraCardPrinter) ArrayList(java.util.ArrayList) List(java.util.List) ZebraCardGraphics(com.zebra.sdk.common.card.graphics.ZebraCardGraphics)

Aggregations

Connection (com.zebra.sdk.comm.Connection)2 ConnectionException (com.zebra.sdk.comm.ConnectionException)2 TcpConnection (com.zebra.sdk.comm.TcpConnection)2 GraphicsInfo (com.zebra.sdk.common.card.containers.GraphicsInfo)2 ZebraCardException (com.zebra.sdk.common.card.exceptions.ZebraCardException)2 ZebraCardGraphics (com.zebra.sdk.common.card.graphics.ZebraCardGraphics)2 ZebraGraphics (com.zebra.sdk.common.card.graphics.ZebraGraphics)2 Code39Util (com.zebra.sdk.common.card.graphics.barcode.Code39Util)2 ZebraCardPrinter (com.zebra.sdk.common.card.printer.ZebraCardPrinter)2 Font (java.awt.Font)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 UnableToPrintException (org.openmrs.module.printer.UnableToPrintException)2