gpt4 book ai didi

Java调用JasperReports打印问题

转载 作者:行者123 更新时间:2023-12-02 03:34:27 24 4
gpt4 key购买 nike

我用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!");
}

如何解决这个问题

最佳答案

我也遇到了同样的问题。这有点奇怪,但是当我搜索 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();

希望这会有所帮助!

关于Java调用JasperReports打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com