2017-10-10 60 views
1

我们遇到了一个奇怪的问题,试图从Mac打印条形码。一切工作正常的Windows,但是,在Mac上:Jasperreports在Mac OS X上不打印条形码

  • 碧玉预览显示条形码细
  • 通过碧玉
  • 保存PDF显示条形码罚款和PDF打印正确
  • 打印报表不包括条码
  • 通过保存PDF Mac打印机对话框不包含条形码

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

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

我的Mac当前运行的是java 10的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> 

回答

0

我跑了你的报告,得到了,看起来就像一个错误信息一个来自JDK-8038142。因此,您遇到的问题很可能是由同一个Java错误引起的(应该在最新版本中修复,但由于某种原因,它仍然不适用于我)。

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

return MatrixToImageWriter.toBufferedImage(bm);