Reduce the size of Scribus generated PDFs

From Scribus Wiki
Revision as of 07:46, 3 May 2009 by Owencook (talk | contribs)
Jump to: navigation, search
This article is part of the Scripts series.

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


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

import sys
import os
import shutil		#

'''
This program is Linux only as I don't have Windows to develop it there.
Doesn't seem to work in 1.3.5, don't know why yet
Requires pdftops and ps2pdf14
'''
fd = open('/tmp/testing123', 'a')
#      fd.write('len(filename) = ' + str(len(filename)) + '\n')
#      fd.write('filename = ' + filename + '\n')
#      fd.close()
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'  )