Reduce the size of Scribus generated PDFs: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Script to reduce the size of PDFs (1.3.3))
 
mNo edit summary
Line 2: Line 2:


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




<pre><nowiki>
<pre><nowiki>
#! /usr/bin/env python
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
Line 21: Line 19:
Requires pdftops and ps2pdf14
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"
failure = "failure"
filesTo_find = ['pdftops','ps2pdf14']
filesTo_find = ['pdftops','ps2pdf14']
Line 72: Line 73:
if os.path.isfile(filename):
if os.path.isfile(filename):
   orig_size = os.path.getsize(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
   cmd = 'pdftops' + ' ' + filename
Line 83: Line 78:


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


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


pdf_file = os.path.basename(filename)
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'  )
result = scribus.messageBox(pdf_file,"Original size was" + ' ' + str(orig_size) + ' ' + "bytes" + '\n' + "New size is" + ' ' + str(new_size) + ' ' + "bytes"'\n'  )


</nowiki></pre>
</nowiki></pre>

Revision as of 07:46, 3 May 2009

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