-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-pages.py
executable file
·282 lines (255 loc) · 9.53 KB
/
pdf-pages.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
281
282
#!/usr/bin/env python3
"""
A native Python module to get pages' sizes from PDF.
I needed this to get pages' sizes from huge PDFs (containing large images)
without big memory consumption.
Done by following [Portable Document Format – Part 1: PDF 1.7, First Edition]
(https://wwwimages2.adobe.com/content/dam/acom/en/devnet/pdf/PDF32000_2008.pdf)
found [here](https://www.adobe.com/devnet/pdf/pdf_reference.html).
**NOTE:** For a proper solution that uses poppler, see
[here](https://stackoverflow.com/a/51559543/1667018).
"""
from math import log10
import re
from sys import argv
def _pages_tree_to_list(pages, root=None):
"""
Return list of pages in the right order from PDF's tree.
"""
if root is None:
root = next(
ref
for ref in pages
if not any(ref in pgs for pgs in pages.values())
)
result = list()
for ref in pages[root]:
if ref in pages:
result.extend(_pages_tree_to_list(pages, ref))
else:
result.append(ref)
return result
def _str_to_num(s):
"""
Return string converted to `int` or `float`.
"""
try:
return int(s)
except ValueError:
return float(s)
def _ref_m_to_tuple(m):
"""
Return reference tuple from RegEx `Match`.
"""
return int(m.group("m")), int(m.group("n"))
def pdf_pages_sizes(fp, chunk_size=1024**2, units="px"):
"""
Return list of sizes for PDF in stream `fp` (open file, `StringIO`, etc).
:param chunk_size: The size of the chunks to be read from the file.
Shouldn't be too small (at least 10 or so). Smaller will usually mean
slower, while bigger uses more memory. The default of 1 MiB seems to
work just fine.
:param units: A string describing in which units should the result be
returned. Possible values: `"px"` (the default), `"in"`, `"mm"`,
`"cm"`.
:return: List of `(width, height)` tuples, each corresponding to one page.
The values are given in the unit corresponding with the `units`
parameter.
"""
# Debug print
def print_data():
print({k: v for k, v in data.items() if k != "text"})
# Get page size for a given reference
def get_mediabox(ref):
supported_units = {
"px": 1, "in": 1.0 / 72, "mm": 25.4 / 72, "cm": 2.54 / 72,
}
mediabox = data["mediaboxes"][ref]
unit_factor = supported_units[units]
while ref:
unit_factor *= data["units"].get(ref, 1)
ref = data["parents"].get(ref)
return tuple(value * unit_factor for value in mediabox[2:])
# Functions to handle recognised PDF entities
def obj(m):
if data["depth"] == 0:
data["ref"] = _ref_m_to_tuple(m)
def start(m):
data["depth"] += 1
def object_type(m):
if data["depth"] == 1:
data["type"] = m.group("type")
def mediabox(m):
data["mediabox"] = tuple(
_str_to_num(d) for d in re.split(r"\s+", m.group("size"))
)
def end(m):
data["depth"] -= 1
assert data["depth"] >= 0
if data["depth"] == 0:
if data["ref"]:
if data["type"] == "Page" and data["mediabox"]:
data["mediaboxes"][data["ref"]] = data["mediabox"]
elif data["type"] == "Pages" and data["kids"]:
data["pages"][data["ref"]] = data["kids"]
data["ref"] = None
data["type"] = None
data["mediabox"] = None
data["kids"] = list()
def stream(m):
data["in_stream"] = True
def endstream(m):
data["in_stream"] = False
data["needs_fresh_pos"] = True
def kids(m):
assert not data["kids"]
kids_list = re.split(r"\s+", m.group("kids"))
new_kids = [
(int(m), int(n)) for m, n in zip(kids_list[0::3], kids_list[1::3])
]
data["kids"].extend(new_kids)
if data["ref"] is not None:
data["parents"].update({kid: data["ref"] for kid in new_kids})
def parent(m):
assert data["ref"] is not None
data["parents"][data["ref"]] = _ref_m_to_tuple(m)
def userunit(m):
if data["depth"] == 1 and data["ref"]:
data["units"][data["ref"]] = _str_to_num(m.group("unit"))
# Regx Strings (subexpressions to use):
rs_one_to_list = r"{fmt}(?:\s+{fmt})*"
rs_ref = r"\d+\s+\d+\s+R"
rs_refs = rs_one_to_list.format(fmt=rs_ref)
rs_nref = r"(?P<m>\d+)\s+(?P<n>\d+)\s+R" # TODO \b
rs_float = r"(?:\b\d+(?:\.\d*)?|\.\d+\b)"
rs_floats = rs_one_to_list.format(fmt=rs_float)
# What to search for -> the function to handle it.
searches = {
r"(?P<m>\d+)\s+(?P<n>\d+)\s+obj\b": obj,
r"<<": start,
r"/Type\s*/(?P<type>\w+)": object_type,
r"/MediaBox\s*\[\s*(?P<size>" + rs_floats + r")\s*\]": mediabox,
r">>": end,
r"\bstream\b": stream,
r"\bendstream\b": endstream,
r"/Kids\s*\[\s*(?P<kids>" + rs_refs + r")\s*\]": kids,
r"/Parent\s+" + rs_nref: parent,
r"/UserUnit\s*(?P<unit>" + rs_float + r")": userunit,
}
# re has caching, but this allows us to use `pos` argument with `search`,
# which gives a nice speed improvement over cropping the text all the time.
compiled_searches = {restr: re.compile(restr) for restr in searches}
# Initial data; we could use normal variables and `nonlocal` instead,
# but this seemed neater and it is compatible with Python 2, which is
# where I need this module (a legacy project).
data = {
"depth": 0,
"in_stream": False,
"kids": list(),
"mediabox": None,
"mediaboxes": dict(),
"needs_fresh_pos": True,
"pages": dict(),
"parents": dict(),
"ref": None,
"text": "",
"type": None,
"units": dict(),
}
# Read and analyze the PDF.
old_chunk = ""
while True:
# Read the next chunk and add it to the current text.
chunk = fp.read(chunk_size)
if not chunk:
break
chunk = chunk.decode("latin-1")
data["text"] = old_chunk + chunk
# Chip those elements away, until none are left.
pos = dict()
offset = 0
data["needs_fresh_pos"] = True
while data["needs_fresh_pos"] or pos:
# Find all the elements that we recognise.
if data["needs_fresh_pos"]:
pos = {
restr: match
for restr, match in (
(
restr,
compiled_searches[restr].search(
data["text"], pos=offset,
),
)
for restr in searches.keys()
if (
not data["in_stream"]
or searches[restr].__name__ == "endstream"
)
)
if match
}
if not pos:
break
data["needs_fresh_pos"] = False
# Get the leftmost matched element.
restr, match = min(pos.items(), key=lambda it: it[1].start())
# Process it...
func = searches[restr]
if not data["in_stream"] or func.__name__ == "endstream":
# ...but only if not in stream or is a keyword that ends
# streams.
func(match)
# Reset the offset (to ignore the processed part of the text).
offset = match.end()
# Find the next `restr` element and update the dictionary of
# found elements.
if not data["needs_fresh_pos"]:
match = compiled_searches[restr].search(
data["text"], pos=offset,
)
if match:
pos[restr] = match
else:
pos.pop(restr)
# Remove unneeded text.
# This is the reason why chunk_size shouldn't be too small. Basically,
# it must be bigger than the length of anything (except the `/Kids`
# element) that we can find.
if offset:
data["text"] = data["text"][offset:]
old_chunk = (
data["text"]
if "/Kids" in data["text"] else
data["text"] if len(data["text"]) < len(chunk) else chunk
)
# Order the pages, get their sizes and return those as a list.
pages = _pages_tree_to_list(data["pages"])
assert len(pages) == len(data["mediaboxes"])
return [get_mediabox(ref) for ref in pages]
if __name__ == "__main__":
try:
fname = argv[1]
except IndexError:
print("Usage: {cmd} file_name.pdf [units]".format(cmd=argv[0]))
else:
try:
units = argv[2]
except IndexError:
units = "px"
with open(fname, "rb") as f:
sizes = pdf_pages_sizes(f, units=units)
if sizes:
fmt = " {{num:{max_len}d}}. {{size}}".format(
max_len=int(log10(len(sizes)) + 1),
)
print("Pages' sizes:")
for num, size in enumerate(sizes, start=1):
print(fmt.format(
num=num,
size="{w:.2f} x {h:.2f} {units}".format(
w=size[0], h=size[1], units=units,
),
))
else:
print("Not a single page in this PDF?!?")