【问题标题】:Java Call JasperReports print issueJava 调用 JasperReports 打印问题
【发布时间】:2016-06-03 21:41:21
【问题描述】:

我已经用 Java 制作了一个应用程序。对于应用程序账单收据是必需的,我使用了 JasperReports (.jrxml) 文件。

我在 Java 代码中使用 JasperReports 调用来生成账单。

当我点击打印账单时,打印不正确,尺寸减小。

其实这个问题很奇怪。我在台式电脑上工作,当我从它打印时,打印还可以。但是当我将我的项目移动到其他系统(比如笔记本电脑)时,我得到的打印比原始打印减少了大约 50%。 iReport有什么问题吗?

我用过下面的Java代码:

public static void main(String[] args) throws JRException,
        ClassNotFoundException, SQLException {

    String reportSrcFile = "F:/Bills/FirstJasperReport.jrxml";

    // First, compile jrxml file.
    JasperReport jasperReport =    JasperCompileManager.compileReport(reportSrcFile);

    Connection conn = ConnectionUtils.getConnection();

    // Parameters for report
    Map<String, Object> parameters = new HashMap<String, Object>();

    JasperPrint print = JasperFillManager.fillReport(jasperReport,
            parameters, conn);

    // Make sure the output directory exists.
    File outDir = new File("C:/jasperoutput");
    outDir.mkdirs();

    // PDF Exportor.
    JRPdfExporter exporter = new JRPdfExporter();

    ExporterInput exporterInput = new SimpleExporterInput(print);
    // ExporterInput
    exporter.setExporterInput(exporterInput);

    // ExporterOutput
    OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
            "C:/jasperoutput/FirstJasperReport.pdf");
    // Output
    exporter.setExporterOutput(exporterOutput);

    //
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    exporter.setConfiguration(configuration);
    exporter.exportReport();

    System.out.print("Done!");
}

如何解决此问题

【问题讨论】:

  • When I click on print bill the print is not proper, size is reduced - 你是什么意思?我认为您的报告模板中存在这个问题

标签: java jasper-reports


【解决方案1】:

我遇到了同样的问题。这有点奇怪,但是当我搜索 Jasper Community 时,我找到了解决方案

代码解决了问题

PrintRequestAttributeSet printRequestAttrs = new HashPrintRequestAttributeSet();
printRequestAttrs.add(new PrinterResolution(600, 600, ResolutionSyntax.DPI)); // this resolution solved the problem
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, report);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttrs);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
exporter.exportReport();

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您是否尝试在打印前检查打印机属性?可能您的报告模板尺寸与打印机上设置的纸张尺寸不匹配。

    【讨论】:

    • 其实这个问题很奇怪。我在台式电脑上工作,当我从它打印时,打印还可以。但是当我将我的项目移动到其他系统(比如笔记本电脑)时,我得到的打印比原始打印减少了大约 50%。 iReport有问题吗?我还编辑了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2021-08-17
    • 2015-01-04
    • 2011-09-02
    • 2020-06-20
    • 2017-09-09
    相关资源
    最近更新 更多