使用 pdf.js 在瀏覽器中偵測雙欄履歷
Detecting two-column resumes in the browser with pdf.js
以 pdf.js 建構的瀏覽器履歷檢查器會偵測 PDF 是否採用雙欄版面,並展示簡單解析器混讀側欄與工作經歷的結果;檔案全程留在瀏覽器內。檢查器會掃描文字寬度中段 15% 至 85% 的垂直位置,每 2pt 檢查一次,並以最多 1% 字元跨越作為空白分隔判準。為排除右對齊日期造成的誤判,兩側各須至少有 4 行和 20 個詞,少於一半行數屬日期類,且至少 30% 的文字行帶兩側都有文字。
A lot of resume advice says "don't use two columns, the ATS can't read them." That's mostly true, but it's hard to show someone. Their PDF looks fine on screen. So while building a small in-browser resume checker, I wanted to answer two concrete questions:
- Does this PDF actually have a side column?
- What would a naive parser produce from it?
Everything below runs client-side. The file never leaves the browser.
Step 1: get positioned text out of the PDF
pdf.js gives you text items with a transform matrix. The last two numbers are the x/y position (PDF coordinates, so y grows upward):
const page = await pdf.getPage(n);
const { width, height } = page.getViewport({ scale: 1 });
const { items } = await page.getTextContent();
const words = items
.filter(it => it.str.trim())
.map(it => ({ str: it.str, x: it.transform[4], y: it.transform[5], w: it.width, h: it.height }));
Step 2: what a naive parser sees
The simplest way to turn that into text is to sort top-to-bottom, then left-to-right, and glue items that share a baseline into one line:
const sorted = words.slice().sort((a, b) => b.y - a.y || a.x - b.x);
On a one-column resume this works. On a two-column one it reads straight across the page, so a line from your Skills sidebar ends up glued to a line from your job history:
HubSpot Grew organic signups 38% in 12 months
Google Ads Responsible for social media.
That is the failure people warn about, and it's exactly what I show users. Seeing their own lines mixed up makes the point far better than a warning.
Step 3: find the gutter
To detect a column split, I scan vertical lines across the middle of the page (from 15% to 85% of the text's width, every 2pt). For each x position I count how many characters belong to text items that cross it. A real gutter is a run of x positions where almost nothing crosses:
for (let s = minX + span * 0.15; s <= minX + span * 0.85; s += 2) {
let crossing = 0;
for (const it of words) {
if (it.x < s - 1 && it.x + it.w > s + 1) crossing += it.str.trim().length;
}
if (crossing <= totalChars * 0.01) { /* extend or start an empty run */ }
else { /* close the run */ }
}
I allow 1% because a single long heading or a horizontal rule label often spans the whole page. Then I try the widest empty run first and take its midpoint as the split.
Step 4: reject the false positives
An empty vertical strip doesn't always mean two columns. The common false positive is a normal one-column resume with dates right-aligned:
Marketing Manager Mar 2021 – Present
There's a big gap between the title and the date, so the gap looks like a gutter. Three checks filter these out. Each side must:
- have at least 4 lines and 20 words,
- be mostly not dates or short locations (fewer than half its lines can be date-ish),
- sit beside the other side: I bucket y positions into 6pt bands, and at least 30% of the bands must have text on both sides.
The date filter is the one that matters most. Without it, almost every resume with right-aligned dates gets flagged.
Step 5: read it in a sensible order
Once a split is accepted, the side with more characters is treated as the main column and read first, then the sidebar. That reading order is used for the rest of the checks (section detection, contact info, and so on), and also for the "fixed" one-column file the user can download.
DOCX is easier and harder
For Word files I unzip the package and read the XML directly. Layout problems are explicit there: w:cols with num > 1 means Word columns, a table holding most of the text means a layout table, and text boxes and header/footer parts are separate elements. It's easier to detect than PDF, but it turns up more ways to hide text, like contact details that only exist in the page header, which some parsers skip.
What I learned
- Showing the mixed-up text beats any explanation.
- Most of the work is in rejecting false positives, not in finding the gutter.
- Keep the rules fixed and the scoring visible. The same file should always get the same result, and users should be able to see why.
If you want to try it on your own resume, it's at atsresumechecker.app. It's free and runs in the browser. I'd love to hear about PDFs that break the column detection.
I used AI to help write this post. The approach and the code are from the actual project.
來源:DEV Community · dev.to