MD
PDFtoMarkdown
Algorithmic Deep Dive

How PDF Layout Detection & Spatial Parsing Works

A masterclass on PDF binary coordinate systems, affine transformations, baseline clustering, and how WebAssembly transforms disjointed glyphs into clean Markdown ASTs.

100% In-Browser Private
8 min read
Updated September 2026

Experience Spatial Layout Reconstruction

100% In-Browser Conversion — Your Files Are Never Uploaded

Drag & Drop your PDF document here

or browse files on your device

⚡ No file size limit 🔒 100% Private
Markdown Output
2D Spatial Bounding-Box Clustering Algorithm and Matrix Transformation Diagram

1. The PDF Coordinate Matrix Model

In the PDF ISO 32000-1 specification, text positioning is defined by a 6-element affine transformation matrix:

$$\begin{bmatrix} x' & y' & 1 \end{bmatrix} = \begin{bmatrix} x & y & 1 \end{bmatrix} \begin{bmatrix} a & b & 0 \\ c & d & 0 \\ e & f & 1 \end{bmatrix}$$

Here, [a, b, c, d] control scaling and rotation, while [e, f] define horizontal and vertical translation offsets. Furthermore, PDF coordinates place $(0,0)$ at the bottom-left corner of the page, whereas modern document models (DOM, Markdown AST) place $(0,0)$ at the top-left corner.

Frequently Asked Questions

Everything you need to know about format extraction, privacy, and markdown compatibility.

Why can't computers read PDFs like normal text files?

PDF files are not formatted as text streams with paragraphs or lines. A PDF is an explicit 2D graphical display instruction program. Text characters are drawn at arbitrary coordinate points (e.g., 'Draw glyph at X=72, Y=540'). The PDF specification does not define where a paragraph begins, whether two words belong to the same sentence, or what constitutes a table cell.

How does the Euclidean font scaling formula work?

Text scaling in PDFs is defined by a 2x2 affine transformation matrix [scaleX, skewY, skewX, scaleY]. To compute the actual rendered font size in points, our engine calculates the Euclidean norm: fontSize = sqrt(scaleX^2 + skewY^2). This allows us to deterministically map rendered font sizes to # H1, ## H2, and ### H3 headings.

How does the parser group characters into lines?

The engine clusters characters along the vertical Y-axis using a ±5px proximity tolerance. Glyphs sharing the same baseline are sorted left-to-right along the X-axis. If the horizontal gap between characters exceeds the average glyph width, a whitespace delimiter is inserted.

How are multi-column layouts de-interleaved?

The parser computes a 1D histogram of text box coordinates projected onto the X-axis. Valleys with zero character density identify vertical column gutters. The parser then traverses each column sequentially from top to bottom before moving to the next column.

Related Conversion Guides & Workflows

Explore dedicated documentation for other document formats and developer pipelines.