Compare commits

..
Author SHA1 Message Date
Abimael Martell 512c50c64d napi: add probe-doc.mjs for single-region detect_vector_grid checks
Generalized one-shot probe — calls detectVectorGridInRegion against
a single PDF + region without spinning up the api or running the bench.
Useful for fast pdf-inspector dev loops:

  cargo build --release && cd napi && bun run build:debug
  node probe-doc.mjs <pdf-path> [page] [bbox] [dpi]

Output: cells/rows/cols and timing, or null when the region has no
vector grid. ~5s round-trip vs ~30s for the full bench harness.

Generalizes the existing probe-indent.mjs pattern so it works on
arbitrary PDFs, not just the multi-line indent fixture.
2026-05-07 23:05:42 -07:00
6 changed files with 70 additions and 251 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@firecrawl/pdf-inspector",
"version": "1.8.8",
"version": "1.8.7",
"description": "Fast PDF classification and text extraction. Detect text-based vs scanned PDFs, extract text by region with quality checks. Native Rust performance via napi-rs.",
"main": "index.js",
"types": "index.d.ts",
+64
View File
@@ -0,0 +1,64 @@
#!/usr/bin/env node
// Generalized one-shot probe: run detectVectorGridInRegion against a single
// PDF + region without spinning up the api or the bench.
//
// Usage:
// node probe-doc.mjs <pdf-path> [page] [bbox] [dpi]
//
// pdf-path : path to a PDF on disk (required)
// page : 0-indexed page number (default 0)
// bbox : "x0,y0,x1,y1" in PDF points (default = full standard letter)
// dpi : render dpi (default 200)
//
// Examples:
// node probe-doc.mjs ~/Code/opendataloader-bench/pdfs/01030000000127.pdf 0
// node probe-doc.mjs /tmp/foo.pdf 3 "50,100,560,700" 200
// node probe-doc.mjs /tmp/foo.pdf # page 0, full page, dpi 200
//
// Prints: cells / rows / cols / null. Useful for fast pdf-inspector loops:
// cargo build --release && bun run build:debug && node probe-doc.mjs ...
import { readFileSync, existsSync } from "node:fs";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const { detectVectorGridInRegion } = require("./index.js");
const [, , pdfPath, pageArg, bboxArg, dpiArg] = process.argv;
if (!pdfPath) {
console.error("usage: node probe-doc.mjs <pdf-path> [page] [bbox] [dpi]");
process.exit(2);
}
if (!existsSync(pdfPath)) {
console.error(`pdf not found: ${pdfPath}`);
process.exit(2);
}
const page = pageArg !== undefined ? Number(pageArg) : 0;
const bbox = bboxArg
? bboxArg.split(",").map(Number)
: [0, 0, 612, 792]; // standard US letter
const dpi = dpiArg !== undefined ? Number(dpiArg) : 200;
if (bbox.length !== 4 || bbox.some(Number.isNaN)) {
console.error(`invalid bbox "${bboxArg}", expected "x0,y0,x1,y1"`);
process.exit(2);
}
const pdf = readFileSync(pdfPath);
const t0 = Date.now();
const result = detectVectorGridInRegion(pdf, page, bbox, dpi);
const ms = Date.now() - t0;
if (!result) {
console.log(`null (${ms}ms) page=${page} bbox=${bbox.join(",")} dpi=${dpi}`);
process.exit(0);
}
const rows = result.structureTokens.filter((t) => t === "<tr>").length;
const cols = rows > 0 ? result.cellBboxes.length / rows : 0;
console.log(
`cells=${result.cellBboxes.length} rows=${rows} cols=${cols} (${ms}ms) page=${page} bbox=${bbox.join(",")} dpi=${dpi}`,
);
-40
View File
@@ -1280,46 +1280,6 @@ mod vector_grid_tests {
);
}
/// Regression for `wired_header_data_misalign.pdf` — a single page from a
/// parts catalog with a 4-column wire-bordered table (`Item | EAN | Nombre
/// | Cant`). Column headers are centered/right-aligned inside their cells
/// while data is left-aligned, so cluster_x_positions merges or drops
/// columns and the cell-rect fallback used to assign text to the wrong
/// columns (lost a column, fragmented neighbor cells). The fix prefers
/// rect-border-derived column edges when they're well-distributed across
/// the actual text items. This test asserts the detector keeps all 4
/// columns and every column ends up populated.
#[test]
fn wired_header_data_misalign_keeps_all_columns() {
let tables = detect_rect_tables_in_fixture("tests/fixtures/wired_header_data_misalign.pdf");
let table = tables
.iter()
.find(|t| t.columns.len() == 4 && t.rows.len() >= 5)
.unwrap_or_else(|| {
panic!(
"expected a 4-column ≥5-row table; got {:?}",
tables
.iter()
.map(|t| (t.rows.len(), t.columns.len()))
.collect::<Vec<_>>()
)
});
for c in 0..4 {
let populated_rows = table
.cells
.iter()
.filter(|row| !row[c].trim().is_empty())
.count();
assert!(
populated_rows >= 2,
"column {} only populated in {} rows; cells: {:?}",
c,
populated_rows,
table.cells
);
}
}
#[test]
fn test_crop_px_bbox_is_plausible_bounds() {
let crop = [10.0, 20.0, 110.0, 220.0];
+5 -86
View File
@@ -110,21 +110,15 @@ pub fn detect_tables_from_lines(items: &[TextItem], lines: &[PdfLine], page: u32
return Vec::new();
}
// Reject page-spanning frames: a decorative outer border has just 4
// edges (top/bottom/left/right). Real full-page tables — common in
// governmental ledgers, financial reports, etc. — span the same A4 /
// Letter dimensions but have many internal row/column rules. Only
// reject when the line set looks like a bare frame, not a grid.
// Reject page-spanning frames: if the grid covers >90% of a standard page
// dimension in both axes, it's a border frame, not a table.
// Standard pages are ~595×842 (A4) or ~612×792 (Letter).
if table_width > 500.0 && table_height > 700.0 && horizontals.len() <= 4 && verticals.len() <= 4
{
if table_width > 500.0 && table_height > 700.0 {
log::debug!(
"detect_lines p{}: rejected — page-spanning frame ({:.0}×{:.0}, {} h + {} v)",
"detect_lines p{}: rejected — page-spanning frame ({:.0}×{:.0})",
page,
table_width,
table_height,
horizontals.len(),
verticals.len()
table_height
);
return Vec::new();
}
@@ -416,81 +410,6 @@ mod tests {
assert!(tables.is_empty());
}
#[test]
fn test_page_spanning_bare_frame_rejected() {
// Just an outer A4-sized rectangle: 2 horizontals + 2 verticals.
// No internal structure → decorative border, not a table.
let lines = vec![
make_hline(20.0, 20.0, 575.0, 1), // top
make_hline(820.0, 20.0, 575.0, 1), // bottom
make_vline(20.0, 20.0, 820.0, 1), // left
make_vline(575.0, 20.0, 820.0, 1), // right
];
let items = vec![
make_item("title", 100.0, 100.0, 1),
make_item("body", 100.0, 200.0, 1),
];
let tables = detect_tables_from_lines(&items, &lines, 1);
assert!(
tables.is_empty(),
"Page-sized 4-edge frame should be rejected as decoration"
);
}
#[test]
fn test_page_spanning_grid_with_internal_lines_accepted() {
// Full-page table on an A4-sized layout (width > 500, height > 700)
// that previously hit the "page-spanning frame" early reject before
// downstream validation could even look at it. The line set has
// many internal horizontal + vertical rules, so the bare-frame
// guard should let it through.
let mut lines = Vec::new();
// 13 horizontal rules across a 540pt-wide span. Row heights vary
// a touch so the chart-gridline rejector (CV < 0.02) doesn't fire.
let x_left = 20.0_f32;
let x_right = 560.0_f32;
let h_ys: Vec<f32> = [
30.0, 95.0, 155.0, 220.0, 280.0, 345.0, 410.0, 470.0, 535.0, 600.0, 660.0, 720.0, 780.0,
]
.to_vec();
for &y in &h_ys {
lines.push(make_hline(y, x_left, x_right, 1));
}
// 7 column dividers spanning full table height (>700pt span).
let v_xs = [20.0, 95.0, 175.0, 250.0, 340.0, 450.0, 560.0];
let y_top = *h_ys.first().unwrap();
let y_bot = *h_ys.last().unwrap();
for &x in &v_xs {
lines.push(make_vline(x, y_top, y_bot, 1));
}
// Populate every cell so the capture-ratio + density checks pass.
let mut items = Vec::new();
for r in 0..(h_ys.len() - 1) {
let row_y = (h_ys[r] + h_ys[r + 1]) / 2.0;
for c in 0..(v_xs.len() - 1) {
let col_x = (v_xs[c] + v_xs[c + 1]) / 2.0;
items.push(make_item("x", col_x, row_y, 1));
}
}
let tables = detect_tables_from_lines(&items, &lines, 1);
assert_eq!(
tables.len(),
1,
"Full-page table with internal grid should be accepted"
);
let t = &tables[0];
assert!(
t.cells.len() >= 6,
"expected ≥6 rows, got {}",
t.cells.len()
);
assert!(
t.cells[0].len() >= 3,
"expected ≥3 columns, got {}",
t.cells[0].len()
);
}
#[test]
fn test_single_column_rejected() {
// Only 2 col edges (1 column) — not a table even with verticals
-124
View File
@@ -1632,49 +1632,7 @@ fn detect_row_stripe_table_from_cell_rects(
}
};
// For wired-grid tables whose header text is centered/right-aligned but
// whose data is left-aligned, cluster_x_positions can drop the header-only
// x-cluster in its singleton-filter pass and merge adjacent data clusters
// when the gap is below threshold, losing a column. Rect borders are
// ground truth in that case — but only when each rect column actually
// holds text. Decorative or background rects (prose laid out in a frame,
// cell-fill rects with extra borders) can produce more rect-derived
// columns than the text supports; preferring rects there would split a
// logical column into spurious sub-columns.
let rect_cols_match_text = match (&rect_col_edges, &text_col_edges) {
(Some(rect_edges), _) if rect_edges.len() >= 4 => {
let num_rect_cols = rect_edges.len() - 1;
let mut col_item_counts = vec![0usize; num_rect_cols];
for (_, item) in &page_items {
let cx = item.x + item.width / 2.0;
for c in 0..num_rect_cols {
if cx >= rect_edges[c] - 2.0 && cx <= rect_edges[c + 1] + 2.0 {
col_item_counts[c] += 1;
break;
}
}
}
// Require every rect column to hold multiple text items. A rect
// column with no (or only one) item is decorative or the rect grid
// is detecting a spurious column the data does not need; in those
// cases the old text-cluster preference is the safer fallback.
col_item_counts.iter().all(|&n| n >= 2)
}
_ => false,
};
let (col_edges, columns_from_text) = match (rect_col_edges, text_col_edges) {
(Some(rect_edges), text_edges_opt) if rect_cols_match_text => {
debug!(
" cell-rect using {} rect-derived columns (text clusters: {}; rect cols well-distributed)",
rect_edges.len() - 1,
text_edges_opt
.as_ref()
.map(|e| (e.len() - 1) as i32)
.unwrap_or(-1)
);
(rect_edges, false)
}
(Some(rect_edges), Some(text_edges)) if rect_edges.len() <= text_edges.len() => {
debug!(
" cell-rect using {} rect-derived columns over {} text clusters",
@@ -3384,88 +3342,6 @@ mod tests {
assert!(table.cells[2][1].contains("deny unauthorized"));
}
/// Wire-bordered 4-column table whose header text is centered/right-aligned
/// inside each cell while the data is left-aligned: cluster_x_positions
/// merges adjacent columns (data Item→EAN gap is below threshold) and
/// drops the header-only x-clusters in the filter pass, leaving only 3
/// text-derived columns. Rect borders are 4 columns of ground truth.
/// Before the fix the cell-rect path preferred text edges when they were
/// the smaller set — losing a column. After the fix, 3+ rect columns
/// always win.
#[test]
fn wired_header_data_misaligned_keeps_all_columns_from_rects() {
let page = 1;
// 4 cols: Item | EAN | Nombre | Cant
let col_xs = [380.0_f32, 410.0, 470.0, 660.0, 700.0];
// Header + 9 data rows at 15pt tall each (y descending).
let row_ys: Vec<f32> = (0..=10).map(|r| 400.0 - 15.0 * r as f32).collect();
let mut rects: Vec<(f32, f32, f32, f32)> = Vec::new();
for r in 0..10 {
let y_top = row_ys[r];
let y_bot = row_ys[r + 1];
for c in 0..4 {
rects.push((col_xs[c], y_bot, col_xs[c + 1] - col_xs[c], y_top - y_bot));
}
}
let mut items: Vec<TextItem> = Vec::new();
// Header row (y ≈ 392.5): headers sit further to the right than data
// because they are centered/right-aligned in the cells.
items.push(make_item("Item", 389.0, 392.5, 9.0));
items.push(make_item("EAN", 432.0, 392.5, 9.0));
items.push(make_item("Nombre", 552.0, 392.5, 9.0));
items.push(make_item("Cant", 672.0, 392.5, 9.0));
let names = [
"Arnes Frontal",
"Arnes Motor",
"Arnes Piso",
"Arnes Techo",
"Arnes Puerta",
"Arnes Tablero",
"Arnes Trasero",
"Arnes Lateral",
"Arnes Sensor",
];
for r in 0..9 {
let y = 377.5 - 15.0 * r as f32;
items.push(make_item(&(r + 1).to_string(), 396.0, y, 9.0));
items.push(make_item("7701023403016", 410.0, y, 9.0));
items.push(make_item(names[r], 480.0, y, 9.0));
items.push(make_item("1", 680.0, y, 9.0));
}
let table = detect_row_stripe_table_from_cell_rects(&items, &rects, page)
.expect("wired 4-column table with header/data x-misalignment must detect");
assert_eq!(
table.columns.len(),
4,
"expected 4 columns from rect borders; cells: {:?}",
table.cells
);
for c in 0..4 {
let any_populated = table.cells.iter().any(|row| !row[c].trim().is_empty());
assert!(
any_populated,
"column {} empty across all rows; cells: {:?}",
c, table.cells
);
}
// Header row populated in all 4 cells.
let header = &table.cells[0];
assert_eq!(header[0].trim(), "Item");
assert_eq!(header[1].trim(), "EAN");
assert_eq!(header[2].trim(), "Nombre");
assert_eq!(header[3].trim(), "Cant");
// First data row: Item="1", EAN, name, count="1" — no Item↔EAN merge.
let data1 = &table.cells[1];
assert_eq!(data1[0].trim(), "1");
assert_eq!(data1[1].trim(), "7701023403016");
assert!(data1[2].trim().contains("Arnes"));
assert_eq!(data1[3].trim(), "1");
}
#[test]
fn failed_cluster_no_hint_without_items() {
// Rects with no text items inside → no failed-cluster hint generated.
Binary file not shown.