Sample PDF file (1 page)
Minimal well-formed single-page PDF 1.4 with selectable Helvetica text.
↓ Download valid-1page.pdf| Filename | valid-1page.pdf |
| Format | |
| Size | 750 bytes (750 bytes) |
| MIME type | application/pdf |
| SHA-256 | 84e95ec0084af11ba5ddee05e2a6755a46124912b65e1378ff80bc1e119609ec |
Handle it in code
Each snippet below was run against this exact file with Python 3.14.4 and Node 24.15.0 when this page was built, and the output shown is what it printed.
Python
A cheap structural check before handing the file to a PDF library, which will often repair these silently. Use it to reject or flag uploads, not to validate PDFs fully.
import re
with open("valid-1page.pdf", "rb") as f:
data = f.read()
problems = []
if not data.startswith(b"%PDF-"):
problems.append("no %PDF- header")
tail = data[-1024:]
if b"%%EOF" not in tail:
problems.append("no %%EOF marker: the file is truncated")
m = re.search(rb"startxref\s+(\d+)\s+%%EOF", tail)
if m:
offset = int(m.group(1))
at = data[offset:offset + 20]
if offset >= len(data) or not (at.startswith(b"xref") or re.match(rb"\d+ \d+ obj", at)):
problems.append(f"startxref points at byte {offset}, where there is no cross-reference table")
print("; ".join(problems) or "structure looks intact")Run against this file, it prints: structure looks intact
Python
Quick triage without a PDF library: count page objects and look for the text-drawing operators (Tj, TJ) in the content streams, inflating Flate streams with zlib. Use it to route files, for example image-only scans to OCR. PDFs that pack objects into compressed object streams need a real parser such as pypdf.
import re
import zlib
with open("valid-1page.pdf", "rb") as f:
data = f.read()
pages = len(re.findall(rb"/Type\s*/Page\b", data))
drawing = 0
for m in re.finditer(rb"(?<!end)stream\r?\n", data):
raw = data[m.end():data.find(b"endstream", m.end())]
try:
raw = zlib.decompressobj().decompress(raw)
except zlib.error:
pass # not Flate-compressed
if re.search(rb"\bT[Jj]\b", raw):
drawing += 1
print(f"page objects: {pages}, content streams that draw text: {drawing}")
if pages == 0:
print("no pages: most readers show an error or a blank document")
elif drawing == 0:
print("no text layer: extraction returns nothing, so this needs OCR")Run against this file, it prints: page objects: 1, content streams that draw text: 1