/
page.py
107 lines (88 loc) · 3.74 KB
/
page.py
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
##############################################################################
#
# Copyright (c) 2007 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.
#
##############################################################################
"""Page Drawing Related Element Processing
"""
import cStringIO
from z3c.rml import attr, directive, interfaces
try:
import PyPDF2
from PyPDF2.generic import NameObject
except ImportError:
# We don't want to require pyPdf, if you do not want to use the features
# in this module.
PyPDF2 = None
class MergePostProcessor(object):
def __init__(self):
self.operations = {}
def process(self, inputFile1):
input1 = PyPDF2.PdfFileReader(inputFile1)
output = PyPDF2.PdfFileWriter()
output._info.getObject().update(input1.documentInfo)
output._root.getObject()[NameObject("/Outlines")] = (
output._addObject(input1.trailer["/Root"]["/Outlines"]))
for (num, page) in enumerate(input1.pages):
if num in self.operations:
for mergeFile, mergeNumber in self.operations[num]:
merger = PyPDF2.PdfFileReader(mergeFile)
mergerPage = merger.getPage(mergeNumber)
mergerPage.mergePage(page)
page = mergerPage
output.addPage(page)
outputFile = cStringIO.StringIO()
output.write(outputFile)
return outputFile
class IMergePage(interfaces.IRMLDirectiveSignature):
"""Merges an existing PDF Page into the one to be generated."""
filename = attr.File(
title=u'File',
description=(u'Reference to the PDF file to extract the page from.'),
required=True)
page = attr.Integer(
title=u'Page Number',
description=u'The page number of the PDF file that is used to merge..',
required=True)
class MergePage(directive.RMLDirective):
signature = IMergePage
def getProcessor(self):
manager = attr.getManager(self, interfaces.IPostProcessorManager)
procs = dict(manager.postProcessors)
if 'MERGE' not in procs:
proc = MergePostProcessor()
manager.postProcessors.append(('MERGE', proc))
return proc
return procs['MERGE']
def process(self):
if PyPDF2 is None:
raise Exception(
'pyPdf is not installed, so this feature is not available.')
inputFile, inPage = self.getAttributeValues(valuesOnly=True)
manager = attr.getManager(self, interfaces.ICanvasManager)
outPage = manager.canvas.getPageNumber()-1
proc = self.getProcessor()
pageOperations = proc.operations.setdefault(outPage, [])
pageOperations.append((inputFile, inPage))
class MergePageInPageTemplate(MergePage):
def process(self):
if PyPDF2 is None:
raise Exception(
'pyPdf is not installed, so this feature is not available.')
inputFile, inPage = self.getAttributeValues(valuesOnly=True)
onPage = self.parent.pt.onPage
def drawOnCanvas(canvas, doc):
onPage(canvas, doc)
outPage = canvas.getPageNumber()-1
proc = self.getProcessor()
pageOperations = proc.operations.setdefault(outPage, [])
pageOperations.append((inputFile, inPage))
self.parent.pt.onPage = drawOnCanvas