mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Error checking and getting meta-data right
This commit is contained in:
parent
a5e4ed2779
commit
db32290326
@ -29,7 +29,7 @@ def config(defaults=None):
|
|||||||
c.add_opt('top_right_y', [ '-w', '--righty'], default=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 )
|
help=_('Number of pixels to crop from the right most y (default is %d)')%default_crop )
|
||||||
c.add_opt('bounding', ['-b', '--bounding'],
|
c.add_opt('bounding', ['-b', '--bounding'],
|
||||||
help=_('A file generated by ghostscript which allows each page to be individually cropped'))
|
help=_('A file generated by ghostscript which allows each page to be individually cropped [gs -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox > bounding] '))
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
@ -38,14 +38,24 @@ def option_parser():
|
|||||||
return c.option_parser(usage=_('''\
|
return c.option_parser(usage=_('''\
|
||||||
%prog [options] file.pdf
|
%prog [options] file.pdf
|
||||||
|
|
||||||
Crop a pdf.
|
Crops a pdf.
|
||||||
'''))
|
'''))
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
parser = option_parser()
|
parser = option_parser()
|
||||||
opts, args = parser.parse_args(args)
|
opts, args = parser.parse_args(args)
|
||||||
source = os.path.abspath(args[1])
|
try:
|
||||||
input_pdf = PdfFileReader(file(source, "rb"))
|
source = os.path.abspath(args[1])
|
||||||
|
input_pdf = PdfFileReader(file(source, "rb"))
|
||||||
|
except:
|
||||||
|
print "Unable to read input"
|
||||||
|
return 2
|
||||||
|
info = input_pdf.getDocumentInfo()
|
||||||
|
title = 'Unknown'
|
||||||
|
author = 'Unknown'
|
||||||
|
if info.title:
|
||||||
|
title = info.title
|
||||||
|
author = info.author
|
||||||
if opts.bounding != None:
|
if opts.bounding != None:
|
||||||
try:
|
try:
|
||||||
bounding = open( opts.bounding , 'r' )
|
bounding = open( opts.bounding , 'r' )
|
||||||
@ -53,7 +63,7 @@ def main(args=sys.argv):
|
|||||||
except:
|
except:
|
||||||
print 'Error opening %s' % opts.bounding
|
print 'Error opening %s' % opts.bounding
|
||||||
return 1
|
return 1
|
||||||
output_pdf = PdfFileWriter()
|
output_pdf = PdfFileWriter(title=title,author=author)
|
||||||
for page_number in range (0, input_pdf.getNumPages() ):
|
for page_number in range (0, input_pdf.getNumPages() ):
|
||||||
page = input_pdf.getPage(page_number)
|
page = input_pdf.getPage(page_number)
|
||||||
if opts.bounding != None:
|
if opts.bounding != None:
|
||||||
|
@ -55,7 +55,7 @@ from utils import readNonWhitespace, readUntilWhitespace, ConvertFunctionsToVirt
|
|||||||
# This class supports writing PDF files out, given pages produced by another
|
# This class supports writing PDF files out, given pages produced by another
|
||||||
# class (typically {@link #PdfFileReader PdfFileReader}).
|
# class (typically {@link #PdfFileReader PdfFileReader}).
|
||||||
class PdfFileWriter(object):
|
class PdfFileWriter(object):
|
||||||
def __init__(self):
|
def __init__(self,title=u"Unknown",author=u"Unknown"):
|
||||||
self._header = "%PDF-1.3"
|
self._header = "%PDF-1.3"
|
||||||
self._objects = [] # array of indirect objects
|
self._objects = [] # array of indirect objects
|
||||||
|
|
||||||
@ -71,7 +71,9 @@ class PdfFileWriter(object):
|
|||||||
# info object
|
# info object
|
||||||
info = DictionaryObject()
|
info = DictionaryObject()
|
||||||
info.update({
|
info.update({
|
||||||
NameObject("/Producer"): createStringObject(u"Python PDF Library - http://pybrary.net/pyPdf/")
|
NameObject("/Producer"): createStringObject(u"Python PDF Library - http://pybrary.net/pyPdf/"),
|
||||||
|
NameObject("/Author"): createStringObject(author),
|
||||||
|
NameObject("/Title"): createStringObject(title),
|
||||||
})
|
})
|
||||||
self._info = self._addObject(info)
|
self._info = self._addObject(info)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user