How do you compress a PDF that contains images?


I'm fairly new to JasperReports so I apologize now. 

I'm currently using iReports 5.6.0 in a Linux environment.

I'm having difficulty compressing my PDF. It seems fine if I do not include any images but the moment I do JasperReports seems to iqnore my compression settings.

My Java code looks like this:

if (jasperReport != null) {
            JRPdfExporter pdfExporter = new JRPdfExporter();
            SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();

            pdfExporter.setExporterInput(new SimpleExporterInput(jasperReport));
            pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfFileDest));

and my XML for my image object inside my report looks like this:

                    <jr:column width="270" uuid="ba4b6dd8-4345-4bf9-ac45-040f657f1eb5">
                        <jr:detailCell height="200" rowSpan="1">
                            <image scaleImage="RetainShape" isUsingCache="true">
                                <reportElement positionType="Float" isPrintRepeatedValues="false" x="0" y="11" width="264" height="184" isRemoveLineWhenBlank="true" isPrintWhenDetailOverflows="true" uuid="ea12b5e6-69bb-4161-81fb-268c28d59c73">

As you can see my image is displayed inside of a table.

I should also mention that my report consists of a main report and one sub-report. The sub-report is responsible for displaying the images and contains the XML above.
I have included the property:
<property name="net.sf.jasperreports.export.pdf.compressed" value="true"/>
in both reports.

What am I missing or doing wrong?

mark_37's picture
Joined: Jan 11 2016 - 6:07am
Last seen: 3 years 10 months ago

2 Answers:


Hi Newfoundland Mark, Understand that pdf format is portable document format.  It is called this becuase it is intended to be a portable format for use on the web.  For a reference compare the file size of a pdf and a postScript rendition of the same file, the pdf will be smaller.  With that said, when you add the images to the pdf file, you should use images with a good compression, for example 1. tiff group IV, 2. gif: supports only 256 colors, lossless, good for text images, line drawings, animation.  gif can be large and not photo quality.  3. png: great for logos, lossless, not great for large images compared to jpeg, not for animated images, 24 bit png ssupports millions of colors, 8 bit png has same 256 color limit as gif.  4. jpeg good option for photos, not recommended for line art and logos, jpeg supports broad compression based on loss you are ok with.

An option is to compress the pdf after you create it in your java code: something like `gzip -9 $image` will create a .gz file with maximum compression.  This will work on all Linux/unix platforms.

For your java code, you could also try something like this: 

import java.util.Arrays;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDStream;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
public class compressPDF {
public static void main (String[] args) throws IOException, DocumentException, COSVisitorException {
// Using PDF Box 
PDDocument doc; // = new PDDocument();
doc = PDDocument.load("C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF");
PDStream stream= new PDStream(doc);
// or Using itext
PdfReader reader = new PdfReader("C:/_dev_env_/TEMP/compressPDF/TRPT_135002_1470_20131212_121423.PDF");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/_dev_env_/TEMP/compressPDF/compressed_Itext.pdf"), PdfWriter.VERSION_1_5);
int total = reader.getNumberOfPages() + 1;
for (int i = 1; i < total; i++) {
reader.setPageContent(i, reader.getPageContent(i));
java image pdf itext pdfbox



christopher.g.kennedy's picture
Joined: Mar 18 2015 - 1:10pm
Last seen: 3 years 2 weeks ago

Thanks Chris!

mark_37's picture
Joined: Jan 11 2016 - 6:07am
Last seen: 3 years 10 months ago