Reduce the size of Scribus generated PDFs

From Scribus Wiki
Revision as of 00:40, 3 May 2009 by Owencook (talk | contribs) (Script to reduce the size of PDFs (1.3.3))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
This article is part of the Scripts series.

A linux only program.
This works with Scribus 1.3.3, yet to understand why it doesn't work with 1.3.5
Note: Not all PDFs can be reduced, sometimes they are even larger!
Script creates a back up of the original 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
'''

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)

  try:
      shutil.copyfile(filename, filename + '.orig')     #make a backup
  except:
      result = scribus.messageBox('Unwritable!!!',  errorMessage_4,)
      sys.exit(1)

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

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

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'  )