Using Mirth Connect JavaScript to output a barcode font to PDF

Mirth Connect uses iTextpdf and barcode 128 is not an included font. I downloaded code128.ttf font, but how can I reference this with javascript in Mirth Connect? Maybe someone has a Mirth channel that does this or something similar?

Another possibility is Mirth Connect has a Document Writer template option which can use HTML/CSS to reference the local font. I tried many variations of HTML/Inline CSS to no avail.

6 thoughts on “Using Mirth Connect JavaScript to output a barcode font to PDF”

  1. There are a number of barcode image generator libraries for Java. Create your own Java class that does everything you need, deploy to the /custom-lib folder in the Mirth installation and call the class in your channel’s source or destination transformer JS step.

    Reply
  2. I did it by writing the barcode to a temporary file and referencing that in the Document Writer HTML.

    Add a JavaScript transformer step to your destination Document Writer:

    // Generate your barcode
    var barcodeContents = "0123456789 hello"
    var code128 = new Packages.com.lowagie.text.pdf.Barcode128();
    code128.setCode(barcodeContents);
    
    // Convert to image
    var image = code128.createAwtImage(java.awt.Color.BLACK, java.awt.Color.WHITE);
    var bufferedImage = new java.awt.image.BufferedImage(image.getWidth(),image.getHeight(), java.awt.image.BufferedImage.TYPE_INT_RGB);
    var graphics = bufferedImage.getGraphics();
    graphics.drawImage(image, 0, 0, null);
    
    // Write to JPG file
    var filename = UUIDGenerator.getUUID().toString() + '.jpg';
    javax.imageio.ImageIO.write(bufferedImage, "jpg", new java.io.File("C:\\Temp\\Barcodes\\" + filename));
    
    // Save the filename to use in your Document Writer
    $c('BarcodeFilename', filename);
    

    Then just reference the image in your HTML template:

    <img src="/temp/Barcodes/${BarcodeFilename}"/>
    

    As a bonus you can add a Javascript Writer destination to delete the image afterwards:

    var filename = $c('BarcodeFilename');
    FileUtil.delete("C:\\Temp\\Barcodes\\" + filename);
    
    Reply

Leave a Comment