Parse Spatial Data
The Fork PDF engine generates a binary Protobuf file (data.pb) containing exact layout mappings.
The way you access this data depends on whether you are running a server-side validation test or parsing a
downloaded bundle locally.
Units: geometry is in PDF points (1/72"), and pages are 0-indexed.
printableWidth/printableHeight are the full page size in points. See
Spatial Queries for units, page indexing, and raster mapping.
Schema v2: SDK 2.1.0 decodes the additive spatial schema v2. It adds page metadata,
per-page box fragments, IFC lines, shaped runs, resolved styles, raw DOM text, and offset mappings. Legacy
data.pb files still load with doc.schemaVersion === 0 and an empty
doc.pageInfos array.
Output limit: spatial ZIPs are capped at 39 MB. ForkPDF serializes the rendered DOM into
data.pb, including element attributes. Inline data URI images are carried in both the request
HTML and data.pb, so they can make the ZIP roughly 2× the PDF and also count toward the 20 MB
request limit. URL images keep both the request and data.pb small. Keep inline-data-URI PDFs
around 18 MB or less. See Limits & file sizes for the measured comparison.
Generate a Spatial Bundle with cURL
curl -X POST "https://api.forkpdf.com/v1/generate-pdf" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "x-project-id: YOUR_PROJECT_ID" \
-H "Content-Type: application/json" \
-d '{
"htmlContent": "<h1>Hello</h1>",
"format": "A4",
"spatial": true
}'
Example Response
{
"success": true,
"message": "File successfully generated and saved.",
"id": "xxxxxxxx-9cef-71ef-8b38-9ad2ae57a3cc",
"s3Key": "results/retention-24h/.../result.zip",
"downloadUrl": "https://.../result.zip",
"sizeBytes": 13805,
"testResults": null
}
The ZIP contains document.pdf and the spatial map data.pb. Download and unzip
it, then load data.pb with the SDK below.
Server-Side (Test Cases)
When writing a testCase validation script to validate spatial constraints during generation, you
only have CommonJS available. However, you do not need to initialize or load the data.pb file
manually. The engine pre-initializes the spatial document and injects it directly into your script as the
doc object. Note: The injected doc object has its pierceShadowDOM configuration strictly locked to false, meaning your server-side queries will always ignore Shadow DOM elements.
// Inside your generate() testCase string:
// The 'doc' object is already initialized for you.
const title = doc.querySelector('h1');
if (!title) {
return { success: false, reason: "Missing title" };
}
return { success: true };
Client-Side (Local SDK)
When you set spatial: true during generation, you receive a local ZIP bundle or in-memory buffers (when using unzip: true). You can use the SDK's SpatialDocument class to decode the spatial protobuf data into a queryable DOM tree by providing either a local file path string or an in-memory Buffer directly.
const { SpatialDocument } = require('forkpdf');
const doc = new SpatialDocument();
// Option A: Load directly from a file path
await doc.load('./output/data.pb', { pierceShadowDOM: true });
// Option B: Load directly from an in-memory Buffer (e.g., from client.generate with unzip: true)
await doc.load(response.binaryBuffer, { pierceShadowDOM: true });
Schema v2: Page Metadata
Schema v2 exposes page-local layout information directly from the SDK. Use
pageInfos or getPageInfo(pageIndex) to read the page rectangle, media box,
printable rectangle, margins, and CSS-to-PDF transform.
const page = doc.getPageInfo(0);
console.log(doc.schemaVersion); // 2 for new payloads, 0 for legacy
console.log(doc.pageInfos.length); // one entry per page
console.log(page.layout_rect_css_px); // page-local CSS-pixel layout rect
console.log(page.pdf_media_box_pt); // PDF points
console.log(page.css_to_pdf_transform);
Schema v2: Elements Across Pages
A block element that splits across pages has one entry in box_fragments per page.
Inline content uses the IFC root’s lines[].fragments[]. Each fragment has a 0-indexed
page_index and a page-local rect_lu in 1/64 CSS pixels.
const span = doc.querySelector('#s24');
const ifcRoot = doc.nodes.find((node) => node.index === span.ifc_root_index);
const textIndices = new Set(
span.childNodes
.filter((node) => node.tag_name === '#text')
.map((node) => node.index)
);
for (const line of ifcRoot.ifc.lines || []) {
for (const fragment of line.fragments || []) {
if (!textIndices.has(fragment.node_index)) continue;
console.log({
page: fragment.page_index,
text: fragment.rendered_text,
cssRect: {
x: fragment.rect_lu.x / 64,
y: fragment.rect_lu.y / 64,
width: fragment.rect_lu.width / 64,
height: fragment.rect_lu.height / 64
},
runs: fragment.runs
});
}
}
For a block element such as the parent div or p, use
element.box_fragments[] and its border_box_lu. Do not use the legacy
element.geometry to place one overlay per page; it is a single combined bounding box.
| Method Parameter | Type | Description |
|---|---|---|
input |
string | Buffer | Relative or absolute path to a data.pb file, or a direct Node.js memory Buffer. |
options.pierceShadowDOM |
boolean | If set to true, CSS selectors will search inside Shadow DOM boundaries. Defaults to false. |