Pratap Kate Pratap Kate - 2 years ago 165
Java Question

Java Call JasperReports print issue

I have made an application in Java. For the application bill receipt is required and I have used JasperReports (.jrxml ) file.

I have used JasperReports call in Java code for the generating bill.

When I click on print bill the print is not proper, size is reduced.

Actually the problem is strange. I am working on desktop computer and when I print from it , the print is ok.. but when I move my project to other system (say, Laptop) The print I get is approx 50% reduced than the original print. Is there any problem with iReport?

I have used below Java code:

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");

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

ExporterInput exporterInput = new SimpleExporterInput(print);
// ExporterInput

// ExporterOutput
OutputStreamExporterOutput exporterOutput = new SimpleOutputStreamExporterOutput(
// Output

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();


How to resolve this issue

Answer Source

I was facing the same issue. It is kind weird but when I search at Jasper Community I had found the solution

The code solved the problem

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);

Hope this will help!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download