pdf - 如何在 JasperReports 中压缩 PDF

标签 pdf properties compression 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();

最佳答案

这可能是一个老问题,但我花了一些时间研究如何压缩 PDF 文件,我想分享答案......

setParameter 方法替代方案已被弃用,为了实现同样的功能,文档表明您应该使用 PdfExporterConfiguration 的实现。一个简单的名为 SimplePdfExporterConfiguration已提供并应按如下方式使用:

SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCompressed(true);

使用 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();

我设法将文件从 4 mb 压缩到 1.9 mb,如果有人知道更好的替代方案或更好的方法,请评论此答案。

关于pdf - 如何在 JasperReports 中压缩 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509688/

相关文章:

python - Python修改pdf并进行数字签名

pdf - 那里有质量好的 djvu 到 pdf 转换器吗?

unit-testing - 如何设计用于生成 PDF 文档的单元测试?

python - 属性(property)似乎没有以正确的方式运作

java - 如何在 SQL.properties 文件中的多行中添加 SQL

groovy - 在 groovy 中查找闭包的自由变量的列表/映射

pdf - 将MathematicalPI符号名称转换为Unicode

SAS 解压缩单个 sas7bdat 文件

c# - 使用 dotnet core 解压缩 .taz 文件

asp.net - 在数据库表中存储字节数组的最节省空间的方法 - ASP.NET