Search code examples
javamacosjasper-reportsbarcodezxing

Jasperreports not printing barcodes on Mac OS X


We are experiencing a strange problem trying to print barcodes from the Mac. Everything works fine on Windows, however, on the Mac:

  • Jasper Preview shows barcode fine
  • Saving PDF via jasper shows barcode fine and pdf prints correctly
  • Printing report excludes barcode
  • Saving PDF via Mac Printer dialog excludes barcodes

This report also includes a second image directly from a field, which prints fine on the Mac. The barcode is generated via zxing and written to a ByteArrayOutputStream as a png. This is added to the report as an image object. I've also tried other image formats without success.

This issue has been reproduced across different printers and on the latest jasper library (6.4.1). There are no error message reported in the log. I've also tried generating the barcode slightly smaller than the bounding region to ensure it doesn't get clipped.

My Mac is currently running 10.12.6 with java 8.

Thanks.

Test case below (BarcodeTest.java):

public class BarcodeTest
{
    public static java.io.ByteArrayInputStream createBarcode(String aBarcodeStr, int aAlignmentX, int rotate, int sizeX, int sizeY)
        throws IOException, WriterException, NotFoundException
    {
        Code39Writer c39 = new Code39Writer();
        BitMatrix bm = c39.encode(aBarcodeStr.trim(), BarcodeFormat.CODE_39, sizeX, sizeY);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bm, "PNG", out);
        return new java.io.ByteArrayInputStream(out.toByteArray());
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() ->
        {
            String thisFile = "BarcodeTest.jrxml";
            try
            {
                JasperReport jasperReport = JasperCompileManager.compileReport(thisFile);
                HashMap hm = new HashMap();
                JasperPrint jasperPrint = JasperFillManager.fillReport(
                    jasperReport,
                    hm,
                    new JREmptyDataSource());

                JRViewer jrv = new JRViewer(jasperPrint);
                JFrame jf = new JFrame("Barcode test");
                jf.setSize(800, 600);
                jf.add(jrv);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.getContentPane().add(jrv);
                jf.setLocationRelativeTo(null);
                jf.setVisible(true);
            }
            catch(HeadlessException | JRException e)
            {
                e.printStackTrace();
            }
        });
    }
}

with this jrxml (BarcodeTest.jrxml):

<?xml version="1.0" encoding="Cp1252"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="AdvancedReports" columnCount="3" printOrder="Horizontal" pageWidth="595" pageHeight="842" columnWidth="185" columnSpacing="9" leftMargin="9" rightMargin="9" topMargin="62" bottomMargin="6" uuid="7c881f22-0368-4f79-8e3f-8ca0a36dfe37">
    <pageHeader>
        <band height="72">
            <textField isBlankWhenNull="true">
                <reportElement positionType="Float" x="4" y="35" width="176" height="30" forecolor="#000000" backcolor="#FFFFFF" uuid="1a38a9fe-2887-498f-be6f-758397d57175"/>
                <textElement textAlignment="Left" verticalAlignment="Middle" rotation="None"/>
                <textFieldExpression><![CDATA["123456"]]></textFieldExpression>
            </textField>
            <image hAlign="Left">
                <reportElement x="4" y="4" width="176" height="30" uuid="508b033d-e71c-419d-843f-c23255294533"/>
                <imageExpression><![CDATA[BarcodeTest.createBarcode("123456",2,0,176,30)]]></imageExpression>
            </image>
        </band>
    </pageHeader>
</jasperReport>

Solution

  • I ran your report and got an error message that looks just like the one from JDK-8038142. So chances are the problem that you are experiencing is caused by the same Java bug (which should be fixed in the latest version, but for some reason it still didn't work for me).

    One simple way to workaround the JDK problem is to change your createBarcode method to return a BufferedImage instead of PNG image data. All you need for that is

    return MatrixToImageWriter.toBufferedImage(bm);