How to compress PDF in JasperReports

How to apply a property IS_COMPRESSED = trueto a Jasper PDF report?

This is what I have, but when I create the report in PDF format, it has the same size as the clone without compression:

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();
+3
source share
2 answers

This may be an old question, but I spent some time on how to compress PDF files and I want to share it ...

An alternative to the setParameter method is deprecated, and in order to do the same, the documentation indicates that you should use the PdfExporterConfiguration implementation . Provided with a simple SimplePdfExporterConfiguration and should be used as follows:

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

PDF XML :

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 1,9 , - , .

+2

, PDF, , , (net.sf.jasperreports.export.pdf.compressed) "true".

PDF, , 4 1 . .

0

All Articles