-
Notifications
You must be signed in to change notification settings - Fork 2
/
abbyy2pdf.py
executable file
·244 lines (190 loc) · 7.66 KB
/
abbyy2pdf.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
#
# Copyright (C) 2014 Elliott Sales de Andrade
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of version 3 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import (division, print_function)
import argparse
import warnings
from lxml import etree
from reportlab.pdfgen import canvas
from reportlab.rl_config import defaultPageSize
from reportlab.lib.colors import black, white, red, green, blue, yellow
class Processor:
def __init__(self):
parser = argparse.ArgumentParser(
description='Convert ABBYY XML files to PDF.')
parser.add_argument('input', type=argparse.FileType('rb'),
help='Input XML file')
parser.add_argument('output', type=argparse.FileType('wb'),
help='Output PDF file')
parser.add_argument('-v', '--verbose', action='store_true',
help='Be verbose.')
parser.add_argument('-a', '--annotate', action='append', default=[],
choices=['all', 'rect', 'line',
'Text', 'Table', 'Picture', 'Barcode',
'Separator', 'SeparatorsBox', 'Checkmark',
'GroupCheckmark'],
help='Annotate PDFs by region.')
args = parser.parse_args()
self.input = args.input
self.output = args.output
self.verbose = args.verbose
try:
args.annotate.remove('all')
except ValueError:
self.annotate = args.annotate
else:
self.annotate = ['rect', 'line',
'Text', 'Table', 'Picture', 'Barcode',
'Separator', 'SeparatorsBox', 'Checkmark',
'GroupCheckmark']
for x in args.annotate:
self.annotate.remove(x)
def run(self):
self.pdf = canvas.Canvas(self.output, bottomup=0, verbosity=self.verbose)
self.width, self.height = defaultPageSize
self.resolution = 72
content = etree.iterparse(self.input, ('start', 'end'))
for action, element in content:
localname = element.tag
if '}' in localname:
localname = localname.split('}')[1]
localname = localname[0].upper() + localname[1:]
name = action + localname
if hasattr(self, name):
func = getattr(self, name)
func(element)
else:
warnings.warn('Unimplemented %s tag: %s' % (action, localname))
self.pdf.save()
def startDocument(self, doc):
version = doc.get('version')
producer = doc.get('producer')
pagesCount = int(doc.get('pagesCount', '1'))
mainLanguage = doc.get('mainLanguage')
languages = doc.get('languages')
print(version, producer, pagesCount, mainLanguage, languages)
def endDocument(self, doc):
pass
def startPage(self, page):
self.width = int(page.get('width'))
self.height = int(page.get('height'))
self.resolution = int(page.get('resolution'))
originCoords = page.get('originalCoords') == 'true'
width = self.width / self.resolution * 72
height = self.height / self.resolution * 72
self.pdf.setPageSize((width, height))
def endPage(self, page):
self.pdf.showPage()
def startBlock(self, block):
self.blockType = block.get('blockType')
pageElemId = block.get('pageElemId')
blockName = block.get('blockName')
isHidden = block.get('isHidden') == 'true'
if isHidden:
return
self.drawRect(self.blockType, block, blue, optional=True)
def endBlock(self, block):
self.blockType = None
def startRegion(self, region):
pass
def endRegion(self, region):
pass
def startRect(self, region):
self.drawRect('rect', region, red)
def endRect(self, region):
pass
def startText(self, text):
text_id = text.get('id')
self.orientation = text.get('orientation')
self.bgColour = int(text.get('backgroundColor', '16777215'))
self.mirrored = text.get('mirrored') == 'true'
self.inverted = text.get('inverted') == 'true'
self.text_obj = self.pdf.beginText()
def endText(self, text):
self.pdf.drawText(self.text_obj)
self.text_obj = None
def startLine(self, line):
self.drawRect('line', line, yellow)
def endLine(self, line):
pass
def startFormatting(self, form):
self.fs = form.get('fs')
bold = form.get('bold') == 'true'
italic = form.get('italic') == 'true'
if bold and italic:
self.fontname = 'Helvetica-BoldOblique'
elif bold:
self.fontname = 'Helvetica-Bold'
elif italic:
self.fontname = 'Helvetica-Oblique'
else:
self.fontname = 'Helvetica'
subscript = form.get('subscript') == 'true'
superscript = form.get('superscript') == 'true'
smallcaps = form.get('smallcaps') == 'true'
underline = form.get('underline') == 'true'
strikeout = form.get('strikeout') == 'true'
scaling = int(form.get('scaling') or 1000)
spacing = int(form.get('spacing') or 0)
def endFormatting(self, form):
pass
def startCharParams(self, cp):
left = int(cp.get('l')) / self.resolution * 72
top = int(cp.get('t')) / self.resolution * 72
right = int(cp.get('r')) / self.resolution * 72
bottom = int(cp.get('b')) / self.resolution * 72
suspicious = cp.get('suspicious') == 'true'
self.text_obj.setTextOrigin(left, bottom)
if self.annotate and suspicious:
self.text_obj.setFillColor(red)
else:
self.text_obj.setFillColor(black)
def endCharParams(self, cp):
left = int(cp.get('l')) / self.resolution * 72
right = int(cp.get('r')) / self.resolution * 72
text_width = right - left
if self.fs:
self.text_obj.setFont(self.fontname, self.fs)
else:
fs = 12
for _i in range(1000):
width = self.pdf.stringWidth(cp.text, self.fontname, fs)
if cp.text in 'itr':
width *= 1.5
elif cp.text in 'IT':
width *= 1.2
dw = text_width - width
if abs(dw) < 1e-6:
break
fs = fs * (1 + dw / width)
self.text_obj.setFont(self.fontname, fs)
self.text_obj.textOut(cp.text)
def drawRect(self, type, elem, colour, optional=False):
left = elem.get('l')
top = elem.get('t')
right = elem.get('r')
bottom = elem.get('b')
box = [left, bottom, right, top]
if type not in self.annotate or (optional and None in box):
return
# rect uses (left, bottom, width, height) instead
box = [int(x) / self.resolution * 72 for x in box]
box[2] = box[2] - box[0]
box[3] = box[3] - box[1]
self.pdf.setStrokeColor(colour)
self.pdf.rect(*box)
p = Processor()
p.run()