27

I just learned that PDF files can be compressed to reduce their 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
    
3
  • 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, 2011 at 17:21
  • 1
    @polemon: Thanks! It doesn't. The out put of $file 3.pdf is 3.pdf: PDF document, version 1.4
    – Tim
    Jul 30, 2011 at 17:26
  • 1
    Note that individual streams in the PDF file can be compressed. So asking "is the whole PDF file compressed" is the wrong question (though in many cases all, or most streams will be either compressed or uncompressed).
    – dirkt
    Dec 26, 2018 at 8:49

4 Answers 4

46

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.

3
  • 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, 2011 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, 2011 at 14:10
  • Great! This way I was able to edit a PDF file using nvim, adding some text.
    – xdevs23
    Jun 1, 2020 at 10:50
10

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

$ pdftk test.pdf output compressed_test.pdf compress
8
  • Thanks! I was wondering how to know if a pdf file has already been compressed?
    – Tim
    Jul 30, 2011 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, 2011 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, 2011 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, 2011 at 16:33
  • @Tim: see also this: pandemoniumillusion.wordpress.com/2008/05/07/…
    – nico
    Jul 30, 2011 at 16:43
1

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.

2
  • This is rather iffy. I used your method to check an uncompressed PDF (uncompressed by qpdf) file using grep as in grep -E "(CCITT|JPEG|Flate|LZW|RLE|ZIP)" uncompressed.pdf and it returns this message Binary file uncompressed.pdf matches. Looks like it detects the uncompressed.pdf file to contain both LZW and RLE patterns.
    – user91822
    Jun 29, 2019 at 13:20
  • Grepping (or, in the editor, searching) for /Filter as explained in @Philomath's answer should also find the compressed streams. BTW: GNU grep has a --binary-file=text option, which prints the matching lines even if grep believes it to be binary. Pipe the output to less to avoid overlong lines or weird escape sequences from overwhelming your terminal. Oct 18, 2021 at 8:56
-1

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

1
  • I think the question Heidi is what program to check the properties with, in particular with scriptable command line based programs.
    – Caleb
    Jul 30, 2011 at 15:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .