Reduce the size of Scribus generated PDFs

From Scribus Wiki
Jump to navigation Jump to search
This article is part of the Scripts series.

See also

Simply using Ghostscript

For example, on a MacIntosh with Mac OS 10.6.2, Scribus 1.3.6, Ghostscript 8.71, this ghostscript command will knock a 1.9GB PDF file generated by Scribus down to less than 300MB which is very helpful :

gs-8.71-macosx -dPDFSETTINGS=/prepress -dSAFER -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -dGrayImageResolution=600 -dMonoImageResolution=1200 -dColorImageResolution=300 -sOutputFile=vol_iv_gs.pdf -c .setpdfwrite -f vol_iv.pdf

On a linux PC, this works simply well :

gs -dPDFSETTINGS=/prepress -dSAFER -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -dGrayImageResolution=600 -dMonoImageResolution=1200 -dColorImageResolution=300 -sOutputFile=vol_iv_gs.pdf -c .setpdfwrite -f vol_iv.pdf

or more simply, for press :

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=newfile.pdf myfile.pdf

and for screen viewing :

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

Adding the "-c .setpdfwrite" option is probably usefull (see http://ghostscript.com/doc/current/Language.htm#.setpdfwrite and http://ghostscript.com/doc/current/Ps2pdf.htm#Limitations) :

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=newfile.pdf -c .setpdfwrite -f myfile.pdf

Similar on a Windows PC :

gswin32 -dPDFSETTINGS=/prepress -dSAFER -dCompatibilityLevel=1.5 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sstdout=%stderr -dGrayImageResolution=600 -dMonoImageResolution=1200 -dColorImageResolution=300 -sOutputFile=vol_iv_gs.pdf -c .setpdfwrite -f vol_iv.pdf

Put the command line in a batch file in the same folder as the Scribus generated PDF, and the batch will create the new file via double click.

Using a Python Script

A linux only program.
Note: Not all PDFs can be reduced, sometimes they are even larger!
Script creates a back up of the original (filename.pdf.orig) as well as the new file (filename.new.pdf) and you require pdftops and ps2pdf14. A 5MB file may take about 20 seconds, a 100 MB file may take minutes.

ReducePDFsize.png

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import shutil		#

'''
This program is Linux only.
Requires pdftops and ps2pdf14
'''

failure = "failure"
filesTo_find = ['pdftops','ps2pdf14']
errorMessage_1 = 'This program requires both pdftops and ps2pdf14.\nOne or both is missing. Please install to allow this program to function'
errorMessage_2 = 'This program does not yet operate on windows.\nFeel free to modify it run on a win32 system'
errorMessage_3 = 'This program does not yet operate on OS2.\nFeel free to modify it run on an OS2 system'
errorMessage_4 = 'You do not have permission to write in this directory.\nCopy the pdf to a writable directory and try again.'
found = 0

try:
    import scribus
except ImportError,err:
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    sys.exit(1)

if sys.platform != 'win32':
     pass
else:
     result = scribus.messageBox('Sorry - Linux Only program',  errorMessage_2,)
     sys.exit()

if sys.platform != 'os2':
     pass
else:
     result = scribus.messageBox('Sorry - Linux Only program',  errorMessage_3,)
     sys.exit()

path = os.environ['PATH']
paths = path.split(os.pathsep)
for ftf in filesTo_find:
    for p in paths:
        f = p + '/' + ftf
        if os.path.isfile(f):
            found = found + 1    #need to start search for next file when first file found
            break

if found == 2:
    pass

else:
    result = scribus.messageBox('Missing Programs',  errorMessage_1,)
    sys.exit()

homedir = os.path.expanduser("~")
os.chdir(homedir)

filename=scribus.fileDialog("Reduce PDF",  "PDF files(*.PDF *.pdf)")

if os.path.isfile(filename):
  orig_size = os.path.getsize(filename)

  cmd = 'pdftops' + ' ' + filename
  os.system(cmd)

if os.path.exists(filename[:-3] + 'ps'):

  cmd2 = 'ps2pdf14' + ' ' + filename[:-3] + 'ps' + ' ' + filename[:-3] + 'new' + '.pdf'
  os.system(cmd2)
  new_size = os.path.getsize(filename[:-3] + 'new' + '.pdf')

else:
     result = scribus.messageBox('Big Boo Boo',  'Something real bad has gone wrong! Sorry',)
     sys.exit(1)


pdf_file = os.path.basename(filename)
result = scribus.messageBox(pdf_file,"Original size was" + ' ' + str(orig_size) + ' ' + "bytes" + '\n' + "New size is" + ' ' + str(new_size) + ' ' + "bytes"'\n'  )