#!/usr/bin/env python3 """Rough, stdlib-only text peek of a PDF (for checking licence/copyright statements). Inflates FlateDecode streams and prints literal strings from Tj/TJ operators. Usage: python3 -I pdf_text_peek.py [max_chars] Treats the PDF as untrusted data; nothing in it is executed. """ import re import sys import zlib def main() -> None: path = sys.argv[1] limit = int(sys.argv[2]) if len(sys.argv) > 2 else 20000 data = open(path, "rb").read() out = [] for m in re.finditer(rb"stream\r?\n", data): start = m.end() end = data.find(b"endstream", start) if end < 0: continue raw = data[start:end] try: txt = zlib.decompress(raw) except Exception: try: txt = zlib.decompressobj().decompress(raw) except Exception: continue if b"Tj" not in txt and b"TJ" not in txt: continue line = [] for tm in re.finditer(rb"\[(.*?)\]\s*TJ|\((.*?)(?