Jump to content
Changes to the Jaspersoft community edition download ×

How do you compress a PDF that contains images?


mark_37

Recommended Posts

Hi,
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();
            configuration.setCompressed(true);

            pdfExporter.setConfiguration(configuration);
            pdfExporter.setExporterInput(new SimpleExporterInput(jasperReport));
            pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfFileDest));
                       
            pdfExporter.exportReport();
}

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">
                                    <printWhenExpression><![CDATA[$F{selected}.booleanValue()]]></printWhenExpression>
                                </reportElement>
                                <imageExpression><![CDATA[$F{filePath}]]></imageExpression>
                            </image>
                        </jr:detailCell>
                    </jr:column>

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?

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

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.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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);
stream.addCompression();
doc.save("C:/_dev_env_/TEMP/compressPDF/compressed_pdfBox.pdf");
doc.close();
// 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);
stamper.setFullCompression();
stamper.getWriter().setCompressionLevel(50);
int total = reader.getNumberOfPages() + 1;
for (int i = 1; i < total; i++) {
reader.setPageContent(i, reader.getPageContent(i));
}
stamper.close();
reader.close();
}
}
java image pdf itext pdfbox

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...