Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I just learned that pdf files can be compressed to reduce its disk size.

  1. I was wondering how to know if a pdf file has already been compressed?
  2. What applications/commands can be used to compress or uncompress a pdf file?

My environment is Linux Ubuntu 10.10.


Some attempts don't give satisfactory results:

  1. Here are the results of trying pdftk:

    $ pdftk 3.pdf output 5.pdf uncompress 
    $ pdftk 3.pdf output 3comp.pdf compress 
    $ ls -l 3.pdf 3comp.pdf 5.pdf
    -rwxrwx--- 1 root plugdev  8652269 2011-07-30 12:27 3comp.pdf
    -rwxrwx--- 1 root plugdev  8652319 2011-07-29 22:15 3.pdf
    -rwxrwx--- 1 root plugdev 16829828 2011-07-30 12:27 5.pdf
    

    Properties of the files show that all of them are not optimized.

  2. Results of converting to ps and then back to pdf:

    $ pdf2ps 3.pdf 3.ps
    $ ps2pdf 3.ps 3c.pdf
    $ ls -l 3.pdf 3.ps 3c.pdf
    -rwxrwx--- 1 root plugdev   8808946 2011-07-30 13:14 3c.pdf
    -rwxrwx--- 1 root plugdev   8652319 2011-07-29 22:15 3.pdf
    -rwxrwx--- 1 root plugdev 122375966 2011-07-30 13:14 3.ps
    
share|improve this question
    
I can't test it right now, since I don't have a compressed PDF at hand, but try file. It should show whether the file is compressed or not. –  polemon Jul 30 '11 at 17:21
    
@polemon: Thanks! It doesn't. The out put of $file 3.pdf is 3.pdf: PDF document, version 1.4 –  Tim Jul 30 '11 at 17:26

4 Answers 4

up vote 10 down vote accepted

in short:

To know if it's compressed already:

strings your.pdf | grep /Filter

To (un)compress a PDF, use QPDF

qpdf --stream-data=compress your.pdf compressed.pdf
qpdf --stream-data=uncompress compressed.pdf uncompressed.pdf  

explanation:

The "Filter" keyword inside a pdf file is a indicator of the compression method used. Some of them are:

CCITT G3/G4 – used for monochrome images
JPEG – a lossy algorithm that is used for images
JPEG2000 – a more modern alternative to JPEG, which is also used for compressing images
Flate – used for compressing text as well as images
JBIG2 – an alternative to CCITT compression for monochrome images
LZW – used for compressing text as well as images but getting replaced by Flate
RLE – used for monochrome images
ZIP – used for grayscale or color images

(copied from here).

However, given the PDF complex file structure, most of the time some part (or "stream") of the PDF will be compressed already in some way (and will show up when grepping /Filter) while some other part will not be, so there is no YES / NO answer to the question whether the PDF is compressed.
one way to overcome this would be to add the -c option to grep, which returns the number of occurrences, so you could see relatively how well it is compressed. for example, if strings "large.pdf" | grep -c /Filter returns less then 10 it's pretty non-compressed.

Another property relating to size in PDFs, is whether they have been optimized for quick access, with "optimized" PDFs being bigger in size, to quote from wikipedia:

There are two layouts to the PDF files—non-linear (not "optimized") and linear ("optimized"). Non-linear PDF files consume less disk space than their linear counterparts, though they are slower to access because portions of the data required to assemble pages of the document are scattered throughout the PDF file. Linear PDF files (also called "optimized" or "web optimized" PDF files) are constructed in a manner that enables them to be read in a Web browser plugin without waiting for the entire file to download, since they are written to disk in a linear (as in page order) fashion. PDF files may be optimized using Adobe Acrobat software or QPDF.

You can check whether the PDF is optimized using pdfinfo your.pdf.

share|improve this answer
    
Thanks! (1) The results of using qpdf is similar to using pdftk: with compress, the size actually became a little bigger (strange), and with uncompress, the size became much bigger, almost doubled. (2) Do pdftk and gpdf try to accomplish the same thing, using compress and uncompress options? –  Tim Aug 4 '11 at 3:18
    
@Tim: (sorry I wasn't here). for (1), as I explained, the PDF is partially compressed already (most probably), that's why compressing doesn't save too much, (and decompressing makes it much bigger, becouse it decompresses all the compressed streams). (2) most probably qpdf and pdftk do more-or-less the same thing regarding compression, but qpdf can optimize too (plus many more things). –  Philomath Aug 7 '11 at 14:10

pdftk is a tool to perform some operations on PDF files, like compression/decompression:

$ pdftk test.pdf output compressed_test.pdf compress
share|improve this answer
    
Thanks! I was wondering how to know if a pdf file has already been compressed? –  Tim Jul 30 '11 at 13:05
1  
@Tim: pdfinfo file.pdf tells you if a PDF is optimized. However, I am not sure whether that means compressed or not. –  nico Jul 30 '11 at 14:58
1  
@uloBasEI: I tried your command using pdftk for compression, but the size almost not changed. If I uncompress, the file size double. The properties of the original file, compressed file and uncompressed files all show they are not optimized. –  Tim Jul 30 '11 at 16:30
    
@nico: Thanks! See my comment above and my update. It seems that compression and optimization do not imply each other? –  Tim Jul 30 '11 at 16:33
    
@Tim: see also this: pandemoniumillusion.wordpress.com/2008/05/07/… –  nico Jul 30 '11 at 16:43

Just check its properties; it will say if the file is compressed or not.

share|improve this answer
    
I think the question Heidi is what program to check the properties with, in particular with scriptable command line based programs. –  Caleb Jul 30 '11 at 15:54

I found the compression method in the pdf file. Open the PDF with text editor. Run a Search or Find -enter CCITT if not found, enter JPEG, then Flate, then JBIG2, then LZW, then RLE, and then ZIP.
Sounds worse than it is! Very easy to find the data stream's compression method.

Hope this helps! Thanks for the post...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.