tra*_*ega 4 compression pdf properties jasper-reports
如何将IS_COMPRESSED = true属性应用于Jasper PDF报告?
这就是我所拥有的,但是当我创建PDF报告时,它的大小与不启用压缩的克隆文件相同:
File pdfFile = new File (pdfDirectory.getAbsolutePath() + File.separator + reportName + ".pdf");
File jrPrintFile = new File(jrprintFileLocation.getAbsolutePath() + File.separator + templateName + ".jrprint");
JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(jrPrintFile);
JRPdfExporter jrPdfExporter = new JRPdfExporter();
jrPdfExporter.setParameter(JRPdfExporterParameter.IS_COMPRESSED, true);
jrPdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrPdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile.getAbsolutePath());
jrPdfExporter.exportReport();
Run Code Online (Sandbox Code Playgroud)
这可能是一个古老的问题,但是我花了一些时间寻找如何压缩PDF文件,我想分享答案。
该的setParameter方法替代已被弃用,为了做同样的,该文件表明,你应该使用的实现PdfExporterConfiguration。提供了一个简单的名为SimplePdfExporterConfiguration的应用程序,应该这样使用:
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCompressed(true);
Run Code Online (Sandbox Code Playgroud)
使用XML作为源的PDF生成的完整示例:
Document document = JRXmlUtils.parse(JRLoader.getLocationInputStream(PATH_XML));
//report parameters
Map params = new HashMap();
//document as a parameter
params.put(JRXPathQueryExecuterFactory.PARAMETER_XML_DATA_DOCUMENT, document);
//other parameters needed for the report generation
params.put("parameterName", parameterValue);
JasperPrint jasperPrint = JasperFillManager.fillReport(PATH_JASPER, params);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(PATH_PDF));
//configuration
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCompressed(true);
//set the configuration
exporter.setConfiguration(configuration);
//finally exporting the PDF
exporter.exportReport();
Run Code Online (Sandbox Code Playgroud)
我设法将文件从4 mb压缩到1.9 mb,如果有人知道更好的替代方法或更好的方法,请评论此答案。
| 归档时间: |
|
| 查看次数: |
4892 次 |
| 最近记录: |