/usr/share/pyshared/z3c/rml/pdfinclude.py is in python-z3c.rml 2.0.0-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | ##############################################################################
#
# Copyright (c) 2012 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""``pdfInclude`` Directive.
"""
__docformat__ = "reStructuredText"
try:
import pyPdf
except ImportError:
pyPdf = None
from reportlab.platypus import flowables
from z3c.rml import attr, flowable, interfaces, occurence, page
class IncludePdfPagesFlowable(flowables.Flowable):
def __init__(self, pdf_file, pages, mergeprocessor):
flowables.Flowable.__init__(self)
self.pdf_file = pdf_file
self.proc = mergeprocessor
pdf = pyPdf.PdfFileReader(pdf_file)
self.num_pages = pdf.getNumPages()
self.pages = pages if pages else range(1, self.num_pages+1)
self.width = 10<<32
self.height = 10<<32
def draw():
return NotImplementedError('PDFPages shall be drawn not me')
def split(self, availWidth, availheight):
result = []
for i in self.pages:
result.append(flowables.PageBreak())
result.append(PDFPageFlowable(self, i-1, availWidth, availheight))
return result
class PDFPageFlowable(flowables.Flowable):
def __init__(self, parent, pagenumber, width, height):
flowables.Flowable.__init__(self)
self.parent = parent
self.pagenumber = pagenumber
self.width = width
self.height = height
def draw(self):
# FIXME : scale and rotate ?
# self.canv.addLiteral(self.page.getContents())
proc = self.parent.proc
outPage = self.canv.getPageNumber()-1
pageOperations = proc.operations.setdefault(outPage, [])
pageOperations.append((self.parent.pdf_file, self.pagenumber))
# flowable.NextPage()
def split(self, availWidth, availheight):
return [self]
class IIncludePdfPages(interfaces.IRMLDirectiveSignature):
"""Inserts a set of pages from a given PDF."""
filename = attr.File(
title=u'Path to file',
description=u'The pdf file to include.',
required=True)
pages = attr.IntegerSequence(
title=u'Pages',
description=u'A list of pages to insert.',
required=False)
class IncludePdfPages(flowable.Flowable):
signature = IIncludePdfPages
def getProcessor(self):
manager = attr.getManager(self, interfaces.IPostProcessorManager)
procs = dict(manager.postProcessors)
if 'MERGE' not in procs:
proc = page.MergePostProcessor()
manager.postProcessors.append(('MERGE', proc))
return proc
return procs['MERGE']
def process(self):
if pyPdf is None:
raise Exception(
'pyPdf is not installed, so this feature is not available.')
args = dict(self.getAttributeValues())
proc = self.getProcessor()
self.parent.flow.append(
IncludePdfPagesFlowable(args['filename'], args.get('pages'), proc))
flowable.Flow.factories['includePdfPages'] = IncludePdfPages
flowable.IFlow.setTaggedValue(
'directives',
flowable.IFlow.getTaggedValue('directives') +
(occurence.ZeroOrMore('includePdfPages', IIncludePdfPages),)
)
|