~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/pdf/pdftrim.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from __future__ import with_statement
2
 
__license__   = 'GPL v3'
3
 
__copyright__ = '2009, James Beal, james_@catbus.co.uk'
4
 
__docformat__ = 'restructuredtext en'
5
 
 
6
 
'crop a pdf file'
7
 
 
8
 
import os, sys, re
9
 
from calibre.utils.config import Config, StringConfig
10
 
from pyPdf import PdfFileWriter, PdfFileReader
11
 
 
12
 
def config(defaults=None):
13
 
    desc = _('Options to control the transformation of pdf')
14
 
    default_crop=10
15
 
    if defaults is None:
16
 
        c = Config('trimpdf', desc)
17
 
    else:
18
 
        c = StringConfig(defaults, desc)
19
 
    c.add_opt('verbose', ['-v', '--verbose'], default=0, action='count',
20
 
          help=_('Be verbose, useful for debugging. Can be specified multiple times for greater verbosity.'))
21
 
    c.add_opt('output', ['-o', '--output'],default='cropped.pdf',
22
 
          help=_('Path to output file. By default a file is created in the current directory.'))
23
 
    c.add_opt('bottom_left_x', [ '-x', '--leftx'], default=default_crop,
24
 
          help=_('Number of pixels to crop from the left most x (default is %d) ')%default_crop )
25
 
    c.add_opt('bottom_left_y', [ '-y', '--lefty'], default=default_crop,
26
 
          help=_('Number of pixels to crop from the left most y (default is %d) ')%default_crop )
27
 
    c.add_opt('top_right_x', [ '-v', '--rightx'], default=default_crop,
28
 
          help=_('Number of pixels to crop from the right most x (default is %d) ')%default_crop )
29
 
    c.add_opt('top_right_y', [ '-w', '--righty'], default=default_crop,
30
 
          help=_('Number of pixels to crop from the right most y (default is %d)')%default_crop )
31
 
    c.add_opt('bounding', ['-b', '--bounding'],
32
 
          help=_('A file generated by ghostscript which allows each page to be individually cropped [gs -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox > bounding] '))
33
 
    return c
34
 
 
35
 
 
36
 
def option_parser():
37
 
    c = config()
38
 
    return c.option_parser(usage=_('''\
39
 
        %prog [options] file.pdf
40
 
 
41
 
        Crops a pdf. 
42
 
        '''))
43
 
 
44
 
def main(args=sys.argv):
45
 
    parser = option_parser()
46
 
    opts, args = parser.parse_args(args)
47
 
    try:
48
 
        source = os.path.abspath(args[1])
49
 
        input_pdf = PdfFileReader(file(source, "rb"))
50
 
    except:
51
 
        print "Unable to read input"
52
 
        return 2
53
 
    title   = _('Unknown')
54
 
    author  = _('Unknown')
55
 
    try:
56
 
        info = input_pdf.getDocumentInfo()
57
 
        if info.title:
58
 
            title   = info.title
59
 
        if info.author:
60
 
            author  = info.author
61
 
    except:
62
 
        pass
63
 
    if opts.bounding != None:
64
 
        try:
65
 
            bounding = open( opts.bounding , 'r' )
66
 
            bounding_regex= re.compile('%%BoundingBox: (?P<bottom_x>[0-9]+) (?P<bottom_y>[0-9]+) (?P<top_x>[0-9]+) (?P<top_y>[0-9]+)')
67
 
        except:
68
 
            print 'Error opening %s' % opts.bounding 
69
 
            return 1
70
 
    output_pdf = PdfFileWriter(title=title,author=author)
71
 
    for page_number in range (0, input_pdf.getNumPages() ):
72
 
        page = input_pdf.getPage(page_number)
73
 
        if opts.bounding != None:
74
 
            while True:
75
 
                line=bounding.readline()
76
 
                match=bounding_regex.search(line)
77
 
                if match !=None:
78
 
                    break
79
 
            page.mediaBox.upperRight = (match.group('top_x'),match.group('top_y'))
80
 
            page.mediaBox.lowerLeft  = (match.group('bottom_x'),match.group('bottom_y'))
81
 
        else:
82
 
            page.mediaBox.upperRight = (page.bleedBox.getUpperRight_x()-opts.top_right_x,page.bleedBox.getUpperRight_y()-opts.top_right_y)
83
 
            page.mediaBox.lowerLeft  = (page.bleedBox.getLowerLeft_x()+opts.bottom_left_x,page.bleedBox.getLowerLeft_y()+opts.bottom_left_y)
84
 
        output_pdf.addPage(page)
85
 
    if opts.bounding != None:
86
 
        bounding.close()
87
 
    output_file = file(opts.output, "wb")
88
 
    output_pdf.write(output_file)
89
 
    output_file.close()
90
 
 
91
 
 
92
 
    return 0
93
 
 
94
 
if __name__ == '__main__':
95
 
    sys.exit(main())