开发者

How to reduce size of BIRT generated PDF with background image?

开发者 https://www.devze.com 2022-12-24 05:54 出处:网络
It seems (at least that is our understanding of the issue at this point) that given a background image in BIRT, the PDF generation routine repeats the image information inside the PDF instead of havin

It seems (at least that is our understanding of the issue at this point) that given a background image in BIRT, the PDF generation routine repeats the image information inside the PDF instead of having it o开发者_如何学Pythonnce referenced as the background image of all pages of the PDF.

This causes our BIRT generated PDFs to be too large. Is there a way to get BIRT to only store the image once inside the PDF and have it be the background image on each page of the PDF while keeping the file size more reasonable?


Yes, BIRT will create a new incarnation of the same background image for each page. Unfortunately this is too deeply buried in org/eclipse/birt/report/engine/emitter/pdf/PDFPage.java to easily hack around nicely, and is most definitely not configurable.

But despair not! You can use iText (the same iText used internally by BIRT) to eliminate duplicate entities with PdfSmartCopy. Something along the lines of:

PdfReader reader  = new PdfReader(PDF_IN_FROM_BIRT);
Document document = new Document();
PdfSmartCopy copy = new PdfSmartCopy(document,
  new FileOutputStream(PDF_OUT_NO_DUPLICATES));
document.open();
for (int page = 1; page <= reader.getNumberOfPages(); page++) {
  copy.addPage(copy.getImportedPage(reader, page));
}
document.close();

If the above is not acceptable, then you can patch com.itextpdf.text.Image.getInstance(URL) to behave in a singleton-like manner, returning an existing com.itextpdf.text.Image object if the image had already been obtained from the same URL in the past. You can patch and rebuild the iText JAR used by BIRT, or use Javassist to perform code injection at runtime without a need for recompilation.

0

精彩评论

暂无评论...
验证码 换一张
取 消