Jasperreports不在Mac OS X上打印条形码

时间:2017-10-10 23:38:14

标签: java macos jasper-reports barcode zxing

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

  • Jasper预览显示条形码正常
  • 通过jasper保存PDF显示条形码正常,pdf打印正确
  • 打印报告不包括条形码
  • 通过Mac打印机保存PDF对话框不包括条形码

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

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

我的Mac目前使用java 8运行10.12.6。

感谢。

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

1 个答案:

答案 0 :(得分:0)

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

解决JDK问题的一种简单方法是更改​​createBarcode方法以返回BufferedImage而不是PNG图像数据。你需要的只是

return MatrixToImageWriter.toBufferedImage(bm);
相关问题