-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJasperToHtmlConverter.java
52 lines (41 loc) · 2.08 KB
/
JasperToHtmlConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleHtmlReportConfiguration;
import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class JasperToHtmlConverter {
public static void main(String[] args) {
try {
String jasperFilePath = args[0];
String outputHtmlFilePath = args[1];
Map<String, Object> parameters = new HashMap<>();
for (int i = 2; i < args.length; i++) {
String[] param = args[i].split("=", 2);
if (param.length == 2) {
String key = URLDecoder.decode(param[0], StandardCharsets.UTF_8.toString());
String value = URLDecoder.decode(param[1], StandardCharsets.UTF_8.toString());
parameters.put(key, value);
}
}
JRDataSource dataSource = new JREmptyDataSource();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFilePath, parameters, dataSource);
HtmlExporter exporter = new HtmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(new FileOutputStream(outputHtmlFilePath)));
SimpleHtmlReportConfiguration reportConfig = new SimpleHtmlReportConfiguration();
reportConfig.setEmbedImage(true);
reportConfig.setRemoveEmptySpaceBetweenRows(true);
reportConfig.setWhitePageBackground(false);
exporter.setConfiguration(reportConfig);
exporter.exportReport();
System.out.println("HTML file generated successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
}