I use pdftk to compress a pdf using the following command line
pdftk file1.pdf output file2.pdf compress
It works as the w
pdf2ps large.pdf small.pdf
is enough, instead of two steps
pdf2ps large.pdf very_large.ps
ps2pdf very_large.ps small.pdf
However, ps2pdf large.pdf small.pdf
is a better choice.
ps2pdf
is much fasterpdf2ps
sometimes produces larger file.I had the same problem and found two different solutions (see this thread for more details). Both reduced the size of my uncompressed PDF dramatically.
Pixelated (lossy):
convert input.pdf -compress Zip output.pdf
Unpixelated (lossless, but may display slightly differently):
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=output.pdf input.pdf
Edit: I just discovered another option (for lossless compression), which avoids the nasty gs command. qpdf is a neat tool that converts PDFs (compression/decompression, encryption/decryption), and is much faster than the gs command:
qpdf --linearize input.pdf output.pdf
I had the same issue and I used this function to compress individual pages which results in the file size being compressed by upto 1/3 of the original size.
for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}
I didn't see a lot of reduction in file size using qpdf. The best way I found is after pdftk is done use ghostscript to convert pdf to postscript then back to pdf. In PHP you would use exec:
$ps = $save_path.'/psfile.ps';
exec('ps2ps2 ' . $pdf . ' ' . $ps);
unlink($pdf);
exec('ps2pdf ' .$ps . ' ' . $pdf);
unlink($ps);
I used this a few minutes ago to take pdftk output from 490k to 71k.
The one-line pdf2ps option (by Lee) actually increased the pdf size. However, the two steps one did better. And it can be combined in a single one using redirection from & to standard input/output and pipes:
pdf2ps large.pdf - | ps2pdf - small.pdf
did reduce a PDF generated by xsane from 18 Mo to 630 ko!
Links are lost, but for the present example, it's not a concern... and was the easiest way to achieve the desired result.
Trying to compress a PDF I made with 400ppi tiffs, mostly 8-bit, a few 24-bit, with PackBits compression, using tiff2pdf
compressed with Zip/Deflate. One problem I had with every one of these methods: none of the above methods preserved the bookmarks TOC that I painstakingly manually created in Acrobat Pro X. Not even the recommended ebook
setting for gs
. Sure, I could just open a copy of the original with the TOC intact and do a Replace pages
but unfortunately, none of these methods did a satisfactory job to begin with. Either they reduced the size so much that the quality was unacceptably pixellated, or they didn't reduce the size at all and in one case actually increased it despite quality loss.
pdftk compress
:
no change in size
bookmarks TOC are gone
gs screen
:
takes a ridiculously long time and 100% CPU
errors:
sfopen: gs_parse_file_name failed. ?
| ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->10.2MB hideously pixellated
bookmarks TOC are gone
gs printer
:
takes a ridiculously long time and 100% CPU
no errors
74.8MB-->66.1MB
light blue background on pages 1-4
bookmarks TOC are gone
gs ebook
:
errors:
sfopen: gs_parse_file_name failed.
./base/gsicc_manage.c:1050: gsicc_open_search(): Could not find default_rgb.ic
| ./base/gsicc_manage.c:1651: gsicc_set_device_profile(): cannot find device profile
74.8MB-->32.2MB
badly pixellated
bookmarks TOC are gone
qpdf --linearize
:
very fast, a few seconds
no size change
bookmarks TOC are gone
pdf2ps
:
took very long time
output_pdf2ps.ps 74.8MB-->331.6MB
ps2pdf
:
pretty fast
74.8MB-->79MB
very slightly degraded with sl. bluish background
bookmarks TOC are gone