ForkPDF SDK 2.1.0: Page-Accurate Spatial Data for Split Elements
SDK 2.1.0 adds schema v2 spatial data with per-page boxes, IFC lines, shaped runs, and exact coordinates for spans that cross page boundaries.
On this page
SDK 2.1.0 is a spatial-data release. It keeps the existing query API and adds the ground-truth layout data needed to answer a harder question: where did a specific piece of text land when the element crossed a page boundary?
For most documents, nothing changes. node.geometry remains the default way to get an element’s complete bounding box in PDF points. The new page-local data is only necessary when one DOM node produces multiple physical rectangles.
That distinction matters for paragraphs split across pages, repeated table headers, inline labels, signature anchors, and any layout where a single DOM node spans more than one page.
What is in 2.1.0
The SDK now decodes spatial schema v2 from data.pb:
schemaVersionandpageInfosonSpatialDocumentgetPageInfo(pageIndex)for page-local metadata and the CSS-to-PDF transform- Per-page
box_fragmentson block elements - Inline formatting contexts with items and line boxes
- Page-local line fragments for text nodes and atomic inline elements
- Shaped runs with font identity, advances, and optional glyph data
- Resolved text and layout styles
- Raw DOM text and offset mappings
- Exact per-page rectangles for elements that cross pages
Existing fields are unchanged. Old data.pb files still load with schemaVersion === 0 and an empty pageInfos array.
The cross-page problem
A span that crosses a page does not have one useful rectangle. It has one rectangle on the first page and another on the next page. Its default geometry is a combined representation and should not be used to place a single overlay.
Consider this output from a real v2 run:
#s24 pages: [0, 1]
page 0, line 38: "S24 alpha beta gamma delta epsilon"
page 0, line 39: "zeta eta theta ... rho sigma"
page 1, line 40: "tau."
The span has two fragments on page 0 and one on page 1. Each fragment carries its own page index and page-local rectangle.
Read the per-page rectangles
Use the span’s IFC root to find its line fragments. This example is self-contained: the helpers return one entry per page fragment and convert each rectangle through its own page transform.
function collectTextIndices(node, indices = new Set()) {
if (node.tag_name === '#text') {
indices.add(node.index);
return indices;
}
for (const child of node.childNodes || []) {
collectTextIndices(child, indices);
}
return indices;
}
function getSpanFragments(doc, selector) {
const span = doc.querySelector(selector);
if (!span) return [];
const ifcRoot = doc.nodes.find((node) => node.index === span.ifc_root_index);
if (!ifcRoot?.ifc) return [];
const textIndices = collectTextIndices(span);
const fragments = [];
for (const line of ifcRoot.ifc.lines || []) {
for (const fragment of line.fragments || []) {
if (fragment.node_index == null) continue;
if (!textIndices.has(fragment.node_index)) continue;
fragments.push(fragment);
}
}
return fragments;
}
function toCssRect(rect) {
return {
x: rect.x / 64,
y: rect.y / 64,
width: rect.width / 64,
height: rect.height / 64,
};
}
function toPdfRect(doc, fragment) {
const page = doc.getPageInfo(fragment.page_index);
const t = page?.css_to_pdf_transform;
const r = fragment.rect_lu;
if (!t || !r) return null;
const x0 = t.a * (r.x / 64) + t.e;
const x1 = t.a * ((r.x + r.width) / 64) + t.e;
const y0 = t.d * ((r.y + r.height) / 64) + t.f;
const y1 = t.d * (r.y / 64) + t.f;
return { x0, y0, x1, y1 };
}
const fragments = getSpanFragments(doc, '#s24');
console.log(fragments.map((fragment) => ({
page: fragment.page_index,
text: fragment.rendered_text,
css: fragment.rect_lu ? toCssRect(fragment.rect_lu) : null,
pdf: toPdfRect(doc, fragment),
runs: fragment.runs,
})));
rect_lu is a page-local Blink LayoutUnit rectangle. Divide by 64 to get CSS pixels, then apply the page’s css_to_pdf_transform to get PDF points. The result is a normal PDF-point rectangle with a bottom-left origin, ready for an overlay, highlight, or accessibility check.
Three different rectangles, three different jobs
| Data | Best for |
|---|---|
node.geometry |
Default combined element box in PDF points |
node.box_fragments[] |
Per-page boxes for block layout elements |
ifc.lines[].fragments[] |
Per-page positions for inline text and spans |
For a word that is wrapped in its own <span>, the line fragment gives you the exact page and rectangle for that word. If the span crosses a page, the fragments split at the page boundary. The helper walks nested inline elements recursively, so text inside <strong>, <em>, or another nested inline tag is included.
Compatibility is deliberate
Schema v2 is additive. No existing field was removed or renumbered:
- The new decoder reads old and new
data.pbfiles. - The previous decoder can still read the v1 fields from a new
data.pb. - Existing
geometry,pages, selectors,innerText, and form stamping behave as before. - TypeScript declarations include the new fields without changing existing types.
The result is one decoder that works with both generations of spatial data.
Install
npm install forkpdf@2.1.0
Both CommonJS and native ESM are supported, with bundled TypeScript declarations.
import { SpatialDocument, type SpatialShapedRun } from 'forkpdf';
const doc = new SpatialDocument();
await doc.load('./output/data.pb');
const page = doc.getPageInfo(0);
const heading = doc.querySelector('h1');
const run: SpatialShapedRun | undefined =
heading?.ifc?.lines?.[0]?.fragments?.[0]?.runs?.[0];
console.log(doc.schemaVersion, page?.css_to_pdf_transform, run?.advance_rendered_px);
Upgrade notes
SDK 2.1.0 is additive. No application changes are required:
- Continue using
node.geometryfor normal whole-element coordinates. - New per-page fields are available when you need them; they do not replace the existing API.
Read the SDK changelog for the release history, Spatial Data Parsing for loading bundles, and Spatial Queries for the complete coordinate model.
FAQ
Does SDK 2.1.0 still read old data.pb files?
Yes. Older files load with schemaVersion: 0, an empty pageInfos array, and unchanged existing fields.
Can I get separate coordinates for an element that crosses a page?
Yes. Use ifc.lines[].fragments[] for inline text and box_fragments[] for block elements. Both carry a page index and a page-local rectangle.
Does SDK 2.1.0 support both CommonJS and ESM?
Yes. SDK 2.1.0 ships CommonJS and native ESM entry points with bundled TypeScript declarations. It supports Node.js 18 and later.
Is schema v2 a breaking change?
No. The schema only adds fields. Existing field numbers and runtime behavior are preserved.
Try the ForkPDF API
Generate a PDF, inspect every element's page and coordinates, and return a layout patch — in one API call.
100 free credits. No credit card required.