From e1624601895f12e4596476304f3616d74b9574e8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 8 Jan 2009 06:57:41 -0800 Subject: [PATCH] First attempt at pdfcropping tool --- src/calibre/ebooks/pdf/pdftrim.py | 85 +++++++++++++++++++++++++++++++ src/calibre/linux.py | 1 + 2 files changed, 86 insertions(+) create mode 100644 src/calibre/ebooks/pdf/pdftrim.py diff --git a/src/calibre/ebooks/pdf/pdftrim.py b/src/calibre/ebooks/pdf/pdftrim.py new file mode 100644 index 0000000000..fada1717ed --- /dev/null +++ b/src/calibre/ebooks/pdf/pdftrim.py @@ -0,0 +1,85 @@ +from __future__ import with_statement +__license__ = 'GPL v3' +__copyright__ = '2009, James Beal, james_@catbus.co.uk' +__docformat__ = 'restructuredtext en' + +'crop a pdf file' + +import os, sys, shutil, re +from calibre.utils.config import Config, StringConfig +from pyPdf import PdfFileWriter, PdfFileReader + +def config(defaults=None): + desc = _('Options to control the transformation of pdf') + default_crop=10 + if defaults is None: + c = Config('trimpdf', desc) + else: + c = StringConfig(defaults, desc) + c.add_opt('verbose', ['-v', '--verbose'], default=0, action='count', + help=_('Be verbose, useful for debugging. Can be specified multiple times for greater verbosity.')) + c.add_opt('title', ['-t', '--title'], + help=_('Title for generated ebook. Default is to not change.')) + c.add_opt('author', ['-a', '--author'], + help=_('Set the author in the metadata of the generated ebook. Default is not to change')) + c.add_opt('output', ['-o', '--output'],default='cropped.pdf', + help=_('Path to output file. By default a file is created in the current directory.')) + c.add_opt('bottom_left_x', [ '-x', '--leftx'], default=default_crop, + help=_('Number of pixels to crop from the left most x (default is %d) ')%default_crop ) + c.add_opt('bottom_left_y', [ '-y', '--lefty'], default=default_crop, + help=_('Number of pixels to crop from the left most y (default is %d) ')%default_crop ) + c.add_opt('top_right_x', [ '-v', '--rightx'], default=default_crop, + help=_('Number of pixels to crop from the right most x (default is %d) ')%default_crop ) + c.add_opt('top_right_y', [ '-w', '--righty'], default=default_crop, + help=_('Number of pixels to crop from the right most y (default is %d)')%default_crop ) + c.add_opt('bounding', ['-b', '--bounding'], + help=_('A file generated by ghostscript which allows each page to be individuall cropped')) + return c + + +def option_parser(): + c = config() + return c.option_parser(usage=_('''\ + %prog [options] file.pdf + + Crop a pdf. + ''')) + +def main(args=sys.argv): + parser = option_parser() + opts, args = parser.parse_args(args) + source = os.path.abspath(args[1]) + output_pdf = PdfFileWriter() + input_pdf = PdfFileReader(file(source, "rb")) + if opts.bounding != None: + try: + bounding = open( opts.bounding , 'r' ) + bounding_regex= re.compile('%%BoundingBox: (?P[0-9]+) (?P[0-9]+) (?P[0-9]+) (?P[0-9]+)') + except: + print 'Error opening %s' % opts.bounding + return 1 + print opts.bounding + for page_number in range (0, input_pdf.getNumPages() ): + page = input_pdf.getPage(page_number) + if opts.bounding != None: + while True: + line=bounding.readline() + match=bounding_regex.search(line) + if match !=None: + break + page.mediaBox.upperRight = (match.group('top_x'),match.group('top_y')) + page.mediaBox.lowerLeft = (match.group('bottom_x'),match.group('bottom_y')) + else: + page.mediaBox.upperRight = (page.bleedBox.getUpperRight_x()-opts.top_right_x,page.bleedBox.getUpperRight_y()-opts.top_right_y) + page.mediaBox.lowerLeft = (page.bleedBox.getLowerLeft_x()+opts.bottom_left_x,page.bleedBox.getLowerLeft_y()+opts.bottom_left_y) + output_pdf.addPage(page) + if opts.bounding != None: + bounding.close() + output_file = file(opts.output, "wb") + output_pdf.write(output_file) + output_file.close() + + return 0 + +if __name__ == '__main__': + sys.exit(main()) diff --git a/src/calibre/linux.py b/src/calibre/linux.py index a9d91282e5..cef2e5ddb7 100644 --- a/src/calibre/linux.py +++ b/src/calibre/linux.py @@ -65,6 +65,7 @@ entry_points = { 'calibre-fontconfig = calibre.utils.fontconfig:main', 'calibre-parallel = calibre.parallel:main', 'calibre-customize = calibre.customize.ui:main', + 'pdftrim = calibre.ebooks.pdf.pdftrim:main' , ], 'gui_scripts' : [ __appname__+' = calibre.gui2.main:main',