#1 2011-07-03 20:18:09

codetracker
Member
Registered: 2011-07-03
Posts: 5

Deflate compression of PDF

Just wondering if you can set the level of deflate compression (zlib) in a PDF ie level 1 - 9 and if so how you can do that in code?

Offline

#2 2011-07-03 20:40:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: Deflate compression of PDF

By default, the level is 7.

See

procedure TPdfStream.InternalWriteTo(var W: TPdfWrite);
(...)
      try
        TmpSize := CompressStream(TMemoryStream(FWriter.fDestStream).Memory,
          TmpSize,TmpStream,7,true);
(...)

You can change 7 here by whatever number.

What's wrong with 7?

Offline

#3 2011-07-04 07:38:14

codetracker
Member
Registered: 2011-07-03
Posts: 5

Re: Deflate compression of PDF

Thank you for that. I am just trying to squeeze the pdf down as much as I can as we have terrible internet speeds at work and the pdf's can be large.

thanks again for your great work smile

Offline

#4 2011-07-04 08:16:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: Deflate compression of PDF

I'm note sure you'll see a huge difference between 7 and 9 (which is the best method).

At least, on file size.

But on production speed, 9 is much slower than 7.

I'm not sure it's worth it. But you may try your best!

You should better use JPEG compression for the bitmaps, or not embed true type fonts.

Offline

#5 2011-07-05 10:49:17

codetracker
Member
Registered: 2011-07-03
Posts: 5

Re: Deflate compression of PDF

I tried out the level 9 compression and it worked great and created a file about 10% smaller. I did experiment with jpeg but it was not as good with black and white 1 bit bitmaps and produced larger files. I think for color images, you are certainly right suggesting jpeg as the best option.

Once again thank you for you great effort in making this great tool and your quick help.

Offline

#6 2011-07-05 12:48:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: Deflate compression of PDF

For B&W pictures, I guess the ideal should be Fax-like picture encoding.
Or changing the bitmap resolution...

This could be added to the unit.

Sounds like a good idea to you?

Offline

#7 2011-07-05 18:53:43

TPrami
Member
Registered: 2010-07-06
Posts: 106

Re: Deflate compression of PDF

Hello,

At least I had on problem with very similar jpeg pictures, that there is no compression of whole file, it compressed very very well with any compressor. Because it contained very similar jpeg files (or at least I tried to compress it in jpeg, and it got smaller...)

I don't know is there an option to compression for the whole file, in Pdf...

.-TP-.

Offline

#8 2011-07-06 05:09:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: Deflate compression of PDF

TPrami wrote:

I don't know is there an option to compression for the whole file, in Pdf...

You can compress not the whole file (the catalog e.g. should be uncompressed), but at least some objects content at once. For the pictures, I'm not sure it's feasible.

I was never able to find out in the PDF reference document how to compress the whole content at once.
I remember that it's feasible since revision 1.7 of the format.
If you have some ideas, could be nice to implement...

Offline

#9 2011-07-07 10:55:37

codetracker
Member
Registered: 2011-07-03
Posts: 5

Re: Deflate compression of PDF

From what I understand  CCITT 4 is a very good option for black and white images and produces smaller files, but I have no idea as to how hard that is to implement.

Offline

#10 2011-07-07 14:38:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,207
Website

Re: Deflate compression of PDF

Yes, CCITT 4 is better than deflate for such images, AFAIK.

I could add this to the library (it's just some kind of huffman coding), but I'll have to find out time... wink

Offline

#11 2011-07-11 21:49:05

codetracker
Member
Registered: 2011-07-03
Posts: 5

Re: Deflate compression of PDF

It appears that the specifications for FAX ccit 4 are here http://www.itu.int/rec/T-REC-T.6-198811-I/en but I am not sure how they are implemented or stored in a PDF. If only we all had more time to do the thing we want. We are all time poor and wealthy in trivia that takes out time smile

Offline

#12 2015-01-25 06:36:45

hobbist
Member
Registered: 2015-01-25
Posts: 2

Re: Deflate compression of PDF

Hi. If someone is interested, ASyncPro (open source, http://sourceforge.net/projects/tpapro/) implements G3 compression (procedure acCompressRasterLine in AwFaxCvt.pas). As I understand it, G3 is a special case of G4 (CCITT 4), so it should work and should result in a good compression for pdfs with bitonal images, but unfortunately I was not able to use it to generate valid pdfs.

Last edited by hobbist (2015-02-01 05:38:01)

Offline

#13 2015-07-04 21:46:59

hobbist
Member
Registered: 2015-01-25
Posts: 2

Re: Deflate compression of PDF

In addition to my last post, there is also a lossless jbig2 free implementation, with a much better compression than CCITT, used in rubypdf, in http://soft.rubypdf.com/software/lossle … mic-libary.

Offline

Board footer

Powered by FluxBB