Jasper 报告未在 Mac OS X 上打印条形码

Jasperreports not printing barcodes on Mac OS X

我们在尝试从 Mac 打印条形码时遇到了一个奇怪的问题。在 Windows 上一切正常,但是在 Mac:

此报告还包括直接来自字段的第二张图片,在 Mac 上打印效果很好。条形码通过 zxing 生成并作为 png 写入 ByteArrayOutputStream。这作为图像对象添加到报告中。我也尝试过其他图像格式但没有成功。

此问题已在不同的打印机和最新的 jasper 库 (6.4.1) 上重现。日志中没有报告错误消息。我还尝试生成略小于边界区域的条形码以确保它不会被剪裁。

我的 Mac 目前是 运行 10.12.6 和 java 8.

谢谢。

下面的测试用例 (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();
            }
        });
    }
}

使用这个 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>

我 运行 你的报告,并收到一条错误消息,看起来就像 JDK-8038142 中的错误消息。因此,您遇到的问题很可能是由相同的 Java 错误引起的(应该在最新版本中修复,但由于某种原因它仍然对我不起作用)。

解决 JDK 问题的一种简单方法是将 createBarcode 方法更改为 return 和 BufferedImage 而不是 PNG 图像数据。您只需要

return MatrixToImageWriter.toBufferedImage(bm);