"""Minimal PDF object reader (pure Python, no third-party modules). Reads a PDF as data: splits it into objects (including those packed in object streams), inflates FlateDecode streams, and walks the page tree. Nothing in the PDF is executed. Used to inspect how the preprint's figures are drawn (vector paths or embedded images). Usage: python3 -I pdf_objects.py [page-number ...] """ import re import sys import zlib OBJ_RE = re.compile(rb"(\d+)\s+(\d+)\s+obj\b") def inflate(raw): try: return zlib.decompress(raw) except zlib.error: d = zlib.decompressobj() return d.decompress(raw) def split_objects(data): """Return {objnum: (dict_bytes, stream_bytes_or_None)} for top-level objects.""" objs = {} for m in OBJ_RE.finditer(data): num = int(m.group(1)) start = m.end() end = data.find(b"endobj", start) if end < 0: continue body = data[start:end] s = re.search(rb"stream\r?\n", body) if s and b"endstream" in body: head = body[: s.start()] # Use /Length when it is a direct integer, else up to endstream. lm = re.search(rb"/Length\s+(\d+)(?!\s+\d+\s+R)", head) sstart = s.end() if lm: ln = int(lm.group(1)) stream = body[sstart: sstart + ln] else: e = body.rfind(b"endstream") stream = body[sstart:e].rstrip(b"\r\n") objs[num] = (head, stream) else: objs[num] = (body, None) return objs def resolve_length(objs, head, stream_full): return stream_full def decoded(objs, num): head, stream = objs[num] if stream is None: return None if b"/FlateDecode" in head: return inflate(stream) return stream def expand_objstreams(objs): """Unpack /Type /ObjStm streams into the object table.""" extra = {} for num, (head, stream) in list(objs.items()): if stream is None or b"/ObjStm" not in head: continue data = decoded(objs, num) n = int(re.search(rb"/N\s+(\d+)", head).group(1)) first = int(re.search(rb"/First\s+(\d+)", head).group(1)) nums = list(map(int, data[:first].split())) pairs = [(nums[2 * i], nums[2 * i + 1]) for i in range(n)] for i, (onum, off) in enumerate(pairs): a = first + off b = first + pairs[i + 1][1] if i + 1 < n else len(data) extra[onum] = (data[a:b], None) for k, v in extra.items(): if k not in objs: objs[k] = v return objs def refs(text, key): m = re.search(rb"/" + key + rb"\s*\[(.*?)\]", text, re.S) if m: return [int(x) for x in re.findall(rb"(\d+)\s+\d+\s+R", m.group(1))] m = re.search(rb"/" + key + rb"\s+(\d+)\s+\d+\s+R", text) if m: return [int(m.group(1))] return [] def page_list(objs): # find the root Pages object (has /Kids and no /Parent) roots = [n for n, (h, s) in objs.items() if re.search(rb"/Type\s*/Pages\b", h) and b"/Parent" not in h] order = [] def walk(n): h = objs[n][0] if re.search(rb"/Type\s*/Pages\b", h): for k in refs(h, b"Kids"): walk(k) else: order.append(n) for r in roots: walk(r) return order def main(): path = sys.argv[1] data = open(path, "rb").read() objs = expand_objstreams(split_objects(data)) pages = page_list(objs) print("objects:", len(objs), "pages:", len(pages)) want = [int(x) for x in sys.argv[2:]] or range(1, len(pages) + 1) for p in want: n = pages[p - 1] h = objs[n][0] contents = refs(h, b"Contents") res = re.search(rb"/Resources\s+(\d+)\s+\d+\s+R", h) res_text = objs[int(res.group(1))][0] if res else h xo = re.search(rb"/XObject\s*<<(.*?)>>", res_text, re.S) xobjs = re.findall(rb"/(\w+)\s+(\d+)\s+\d+\s+R", xo.group(1)) if xo else [] if not xo: xr = re.search(rb"/XObject\s+(\d+)\s+\d+\s+R", res_text) if xr: t = objs[int(xr.group(1))][0] xobjs = re.findall(rb"/(\w+)\s+(\d+)\s+\d+\s+R", t) clen = sum(len(decoded(objs, c) or b"") for c in contents) print(f"page {p}: obj {n}, contents {contents} ({clen} bytes decoded)") for name, xn in xobjs: xh = objs[int(xn)][0] sub = re.search(rb"/Subtype\s*/(\w+)", xh) w = re.search(rb"/Width\s+(\d+)", xh) hh = re.search(rb"/Height\s+(\d+)", xh) filt = re.findall(rb"/(\w+Decode)", xh) print(f" XObject {name.decode()} -> obj {xn.decode()}: " f"{sub.group(1).decode() if sub else '?'} " f"{w.group(1).decode() if w else ''}x{hh.group(1).decode() if hh else ''} " f"{[f.decode() for f in filt]}") if __name__ == "__main__": main()