-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path__init__.py
More file actions
295 lines (263 loc) · 10.1 KB
/
__init__.py
File metadata and controls
295 lines (263 loc) · 10.1 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python3
"""
Python interface to Apache PDFBox.
"""
import hashlib
import html.parser
import os
import pathlib
import re
import shutil
import urllib.request
import appdirs
import jpype
import jpype.imports
import pkg_resources
pdfbox_archive_url = 'https://archive.apache.org/dist/pdfbox/'
class _PDFBoxVersionsParser(html.parser.HTMLParser):
"""
Class for parsing versions available on PDFBox archive site.
"""
def feed(self, data):
self.result = []
super(_PDFBoxVersionsParser, self).feed(data)
def handle_starttag(self, tag, attrs):
if tag == 'a':
for a in attrs:
if a[0] == 'href':
s = a[1].strip('/')
if re.search('\d+\.\d+\.\d+.*', s):
self.result.append(s)
class PDFBox(object):
"""
Python interface to Apache PDFBox.
Methods
-------
extract_text(input_path, output_path='',
password=None, encoding=None, html=False, sort=False,
ignore_beads=False, start_page=1, end_page=None)
Extract all text from PDF file.
pdf_to_images(input_path, password=None,
imageType=None, outputPrefix=None,
startPage=None, endPage=None,
page=None, dpi=None, color=None, cropbox=None, time=True)
Extract all pages of PDF file as images.
extract_images(input_path, password=None, prefix=None,
directJPEG=False)
Extract all images from a PDF file.
"""
def _verify_sha512(self, data, digest):
"""
Verify SHA512 checksum.
"""
return hashlib.sha512(data).hexdigest() == digest
def _get_latest_pdfbox_url(self):
r = urllib.request.urlopen(pdfbox_archive_url)
try:
data = r.read()
except:
raise RuntimeError('error retrieving %s' % pdfbox_archive_url)
else:
data = data.decode('utf-8')
p = _PDFBoxVersionsParser()
p.feed(data)
# Temporarily disallow PDFBox 3 because of change in command line
# interface; get major version by splitting base_version because the major attrib is
# not defined for some Python installations:
versions = list(filter(lambda v: int(pkg_resources.parse_version(v).base_version.split('.')[0])<3,
p.result))
latest_version = sorted(versions, key=pkg_resources.parse_version)[-1]
return pdfbox_archive_url + latest_version + '/pdfbox-app-' + \
latest_version + '.jar'
def _get_pdfbox_path(self):
"""
Return path to local copy of PDFBox jar file.
"""
# Use PDFBOX environmental variable if it exists:
if 'PDFBOX' in os.environ:
pdfbox_path = pathlib.Path(os.environ['PDFBOX'])
if not pdfbox_path.exists():
raise RuntimeError('pdfbox not found')
return pdfbox_path
# Use platform-specific cache directory:
a = appdirs.AppDirs('python-pdfbox')
cache_dir = pathlib.Path(a.user_cache_dir)
# Try to find pdfbox-app-*.jar file with most recent version in cache directory:
file_list = list(cache_dir.glob('pdfbox-app-*.jar'))
if file_list:
def f(s):
v = re.search('pdfbox-app-([\w\.\-]+)\.jar', s.name).group(1)
return pkg_resources.parse_version(v)
return sorted(file_list, key=f)[-1]
else:
# If no jar files are cached, find the latest version jar, retrieve it,
# cache it, and verify its checksum:
pdfbox_url = self._get_latest_pdfbox_url()
sha512_url = pdfbox_url + '.sha512'
r = urllib.request.urlopen(pdfbox_url)
try:
data = r.read()
except:
raise RuntimeError('error retrieving %s' % pdfbox_url)
else:
if not os.path.exists(cache_dir.as_posix()):
cache_dir.mkdir(parents=True)
pdfbox_path = cache_dir.joinpath(pathlib.Path(pdfbox_url).name)
with open(pdfbox_path.as_posix(), 'wb') as f:
f.write(data)
r = urllib.request.urlopen(sha512_url)
encoding = r.headers.get_content_charset('utf-8')
try:
sha512 = r.read().decode(encoding).strip()
except:
raise RuntimeError('error retrieving sha512sum')
else:
if not self._verify_sha512(data, sha512):
raise RuntimeError('failed to verify sha512sum')
return pdfbox_path
def __init__(self):
self.pdfbox_path = self._get_pdfbox_path()
jpype.addClassPath(self.pdfbox_path)
if not jpype.isJVMStarted():
jpype.startJVM(convertStrings=False)
import org.apache.pdfbox.tools as tools
self.pdfbox_tools = tools
def extract_text(self, input_path, output_path='',
password=None, encoding=None, html=False, sort=False,
ignore_beads=False, start_page=1, end_page=None, console=False):
"""
Extract all text from PDF file.
Parameters
----------
input_path : str
Input PDF file.
output_path : str
Output text file. If not specified, the extracted text is written to
a text file with the same basename as the input file.
password : str
PDF password.
encoding : str
Text file encoding.
html : bool
If True, extract as HTML.
sort : bool
If True, sort text before returning it.
ignore_beads : bool
If True, ignore separation by beads.
start_page : int
First page to extract (starting with 1).
end_page : int
Last page to extract (starting with 1).
console : bool
If True, write output to console.
"""
options = []
if password:
options.extend(['-password', password])
if encoding:
options.extend(['-encoding', encoding])
if html:
options.append('-html')
if sort:
options.append('-sort')
if ignore_beads:
options.append('-ignoreBeads')
if start_page:
options.extend(['-startPage', str(start_page)])
if end_page:
options.extend(['-endPage', str(end_page)])
if console:
options.append('-console')
args = options
args.append(str(pathlib.Path(input_path).expanduser()))
if output_path:
args.append(str(pathlib.Path(output_path).expanduser()))
self.pdfbox_tools.ExtractText.main(args)
def pdf_to_images(self, input_path, password=None,
imageType=None, outputPrefix=None,
startPage=None, endPage=None,
page=None, dpi=None, color=None, cropbox=None,time=True):
"""
Extract all pages of PDF file as images.
Parameters
----------
input_path : str
Input PDF file.
password : str
PDF password.
imageType : str
The image type to write to. Currently only jpg or png (default:
jpg).
outputPrefix : str
The prefix to the image file (default: name of PDF document).
e.g
>> outputPrefix = '/output/': Images saved in `output` directory
as 1.jpg, 2.jpg, etc.
>> outputPrefix = '/output' : Images saved in `output` directory
as output1.jpg, output2.jpg, etc.
in the same location where the input file is.
startPage : bool
The first page to convert, one-based (default: 1).
endPage : bool
The last page to convert, one-based (default: last).
page : int
The only page to extract, one-based.
dpi : int
DPI resolution of exported images (default:
detected from screen, or 96 if headless).
color : str
The color depth; may be set to `bilevel`, `gray`, `rgb`, `rgba`
(default: `rgb`)
cropbox : str
The page area to export, e.g "34 45 56 67"
time : int
Prints timing information to stdout.
"""
options = []
if password:
options.extend(['-password', password])
if imageType:
options.extend(['-imageType', imageType])
if outputPrefix:
options.extend(['-outputPrefix', str(pathlib.Path(outputPrefix).expanduser())])
if startPage:
options.extend(['-startPage', str(startPage)])
if endPage:
options.extend(['-endPage', str(endPage)])
if page:
options.extend(['-page', str(page)])
if dpi:
options.extend(['-dpi', str(dpi)])
if color:
options.extend(['-color', str(color)])
if cropbox:
options.extend(['-cropbox', str(cropbox)])
if time:
options.append('-time')
args = options
args.append(str(pathlib.Path(input_path).expanduser()))
self.pdfbox_tools.PDFToImage.main(args)
def extract_images(self, input_path, password=None, prefix=None, directJPEG=False):
"""
Extract all images from a PDF file.
Parameters
----------
input_path : str
Input PDF file.
password : str
PDF password.
prefix : str
The prefix to the image file (default: name of PDF document).
directJPEG: bool
Forces the direct extraction of JPEG images regardless of colorspace (default: False).
"""
options = []
if password:
options.extend(['-password', password])
if prefix:
options.extend(['-prefix', str(pathlib.Path(prefix).expanduser())])
if directJPEG:
options.extend(['-directJPEG', directJPEG])
args = options
args.append(str(pathlib.Path(input_path).expanduser()))
self.pdfbox_tools.ExtractImages.main(args)