-
Notifications
You must be signed in to change notification settings - Fork 10
/
testPDFReader.py
280 lines (230 loc) · 11.7 KB
/
testPDFReader.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
# NOTE: This unittest requires that the pdftohtml and related binaries
# are available for calling, making this not a pure unittest. If they
# are not, the tests fall back to using canned result files.
from bz2 import BZ2File
import os
import shutil
import tempfile
from lxml import etree
from ferenda.compat import unittest
from ferenda import errors, util
from ferenda.testutil import FerendaTestCase
# SUT
from ferenda import PDFReader
from ferenda.pdfreader import Textbox, Textelement
class Read(unittest.TestCase):
def setUp(self):
self.maxDiff = None
self.datadir = tempfile.mkdtemp()
def tearDown(self):
shutil.rmtree(self.datadir)
def _copy_sample(self):
for fname in os.listdir("test/files/pdfreader/intermediate"):
shutil.copy("test/files/pdfreader/intermediate/%s" % fname,
self.datadir + os.sep + fname)
def test_basic(self):
try:
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir)
except errors.ExternalCommandError:
self._copy_sample()
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir)
# a temporary copy of the pdf file should not be lying around
# in workdir
# print("Checking if %s has been unlinked" % (self.datadir +
# os.sep + "sample.pdf"))
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.pdf"))
# but the XML file should be stored for subsequent parses
self.assertTrue(os.path.exists(self.datadir + os.sep + "sample.xml"))
# The PDF contained actual textboxes
self.assertFalse(reader.is_empty())
self.assertEqual(len(reader), 1)
# first page, first box
title = str(reader[0][0])
self.assertEqual("Document title ", title)
self.assertEqual(570, reader.median_box_width())
page = reader[0]
self.assertEqual("Page 1 (892 x 1263): 'Document title This is a simple documen...'", str(page))
# an uncropped doc should have nine nonempty textboxes
self.assertEqual(9, len(list(page.boundingbox())))
# a smaller bounding box yields just one
self.assertEqual(1,
len(list(page.boundingbox(190, 130, 230, 460))))
# cropping it with the same dimensions
# NOTE: This will fail if convert (from imagemagick) isn't installed)
try:
page.crop(190, 130, 230, 460)
except errors.ExternalCommandError:
# the rest of the tests cannot succeed now. FIXME: We
# should try to find a way to run them anyway
return
# should also result in just one box -- the bottom one
boxes = list(page.boundingbox())
self.assertEqual(1, len(boxes))
box = boxes[0]
self.assertEqual("This is a simple document in PDF format. ", str(box))
self.assertEqual('#000000', box.font.color)
self.assertEqual(16, box.font.size)
self.assertEqual('1', box.font.id)
self.assertEqual('Cambria', box.font.family)
# this box should have four text elements
self.assertEqual(4, len(box))
self.assertEqual(None, box[0].tag)
self.assertEqual("i", box[1].tag)
self.assertEqual("ib", box[2].tag)
self.assertEqual(None, box[3].tag)
def test_dontkeep(self):
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.xml.bz2"))
try:
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir,
keep_xml=False)
except errors.ExternalCommandError:
self._copy_sample()
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir,
keep_xml=False)
# No XML file should exist
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.xml"))
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.xml.bz2"))
def test_bz2(self):
try:
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir,
keep_xml="bz2")
except errors.ExternalCommandError:
self._copy_sample()
# bzip2 our canned sample.xml
with open(self.datadir + os.sep + "sample.xml", "rb") as rfp:
wfp = BZ2File(self.datadir + os.sep + "sample.xml.bz2", "wb")
wfp.write(rfp.read())
wfp.close()
os.unlink(self.datadir + os.sep + "sample.xml")
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir,
keep_xml="bz2")
# a temporary copy of the pdf file should not be lying around in workdir
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.pdf"))
# but the XML file (only in bz2 format) should be stored
self.assertTrue(os.path.exists(self.datadir + os.sep + "sample.xml.bz2"))
self.assertFalse(os.path.exists(self.datadir + os.sep + "sample.xml"))
# first page, first box
self.assertEqual("Document title ", str(reader[0][0]))
# parsing again should reuse the existing sample.xml.bz2
reader = PDFReader(filename="test/files/pdfreader/sample.pdf",
workdir=self.datadir,
keep_xml="bz2")
def test_convert(self):
# how to test this when soffice isnt available and on $PATH?
pass
def test_ocr(self):
try:
if not os.environ.get("FERENDA_TEST_TESSERACT"):
raise errors.ExternalCommandError
reader = PDFReader(filename="test/files/pdfreader/scanned.pdf",
workdir=self.datadir,
ocr_lang="swe")
except errors.ExternalCommandError:
self._copy_sample()
reader = PDFReader(filename="test/files/pdfreader/scanned.pdf",
workdir=self.datadir,
ocr_lang="swe")
# assert that a hOCR file has been created
self.assertTrue(os.path.exists(self.datadir + os.sep + "scanned.hocr.html"))
# assert that we have two pages
self.assertEqual(2, len(reader))
# assert that first element in the first textbox in the first
# page corresponds to the first bbox, scaled by the
# pixel/point scaling factor.
self.assertEqual("Regeringens ", str(reader[0][0][0]))
self.assertEqual(38, reader[0][0][0].top)
self.assertEqual(31, reader[0][0][0].left)
self.assertEqual(18, reader[0][0][0].height)
self.assertEqual(96, reader[0][0][0].width)
# assert that the <s>third</s>fifth textbox (which has mostly
# normal text) is rendered correctly (note that we have a
# couple of OCR errors).
# self.assertEqual("Regeringen föreslår riksdagen att anta de förslag som har tagits. upp i bifogade utdrag ur regeringsprotokollet den 31 oktober l99l.", util.normalize_space(str(reader[0][3])))
self.assertEqual("Regeringen föreslår riksdagen att anta de förslag som har tagits. upp i", util.normalize_space(str(reader[0][5])))
def test_fallback_ocr(self):
try:
# actually running tesseract takes ages -- for day-to-day
# testing we can just as well use the canned hocr.html
# files that _copy_sample fixes for us.
if not os.environ.get("FERENDA_TEST_TESSERACT"):
raise errors.ExternalCommandError
reader = PDFReader(filename="test/files/pdfreader/scanned-ecma-99.pdf",
workdir=self.datadir,
images=False)
except errors.ExternalCommandError:
self._copy_sample()
reader = PDFReader(filename="test/files/pdfreader/scanned-ecma-99.pdf",
workdir=self.datadir,
images=False)
self.assertTrue(reader.is_empty())
reader = PDFReader(filename="test/files/pdfreader/scanned-ecma-99.pdf",
workdir=self.datadir,
ocr_lang="eng")
self.assertFalse(reader.is_empty())
self.assertEqual(2, len(reader))
self.assertEqual("EUROPEAN COMPUTER MANUFACTURERS ASSOCIATION",
util.normalize_space(str(reader[0][1])))
def test_custom_encoding(self):
try:
reader = PDFReader(filename="test/files/pdfreader/custom-encoding.pdf",
workdir=self.datadir)
except errors.ExternalCommandError as e:
print("test_custom_encoding got ExternalCommandError %s, copying sample and retrying" % e)
self._copy_sample()
reader = PDFReader(filename="test/files/pdfreader/custom-encoding.pdf",
workdir=self.datadir)
# textbox 5 and 6 uses a font with a custom encoding, make
# sure that this is properly decoded.
tbs = list(reader.textboxes())
self.assertEqual("Göran Persson", str(tbs[5]))
self.assertEqual("Bosse Ringholm", str(tbs[6]))
self.assertEqual("(Finansdepartementet)", str(tbs[7]))
class AsXHTML(unittest.TestCase, FerendaTestCase):
def _test_asxhtml(self, want, body):
got = etree.tostring(body.as_xhtml(None), pretty_print=True)
self.assertEqualXML(want, got)
def test_basic(self):
body = Textbox([Textelement("test", tag=None)],
top=0, left=0, width=100, height=100, fontid=0)
want = """
<p xmlns="http://www.w3.org/1999/xhtml" class="textbox" style="top: 0px, left: 0px, height: 100px, width: 100px">test</p>
"""
self._test_asxhtml(want, body)
def test_elements_with_tags(self):
body = Textbox([Textelement("normal", tag=None),
Textelement("bold", tag="b"),
Textelement("italic", tag="i"),
Textelement("both", tag="bi")
], top=0, left=0, width=100, height=100, fontid=0)
want = """
<p xmlns="http://www.w3.org/1999/xhtml" class="textbox" style="top: 0px, left: 0px, height: 100px, width: 100px">normal<b>bold</b><i>italic</i><b><i>both</i></b></p>
"""
self._test_asxhtml(want, body)
def test_leading_tag(self):
body = Textbox([Textelement("bold", tag="b"),
Textelement("normal", tag=None),
], top=0, left=0, width=100, height=100, fontid=0)
want = """
<p xmlns="http://www.w3.org/1999/xhtml" class="textbox" style="top: 0px, left: 0px, height: 100px, width: 100px"><b>bold</b>normal</p>
"""
self._test_asxhtml(want, body)
def test_tag_merge(self):
body = Textbox([Textelement("identical ", tag=None),
Textelement("tags ", tag=None),
Textelement("should ", tag="b"),
Textelement("merge", tag="b"),
], top=0, left=0, width=100, height=100, fontid=0)
want = """
<p xmlns="http://www.w3.org/1999/xhtml" class="textbox" style="top: 0px, left: 0px, height: 100px, width: 100px">identical tags <b>should merge</b></p>
"""
self._test_asxhtml(want, body)