Improve space detection logic in PDF text extraction
This commit is contained in:
parent
4ed6b17e98
commit
1bbd77f93b
1 changed files with 15 additions and 3 deletions
|
|
@ -142,10 +142,22 @@ export async function extractTextFromPDF(
|
||||||
const currentStartX = item.transform[4];
|
const currentStartX = item.transform[4];
|
||||||
const space = currentStartX - prevEndX;
|
const space = currentStartX - prevEndX;
|
||||||
|
|
||||||
if (space > ((item.width ?? 0) * 0.3)) {
|
// Get average character width as fallback
|
||||||
lineText += ' ' + item.str;
|
const avgCharWidth = (item.width ?? 0) / Math.max(1, item.str.length);
|
||||||
|
|
||||||
|
// Multiple conditions for space detection
|
||||||
|
const needsSpace =
|
||||||
|
// Primary check: significant gap between items
|
||||||
|
space > Math.max(avgCharWidth * 0.3, 2) ||
|
||||||
|
// Secondary check: natural word boundary
|
||||||
|
(!/^\W/.test(item.str) && !/\W$/.test(prevItem.str)) ||
|
||||||
|
// Tertiary check: items are far enough apart relative to their size
|
||||||
|
(space > ((prevItem.width ?? 0) * 0.25));
|
||||||
|
|
||||||
|
if (needsSpace) {
|
||||||
|
lineText += ' ' + item.str;
|
||||||
} else {
|
} else {
|
||||||
lineText += item.str;
|
lineText += item.str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prevItem = item;
|
prevItem = item;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue