Logo Search packages:      
Sourcecode: calibre version File versions  Download package


# -*- coding: utf-8 -*-
from __future__ import with_statement

__license__   = 'GPL v3'
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
__docformat__ = 'restructuredtext en'

Reverse content of PDF.

import os, sys
from optparse import OptionGroup, Option

from calibre.ebooks.metadata.meta import metadata_from_formats
from calibre.ebooks.metadata import authors_to_string
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, is_encrypted

from pyPdf import PdfFileWriter, PdfFileReader

USAGE = '\n%prog %%name ' + _('''\
[options] file.pdf

Reverse a PDF.

OPTIONS = set([
    OptionRecommendation(name='output', recommended_value='reversed.pdf',
        level=OptionRecommendation.HIGH, long_switch='output', short_switch='o',
        help=_('Path to output file. By default a file is created in the current directory.')),

def print_help(parser, log):
    help = parser.format_help().encode(preferred_encoding, 'replace')

def option_parser(name):
    usage = USAGE.replace('%%name', name)
    return OptionParser(usage=usage)

def option_recommendation_to_cli_option(add_option, rec):
    opt = rec.option
    switches = ['-'+opt.short_switch] if opt.short_switch else []
    attrs = dict(dest=opt.name, help=opt.help,
                     choices=opt.choices, default=rec.recommended_value)
    add_option(Option(*switches, **attrs))

def add_options(parser):
    group = OptionGroup(parser, _('Reverse Options:'), _('Options to control the transformation of pdf'))
    add_option = group.add_option
    for rec in OPTIONS:
        option_recommendation_to_cli_option(add_option, rec)

def reverse(pdf_path, out_path, metadata=None):
    if metadata == None:
        title = _('Unknown')
        author = _('Unknown')
        title = metadata.title
        author = authors_to_string(metadata.authors)

    out_pdf = PdfFileWriter(title=title, author=author)

    pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
    for page in reversed(pdf.pages):

    with open(out_path, 'wb') as out_file:

def main(args=sys.argv, name=''):
    log = Log()
    parser = option_parser(name)
    opts, args = parser.parse_args(args)
    args = args[1:]
    if len(args) < 1:
        print 'Error: A PDF file is required.\n'
        print_help(parser, log)
        return 1
    if not is_valid_pdf(args[0]):
        print 'Error: Could not read file `%s`.' % args[0]
        return 1

    if is_encrypted(args[0]):
        print 'Error: file `%s` is encrypted.' % args[0]
        return 1
    mi = metadata_from_formats([args[0]])

    reverse(args[0], opts.output, mi)

    return 0

if __name__ == '__main__':

Generated by  Doxygen 1.6.0   Back to index