I have a pdf file that contains images and I want to reduce its size in order to upload it to a site with a size limit.
So, how can I reduce the size of a pdf file from the command-line?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI have a pdf file that contains images and I want to reduce its size in order to upload it to a site with a size limit.
So, how can I reduce the size of a pdf file from the command-line?
You can use gs
- GhostScript (PostScript and PDF language interpreter and previewer) as follows:
-sDEVICE=pdfwrite
-dPDFSETTINGS
.From Documentation:
-dPDFSETTINGS=
configuration
Presets the "distiller parameters" to one of four predefined settings:
- /screen selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
- /ebook selects medium-resolution output similar to the Acrobat Distiller "eBook" setting.
- /printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
- /prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
- /default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.
-o
option to output file which also set -dNOPAUSE
and -dBATCH
(see Interaction-related parameters)Example:
$ du -h file.pdf
27M file.pdf
$ gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -q -o output.pdf file.pdf
$ du -h output.pdf
900K output.pdf
Here -q
suppress normal startup messages, and also do the equivalent of -dQUIET
which suppresses routine information comments
tesseract
remains completely unchanged when attempting to shrink it with this command.
Dec 30, 2019 at 17:44
-compress jpeg -quality 50
type of workaround (additionally -resize 75%
) with convert
command.
-sPAPERSIZE=a4 -dPDFFitPage
because /ebook
etc. only shrinks it according to page size
ps2pdf input.pdf output.pdf
I got the answer from ask ubuntu and it worked for me. It actually reduced 18.1Mb to 1.0Mb
tesseract
remains completely unchanged when attempting to shrink it with this command.
Dec 30, 2019 at 17:46
gs
, it gives this example: ps2pdf -dPDFSETTINGS=/prepress figure.ps proof.pdf
Apr 28, 2020 at 23:11
If the PDF file consists entirely of image data;
pdftk inputFile.pdf burst
mogrify -density 300 -format jpg -quality 20 pg_*.pdf
convert *.jpg -auto-orient outputFile.pdf
The density value can match the density of the source image (eg 300 dpi), but the jpeg quality should be lower than the source image (e.g. 20).
Following a pair of recipes derived from the answers of @freeman, @Pandya, and the comments of @CrisLuengo and @Phil294:
ps2pdf -dPDFSETTINGS=/ebook in.pdf out.pdf
in this case, the IMGs are reduced according to the size of output.
If your intention is to produce an output of a different size (Ex make a a4.pdf from a A2 Poster) you may provide extra options:
ps2pdf -sPAPERSIZE=a4 -dPDFFitPage -dPDFSETTINGS=/ebook a2poster.pdf a4out.pdf
See man gs
for more options.
You can try this :
$ time pdftk myFile.pdf output myFile__SMALLER.pdf compress
GC Warning: Repeated allocation of very large block (appr. size 16764928):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 8384512):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 11837440):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 8384512):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 33525760):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 7254016):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 34041856):
May lead to memory leak and poor performance.
GC Warning: Repeated allocation of very large block (appr. size 33525760):
May lead to memory leak and poor performance.
real 0m23.677s
user 0m23.142s
sys 0m0.540s
$ du myFile*.pdf
108M myFile.pdf
74M myFile__SMALLER.pdf
It is faster than gs
but compresses upto 30% in this case for a 107.5MiB input file.
tesseract
remains completely unchanged when attempting to shrink it with this command.
Dec 30, 2019 at 17:47