August 18, 2009 · General · Email This Post

If you want to reduce PDF file size using command line you can use ghostscript.Ghostscript is used for PostScript/PDF preview and printing. Usually as a back-end to a program such as ghostview, it can display PostScript and PDF documents in an X11 environment.

Furthermore, it can render PostScript and PDF files as graphics to be printed on non-PostScript printers. Supported printers include common dot-matrix, inkjet and laser models.

Package gsfonts contains a set of standard fonts for Ghostscript.

Use the following command syntax to reduce your pdf files

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

Replace your file names output.pdf,input.pdf files with your file names.

This is only example you can check man gs command for more options.

If you want to install ghostscript use the following command

sudo apt-get install ghostscript

Sponsored Link

Incoming search terms:

Related posts

41 Comments to “Ubuntu Tip:Howto reduce PDF file size from command line”

  1. g3n1uss says:

    My output file (66.5 KB) is bigger then input one (61.9 KB).

    [Reply]

    anon Reply:

    Come on, you want to shrink a pdf which has 61 kb? This isn’t small enough for you?

    [Reply]

  2. claudio says:

    “-dPDFSETTINGS=/screen” suggest that the file is optimized for screen instead of printing. If this is the case you should point out to the readers that they are making files smaller, but removing quality needed for printing.

    [Reply]

  3. James says:

    Unless this applies only to PDF files created by Adobe Acrobat (i.e. because they’re more bloated to begin with or something) I would suggest that your title should speak of “PDF file size” rather than “Adobe Acrobat file size”. Since 2008, PDF is an ISO standard and PDF viewing and creation may have nothing to do with Adobe software.

    [Reply]

  4. Tony says:

    Hi there, Many thanks for the tip.

    I tried reducing a PDF file of a scanned document of 2.5Mb, but the result was unreadable. (filesize 67Kb)

    I found that by leaving out -dPDFSETTINGS=/screen, I got a very acceptable filesize of 250K
    - readable
    - small enough to send by email

    Summary: leave out: -dPDFSETTINGS=/screen for better quality.

    [Reply]

  5. Anon says:

    This tends to munch special characters and embedded PDF images. :(

    [Reply]

  6. Seemanta says:

    Thanks!!! This was *exactly* what I was looking for! You made my day!!

    [Reply]

  7. Kerneels says:

    Thanx this really helped. Reduced my PDF from 32MB down to 2MB without any visible difference in quality. Ideal for uploading.

    [Reply]

  8. Swaprava says:

    Simply superb, that’s why I love command line so much

    [Reply]

  9. Sneza says:

    Hi,

    I have a scanned pdf 2 pages, 380.8 kb and it got reduced to 292.7 kb. I need it to be bellow 100 kb. Any suggestions? It is scanned text, so it should still be readable…

    Thanks in advance!

    [Reply]

  10. jerico says:

    Hi:

    Is there a way to use this command for multiple pdf files?

    Thanks!

    [Reply]

    Dave Reply:

    There was a typo in my first post…

    I know this is a bit old but I am sure someone out there is looking for this answer…I wrote this script to compress all the PDFs in a folder.
    ———————————–
    #!/bin/bash
    for file in *.pdf
    do
    echo $file #line not required
    temp=`echo “$file” | sed ‘s/.pdf/zqx.pdf/g’`
    echo $temp #line not required
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$temp $file
    mv $temp $file
    done
    ———————————–

    If you need a specific compression add the appropriate flag as shown in zweifel.
    -dPDFSETTINGS=/printer
    …etc.

    [Reply]

    Paul Reply:

    Another option is to use xargs…

    ls *.pdf | xargs -i{} gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile={}-compressed.pdf {}

    [Reply]

  11. Travis Poppe says:

    I had to use -dUseCIEColor to maintain the correct colors in the conversion process (by default, all of the shades turn out weird).

    If you’d like to fine-tune the compression settings beyond what the presets offer (-dPDFSETTINGS)=/preset), you can manipulate the parameters directly; a list of them can be found here: http://pages.cs.wisc.edu/~ghost/doc/cvs/Ps2pdf.htm

    I needed a target file size beyond what even /screen could produce, so I set the resolution to 38 (images really start to degrade at this point):

    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dColorImageResolution=38 -dColorImageDownsampleType=/Average -dGrayImageDownsampleType=/Average -dGrayImageResolution=38 -dMonoImageResolution=38 -dMonoImageDownsampleType=/Average -dOptimize=true -dDownsampleColorImages=true -dDownsampleGrayImages=true -dDownsampleMonoImages=true -dUseCIEColor -dColorConversionStrategy=/sRGB -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

    Optimal results may require some experimenting.

    [Reply]

  12. eremin says:

    Thank you very very much! Exactly what I wanted!

    [Reply]

  13. zweifel says:

    You can also use the following options for better adjustment:

    -dPDFSETTINGS=/screen (screen-view-only quality, 72 dpi images)
    -dPDFSETTINGS=/ebook (low quality, 150 dpi images)
    -dPDFSETTINGS=/printer (high quality, 300 dpi images)
    -dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
    -dPDFSETTINGS=/default (almost identical to /screen)

    More options and fine tunning on the site: http://milan.kupcevic.net/ghostscript-ps-pdf/

    [Reply]

  14. ShellyCat says:

    Awesome tip! A lifesaver!

    For me, using -dPDFSETTINGS=/screen worked great! I tried scanning my *10-page* document at several resolutions, and the smaller ones printed blurry documents, while the next available DPI made the final (combined) PDF too huge to post to a group service. I’ve spent hours trying to shrink this in Xpdf and other programs (even with the CLI-program “convert”), with no success getting it small enough.

    However, this GhostScript trick made the document *much* smaller without loss of quality, and it was the *simplest* thing I’ve done all night! (Bear in mind, there are no photos in this document. Still, it brought the file from over 5 MB to only 659 kb!

    Thanks tons and tons!

    [Reply]

  15. Nico says:

    It gives me this error:

    Error: /rangecheck in –run–
    Operand stack:
    –dict:12/21(L)– –dict:0/0(L)– 686.787 1.9519 1310.25 789.078
    Execution stack:
    %interp_exit .runexec2 –nostringval– –nostringval– –nostringval– 2 %stopped_push –nostringval– –nostringval– –nostringval– false 1 %stopped_push 1878 1 3 %oparray_pop 1877 1 3 %oparray_pop 1861 1 3 %oparray_pop –nostringval– –nostringval– 2 1 330 –nostringval– %for_pos_int_continue –nostringval– –nostringval– false 1 %stopped_push –nostringval–
    Dictionary stack:
    –dict:1157/1684(ro)(G)– –dict:1/20(G)– –dict:75/200(L)– –dict:75/200(L)– –dict:108/127(ro)(G)– –dict:288/300(ro)(G)– –dict:23/25(L)– –dict:6/8(L)– –dict:22/40(L)–
    Current allocation mode is local
    Last OS error: 2
    GPL Ghostscript 8.71: Unrecoverable error, exit code 1
    GPL Ghostscript 8.71: ERROR: A pdfmark destination page 330 points beyond the last page 1.

    [Reply]

  16. Tony says:

    In case anyone finds it useful; here is a very simple bash script that uses the above command to reduce the PDF size, while making a backup of the original, and printing the filesizes (old and new)
    ———————————————
    #!/bin/bash
    #set -x
    File=””
    File=”$1″
    OriginalFile=${File/%\.pdf/-lrg.pdf}
    mv $File $OriginalFile
    gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$File $OriginalFile
    du -sh $OriginalFile
    du -sh $File
    ———————————————

    You might call it smallpdf, place it in /home/user/bin, make executable, and add the following line to your ~/.bashrc
    alias smallpdf=’~/bin/smallpdf’;

    then in any folder do
    ~$ smallpdf input.pdf

    I’m sure the script could be improved/enhanced (e.g. optionally specify ebook/printer/press)

    [Reply]

  17. yifangt says:

    Thanks Tony!

    This is what I was looking for and/as I am learning BASH!

    I did as your post, but when I issue:

    $ smallpdf my_pdf_file.pdf

    There was an error message:

    “smallpdf: command not found!

    The “smallpdf.bash” was made executable and put in my /usr/bin as I do not have a /home/user/bin/ folder in my Ubuntu 11.04.
    Also I put the alias line in my ~/.bashrc.

    Each time I have to type:

    $ smallpdf.bash my_input_file.pdf

    It seems I am getting close, but not sure the place I missed. Thank you!

    [Reply]

  18. Tony says:

    Hi yifangt,

    I’m also a bit of a learner with bash scripts.

    Maybe you need to do have something like
    alias smallpdf=’/usr/bin/smallpdf.bash’;
    or similar.

    I just created a folder called ‘Programs’ in my home folder (rather than calling it ‘bin’), and stored the script there, then my bashrc line is:
    alias smallpdf=’~/Programs/smallpdf’;

    I’m sure you’ll get it right if you play around a bit with the locations/filenames, and permissions. Good luck!

    [Reply]

  19. Pritesh says:

    Thanks guys the command is awesome. Thanks a lot for you help.

    [Reply]

  20. ThorX89 says:

    I don’t feel like resizing images inside my pdf files, however, I have just found another interesting way to significantly reduce the size of my pdf files.
    Simply by compressing them.
    *.pdf.gz files need not be decompressed if you want to open them in evince or okular.
    If, like me, you’ve got quite a lot of pdf files, this can save you a few GIGs of space (! you won’t be able to open the *.pdf.gz files in acroread and probably not on Windows either !)
    USE ON YOUR OWN RESPONSIBILITY:
    1)INSTALL pigz
    $ sudo apt-get install pigz
    (pigz is exactly like gzip, but it can work in multiple threads and thus utilize your multicore processor)
    2)COMPRESS ALL PDFs IN YOUR $HOME
    $ find “$HOME” -name ‘*.pdf’ -print0 | xargs -0 -n32 pigz -v

    [Reply]

  21. Kali says:

    Thx a lot, it’s a very useful tool.

    [Reply]

  22. Stitch says:

    Thanks for this useful suggestion :)

    [Reply]

  23. AndyC says:

    Fantastic tip, thanks! This reduced an unmanageable 30Mb scan of a 6 page doc to a very readable, and very email-able 541k!

    THANK YOU

    [Reply]

  24. Hamed says:

    Thank you for this post. Worked out great for me.

    [Reply]

  25. Fairread says:

    Thanks! My PDF file goes from 7MB to only 0.2MB!

    [Reply]

  26. Sara says:

    Perfect!!excellent post!! thank you!

    [Reply]

  27. El Samuels says:

    Fantastic! Worked like a chamr even in these noob’s hands.

    Reduced file size from 7 MB to 0.54 MB without any noticable reduction in quality. This may be an extreme case given I had scanned .jpg files interleaved with OOo-printed files with enhanced fonts, but no less amazing and greatly appreciated.

    Cheers!

    [Reply]

  28. inem says:

    U’re Genius ^_^

    [Reply]

  29. Dan says:

    Thanks

    [Reply]

  30. BH says:

    Thank you very much!!!!! Another reason why I love Linux so much – you can do anything! :)

    [Reply]

Leave a Reply