Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've got a PDF of a scanned document that's around 20MB and I need to drastically reduce the filesize to be able to email it. I've tried

gs -sDevice=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

but output.pdf isn't created. Instead, I get an X Viewer for Ghostscript briefly. Where am I going wrong?

share|improve this question
Please see this related Q&A for a number of GUI front ends to ghostscript that should make the process of reducing PDF filesizes easier. – Glutanimate Apr 11 '13 at 21:58
possible duplicate of How to reduce pdf filesize? – Eliah Kagan Jul 31 '14 at 19:43
How can this be a duplicate of that other question when it predates it by two years? – Rob Cowell Aug 1 '14 at 8:14
up vote 18 down vote accepted

Here is a script for rewriting scanned pdfs:


gs  -q -dNOPAUSE -dBATCH -dSAFER \
    -sDEVICE=pdfwrite \
    -dCompatibilityLevel=1.3 \
    -dPDFSETTINGS=/screen \
    -dEmbedAllFonts=true \
    -dSubsetFonts=true \
    -dColorImageDownsampleType=/Bicubic \
    -dColorImageResolution=72 \
    -dGrayImageDownsampleType=/Bicubic \
    -dGrayImageResolution=72 \
    -dMonoImageDownsampleType=/Bicubic \
    -dMonoImageResolution=72 \
    -sOutputFile=out.pdf \

You could customise it a bit to make it more reusable but if you only have one pdf, you could just replace $1 with your pdf filename and bung it in a terminal.

share|improve this answer
Works a treat, thanks Oli. You've answered pretty much everything I've asked on here so far :-D – Rob Cowell Sep 1 '10 at 8:15
This is a good answer but in my case at least it takes a lot of time to convert a somewhat large (>10Mb) PDF file (more than a minute). – Gabriel Jun 12 '13 at 19:20
I'm not sure what happens, but a 30 MB PDF results a 68 MB file. Instead of reducing, it enlarges. Same output if using directly ps2pdf as stated in next answer. – Ed Villegas Jun 23 '13 at 18:08
@EdVillegas The only thing I can think of (to explain that sort of increase) is that the images are of a lower resolution than the ones being generated (72dpi). Or somehow embedding the fonts is sucking in all the fonts. – Oli Jun 25 '13 at 7:31

I usually use ps2pdf to do this (easier syntax), something like this:

ps2pdf -dPDFSETTINGS=/ebook BiggerPdf SmallerPDF

I use the following python script to reduce the size of all the pdf files in a dir in a production server (8.04). So it should work.


import os

for fich in os.listdir('.'):
        if fich[-3:]=="pdf":
                os.system("ps2pdf -dPDFSETTINGS=/ebook %s reduc/%s" % (fich,fich))
share|improve this answer
Thanks for the alternative solution. I tried Oli's first and it gave me the result I needed, but I will keep this one for future reference too. – Rob Cowell Sep 1 '10 at 8:17

If converting to djvu would also be ok and if no colors are involved, you could try the following:

Convert the pdf to jpg files using pdfimages -j

If you get pbm files instead, you should do the intermediate step:

for FILENAME in $(ls *.pbm); do convert $FILENAME ${FILENAME%.*}.jpg ;done

The convert command is from the imagemagick package.

Then use scantailor to make tif's out of it.

In a last step you go to scantailors out direcory (where the tif's are located) and apply djvubind to that directory.

This should reduce the filesize drastically without big quality loss of the text. If you want finer control over the ocr-backend, you may try djvubind --no-ocr and use ocrodjvu to add the ocr layer afterwards.

If you have color's in your document things get a bit more complicated. Instead of djvubind you could use didjvu and in scantailor you have to change to mixed mode and select sometimes color-images manually.

share|improve this answer

Control the compression quality:

INPUT=$1; shift
OUTPUT=$1; shift

# Image Compression Quality
# Quality HSamples VSamples QFactor
# Minimum [2 1 1 2] [2 1 1 2] 2.40
# Low     [2 1 1 2] [2 1 1 2] 1.30
# Medium  [2 1 1 2] [2 1 1 2] 0.76
# High    [1 1 1 1] [1 1 1 1] 0.40
# Maximum [1 1 1 1] [1 1 1 1] 0.15 

${GS_BIN} -dBATCH -dSAFER -DNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=${OUTPUT} -c "<< /ColorImageDict << /QFactor ${QFACTOR} /Blend 1 /HSample [1 1 1 1] /VSample [1 1 1 1] >> >> setdistillerparams" -f ${INPUT}
share|improve this answer both INPUT and OUTPUT are the same argument? You might want to add usage guidelines. – mikewhatever Apr 2 at 11:46
Note the shift. First parameter is input file, second is the output file and rest of the parameters will be passed to gs as is. – Mikko Rantalainen May 13 at 12:53

Your Answer


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.