diff --git a/src/calibre/ebooks/pdf/manipulate/crop.py b/src/calibre/ebooks/pdf/manipulate/crop.py index c3eb70c56d..fa996b754f 100644 --- a/src/calibre/ebooks/pdf/manipulate/crop.py +++ b/src/calibre/ebooks/pdf/manipulate/crop.py @@ -19,6 +19,7 @@ from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation +from calibre.ebooks.pdf.verify import is_valid_pdf from pyPdf import PdfFileWriter, PdfFileReader @@ -116,17 +117,6 @@ def crop_pdf(pdf_path, opts, metadata=None): with open(opts.output, 'wb') as output_file: output_pdf.write(output_file) - -# Return True if the pdf is valid. -def valid_pdf(pdf_path): - try: - with open(os.path.abspath(pdf_path), 'rb') as pdf_file: - pdf = PdfFileReader(pdf_file) - if pdf.isEncrypted or pdf.numPages <= 0: - raise Exception - except: - return False - return True def main(args=sys.argv, name=''): log = Log() @@ -141,7 +131,7 @@ def main(args=sys.argv, name=''): print_help(parser, log) return 1 - if not valid_pdf(args[0]): + if not is_valid_pdf(args[0]): print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0] return 1 diff --git a/src/calibre/ebooks/pdf/manipulate/info.py b/src/calibre/ebooks/pdf/manipulate/info.py index 4aff524330..21a07fdeff 100644 --- a/src/calibre/ebooks/pdf/manipulate/info.py +++ b/src/calibre/ebooks/pdf/manipulate/info.py @@ -16,6 +16,7 @@ from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation +from calibre.ebooks.pdf.verify import is_valid_pdfs from pyPdf import PdfFileWriter, PdfFileReader @@ -56,17 +57,6 @@ def print_info(pdf_path): print _('PDF Version: %s' % mo.group('version')) except: pass -def verify_files(files): - invalid = [] - - for pdf_path in files: - try: - with open(os.path.abspath(pdf_path), 'rb') as pdf_file: - pdf = PdfFileReader(pdf_file) - except: - invalid.append(pdf_path) - return invalid - def main(args=sys.argv, name=''): log = Log() parser = option_parser(name) @@ -79,7 +69,7 @@ def main(args=sys.argv, name=''): print_help(parser, log) return 1 - bad_pdfs = verify_files(args) + bad_pdfs = is_valid_pdfs(args) if bad_pdfs != []: for pdf in bad_pdfs: print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf diff --git a/src/calibre/ebooks/pdf/manipulate/merge.py b/src/calibre/ebooks/pdf/manipulate/merge.py index f0ecb9bd7a..1e285e3bdf 100644 --- a/src/calibre/ebooks/pdf/manipulate/merge.py +++ b/src/calibre/ebooks/pdf/manipulate/merge.py @@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation +from calibre.ebooks.pdf.verify import is_valid_pdfs from pyPdf import PdfFileWriter, PdfFileReader @@ -76,19 +77,6 @@ def merge_files(in_paths, out_path, metadata=None): with open(out_path, 'wb') as out_file: out_pdf.write(out_file) - -def verify_files(files): - invalid = [] - - for pdf_path in files: - try: - with open(os.path.abspath(pdf_path), 'rb') as pdf_file: - pdf = PdfFileReader(pdf_file) - if pdf.isEncrypted or pdf.numPages <= 0: - raise Exception - except: - invalid.append(pdf_path) - return invalid def main(args=sys.argv, name=''): log = Log() @@ -103,7 +91,7 @@ def main(args=sys.argv, name=''): print_help(parser, log) return 1 - bad_pdfs = verify_files(args) + bad_pdfs = is_valid_pdfs(args) if bad_pdfs != []: for pdf in bad_pdfs: print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf diff --git a/src/calibre/ebooks/pdf/manipulate/reverse.py b/src/calibre/ebooks/pdf/manipulate/reverse.py index 189cbf009b..564e523ae3 100644 --- a/src/calibre/ebooks/pdf/manipulate/reverse.py +++ b/src/calibre/ebooks/pdf/manipulate/reverse.py @@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation +from calibre.ebooks.pdf.verify import is_valid_pdf from pyPdf import PdfFileWriter, PdfFileReader @@ -74,17 +75,6 @@ def reverse(pdf_path, out_path, metadata=None): with open(out_path, 'wb') as out_file: out_pdf.write(out_file) -# Return True if the pdf is valid. -def valid_pdf(pdf_path): - try: - with open(os.path.abspath(pdf_path), 'rb') as pdf_file: - pdf = PdfFileReader(pdf_file) - if pdf.isEncrypted or pdf.numPages <= 0: - raise Exception - except: - return False - return True - def main(args=sys.argv, name=''): log = Log() parser = option_parser(name) @@ -98,7 +88,7 @@ def main(args=sys.argv, name=''): print_help(parser, log) return 1 - if not valid_pdf(args[0]): + if not is_valid_pdf(args[0]): print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % args[0] return 1 diff --git a/src/calibre/ebooks/pdf/manipulate/split.py b/src/calibre/ebooks/pdf/manipulate/split.py index 8996a4cb6b..fb7e4d06d7 100644 --- a/src/calibre/ebooks/pdf/manipulate/split.py +++ b/src/calibre/ebooks/pdf/manipulate/split.py @@ -18,6 +18,7 @@ from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation +from calibre.ebooks.pdf.verify import is_valid_pdf from pyPdf import PdfFileWriter, PdfFileReader @@ -163,17 +164,6 @@ def clean_page_list(pdf_path, pages, page_ranges): return pages, page_ranges -# Return True if the pdf is valid. -def valid_pdf(pdf_path): - try: - with open(os.path.abspath(pdf_path), 'rb') as pdf_file: - pdf = PdfFileReader(pdf_file) - if pdf.isEncrypted or pdf.numPages <= 0: - raise Exception - except: - return False - return True - def main(args=sys.argv, name=''): log = Log() parser = option_parser(name) @@ -194,7 +184,7 @@ def main(args=sys.argv, name=''): print_help(parser, log) return 1 - if not valid_pdf(pdf): + if not is_valid_pdf(pdf): print 'Error: Could not read file `%s`. Is it a vaild PDF file or is it encrypted/DRMed?.' % pdf return 1 diff --git a/src/calibre/ebooks/pdf/verify.py b/src/calibre/ebooks/pdf/verify.py new file mode 100644 index 0000000000..35f7edf0be --- /dev/null +++ b/src/calibre/ebooks/pdf/verify.py @@ -0,0 +1,37 @@ +from __future__ import with_statement +# -*- coding: utf-8 -*- + +__license__ = 'GPL v3' +__copyright__ = '2009, John Schember ' +__docformat__ = 'restructuredtext en' + +''' +Verify PDF files. +''' + +import os + +from pyPdf import PdfFileWriter, PdfFileReader + +def is_valid_pdf(pdf_path): + ''' + Returns True if the pdf file is valid. + ''' + + try: + with open(os.path.abspath(pdf_path), 'rb') as pdf_file: + pdf = PdfFileReader(pdf_file) + except: + return False + return True + +def is_valid_pdfs(pdf_paths): + ''' + Returns a list of invalid pdf files. + ''' + + invalid = [] + for pdf_path in pdf_paths: + if not is_valid_pdf(pdf_path): + invalid.append(pdf_path) + return invalid