-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.go
More file actions
445 lines (411 loc) · 11.4 KB
/
pdf.go
File metadata and controls
445 lines (411 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package parser
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
pdflib "github.com/ledongthuc/pdf"
)
// PDF is a pragmatic first-pass PDF parser.
//
// PDF is a layout format, not a structured format — there are no real
// headings in the wire layer, just runs of glyphs with font sizes and
// positions. To recover structure we:
//
// 1. Extract text per page, row-by-row, with font-size information.
// 2. Compute the median font size across the whole document.
// 3. Treat any row whose font size exceeds a threshold (1.2× median)
// AND that is short (<= 14 words) as a heading candidate.
// 4. Group headings into levels by font-size buckets (largest = level 1).
// 5. Everything else is body text for the most recent heading.
//
// This won't beat a PDF with a proper bookmark outline, but it recovers
// surprisingly usable structure from academic papers, whitepapers, and
// reports. A future parser can read the PDF's /Outlines dictionary
// directly for documents that have one.
//
// Encrypted PDFs, PDFs with non-standard fonts, and scanned PDFs (pure
// images) are not supported at this stage.
type PDF struct{}
// NewPDF returns a new PDF parser.
func NewPDF() *PDF { return &PDF{} }
// Name implements Parser.
func (*PDF) Name() string { return "pdf" }
// Accepts implements Parser.
func (*PDF) Accepts(contentType, filename string) bool {
if contentType == "application/pdf" {
return true
}
return HasExt(filename, ".pdf")
}
// Parse implements Parser.
func (*PDF) Parse(_ context.Context, r io.Reader) (*ParsedDoc, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
reader, err := pdflib.NewReader(bytes.NewReader(buf), int64(len(buf)))
if err != nil {
return nil, fmt.Errorf("pdf: open: %w", err)
}
rows, err := extractPDFRows(reader)
if err != nil {
return nil, err
}
if len(rows) == 0 {
return &ParsedDoc{
Title: "",
Sections: []Section{{Level: 1, Title: "Document", Content: ""}},
}, nil
}
// If the PDF ships with a real outline (bookmarks), use it as ground
// truth for structure — beats any font-size heuristic. We still rely
// on row extraction for section bodies by matching outline titles
// against the first occurrence of that text in the row stream.
if outline := reader.Outline(); len(outline.Child) > 0 {
if doc, ok := parsePDFWithOutline(outline, rows); ok {
return doc, nil
}
}
// Median font size — our reference for "normal body text".
sizes := make([]float64, 0, len(rows))
for _, r := range rows {
if r.fontSize > 0 {
sizes = append(sizes, r.fontSize)
}
}
sort.Float64s(sizes)
median := 0.0
if n := len(sizes); n > 0 {
median = sizes[n/2]
}
headingFloor := median * 1.2
// Unique heading sizes, largest first. These define heading levels:
// the largest bucket is level 1, next is level 2, etc. (capped at 6).
levelForSize := buildHeadingLevelMap(rows, headingFloor)
type flat struct {
level int
title string
body strings.Builder
}
flats := []*flat{{level: 0, title: ""}}
current := flats[0]
for _, row := range rows {
text := strings.TrimSpace(row.text)
if text == "" {
continue
}
lvl, isHeading := levelForSize[roundSize(row.fontSize)]
if isHeading && looksLikeHeading(text) {
current = &flat{level: lvl, title: text}
flats = append(flats, current)
continue
}
if current.body.Len() > 0 {
current.body.WriteString(" ")
}
current.body.WriteString(text)
}
if len(flats) > 1 && flats[0].level == 0 && strings.TrimSpace(flats[0].body.String()) == "" {
flats = flats[1:]
}
var title string
for _, f := range flats {
if f.level == 1 {
title = f.title
break
}
}
if title == "" && len(flats) > 0 {
title = flats[0].title
}
// Build hierarchy via level stack.
rootSec := &Section{Level: 0, Title: title}
stack := []*Section{rootSec}
for _, f := range flats {
sec := Section{
Level: f.level,
Title: f.title,
Content: strings.TrimSpace(f.body.String()),
}
if f.level == 0 {
if sec.Content == "" {
continue
}
sec.Level = 1
sec.Title = "Introduction"
}
for len(stack) > 1 && stack[len(stack)-1].Level >= sec.Level {
stack = stack[:len(stack)-1]
}
parent := stack[len(stack)-1]
parent.Children = append(parent.Children, sec)
tail := &parent.Children[len(parent.Children)-1]
stack = append(stack, tail)
}
// No headings recovered? Fall back to one "Document" section.
if len(rootSec.Children) == 0 {
var all strings.Builder
for _, f := range flats {
if s := strings.TrimSpace(f.body.String()); s != "" {
if all.Len() > 0 {
all.WriteString(" ")
}
all.WriteString(s)
}
}
rootSec.Children = []Section{{
Level: 1,
Title: "Document",
Content: all.String(),
}}
}
return &ParsedDoc{
Title: title,
Sections: rootSec.Children,
}, nil
}
type pdfRow struct {
page int
fontSize float64
text string
}
// extractPDFRows walks each page, grouping letters into rows by y-position
// and recording the dominant font size per row. ledongthuc/pdf's Content()
// returns individual glyphs; we reassemble them into lines.
func extractPDFRows(reader *pdflib.Reader) ([]pdfRow, error) {
numPages := reader.NumPage()
var out []pdfRow
for pageNum := 1; pageNum <= numPages; pageNum++ {
page := reader.Page(pageNum)
if page.V.IsNull() {
continue
}
content := page.Content()
// Group letters by (approximate) baseline Y. Values within 2pt are
// considered the same row — PDFs frequently jitter Y by a fraction.
type rowBucket struct {
y float64
maxFS float64
chars []pdflib.Text
}
var buckets []*rowBucket
find := func(y float64) *rowBucket {
for _, b := range buckets {
if abs(b.y-y) < 2.0 {
return b
}
}
b := &rowBucket{y: y}
buckets = append(buckets, b)
return b
}
for _, t := range content.Text {
b := find(t.Y)
b.chars = append(b.chars, t)
if t.FontSize > b.maxFS {
b.maxFS = t.FontSize
}
}
// Sort rows top-to-bottom (higher Y = higher on page in PDF).
sort.Slice(buckets, func(i, j int) bool { return buckets[i].y > buckets[j].y })
for _, b := range buckets {
sort.Slice(b.chars, func(i, j int) bool { return b.chars[i].X < b.chars[j].X })
var sb strings.Builder
var lastX float64
for i, ch := range b.chars {
// Insert a space if there's a visible gap between glyphs.
if i > 0 && ch.X-lastX > ch.FontSize*0.3 {
sb.WriteString(" ")
}
sb.WriteString(ch.S)
lastX = ch.X + ch.W
}
text := strings.TrimSpace(sb.String())
if text == "" {
continue
}
out = append(out, pdfRow{
page: pageNum,
fontSize: b.maxFS,
text: text,
})
}
}
return out, nil
}
// buildHeadingLevelMap returns a map from rounded-font-size → heading level
// (1 = largest = h1). Only sizes above headingFloor are considered.
// Levels are capped at 6.
func buildHeadingLevelMap(rows []pdfRow, floor float64) map[int]int {
seen := map[int]bool{}
for _, r := range rows {
if r.fontSize > floor {
seen[roundSize(r.fontSize)] = true
}
}
var bigs []int
for k := range seen {
bigs = append(bigs, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(bigs)))
out := make(map[int]int, len(bigs))
for i, sz := range bigs {
lvl := i + 1
if lvl > 6 {
lvl = 6
}
out[sz] = lvl
}
return out
}
// roundSize rounds a font size to the nearest 0.5pt, expressed as an int
// (×2) so it can key a map. Two glyphs with nominally the same font size
// often jitter by a fraction of a point.
func roundSize(s float64) int {
return int(s*2 + 0.5)
}
// parsePDFWithOutline builds a ParsedDoc using the PDF's /Outlines as
// the structural ground truth. For each outline entry (depth-first,
// pre-order) we scan forward through rows starting at the last match
// position and treat the first matching row as that heading. Content
// between one outline match and the next is the preceding heading's
// body.
//
// Returns ok=false if we can't match enough outline entries to rows —
// in which case the caller falls back to the font-size heuristic.
func parsePDFWithOutline(outline pdflib.Outline, rows []pdfRow) (*ParsedDoc, bool) {
// Flatten outline to (level, title) pairs via depth-first walk.
type entry struct {
level int
title string
}
var flat []entry
var walk func(nodes []pdflib.Outline, depth int)
walk = func(nodes []pdflib.Outline, depth int) {
lvl := depth + 1
if lvl > 6 {
lvl = 6
}
for _, n := range nodes {
t := strings.TrimSpace(n.Title)
if t != "" {
flat = append(flat, entry{level: lvl, title: t})
}
walk(n.Child, depth+1)
}
}
walk(outline.Child, 0)
if len(flat) == 0 {
return nil, false
}
// Match each outline title to the first row at or after the cursor
// whose normalized text begins with the normalized title. This is
// forgiving of trailing page numbers, section numbering prefixes the
// outline sometimes omits, etc.
type matched struct {
level int
title string
rowIdx int // index into rows where this heading starts
}
var chosen []matched
cursor := 0
for _, e := range flat {
want := normalizeForMatch(e.title)
found := -1
for i := cursor; i < len(rows); i++ {
if strings.HasPrefix(normalizeForMatch(rows[i].text), want) {
found = i
break
}
}
if found < 0 {
continue
}
chosen = append(chosen, matched{level: e.level, title: e.title, rowIdx: found})
cursor = found + 1
}
// Require at least half the outline to match, otherwise the outline
// likely doesn't describe the text we extracted (encrypted fonts,
// weird glyph mappings) and we should fall back.
if len(chosen)*2 < len(flat) {
return nil, false
}
// Assemble sections: body text is the concatenation of rows between
// one match and the next (exclusive).
rootSec := &Section{Level: 0}
stack := []*Section{rootSec}
for i, m := range chosen {
end := len(rows)
if i+1 < len(chosen) {
end = chosen[i+1].rowIdx
}
var body strings.Builder
for _, row := range rows[m.rowIdx+1 : end] {
text := strings.TrimSpace(row.text)
if text == "" {
continue
}
if body.Len() > 0 {
body.WriteByte(' ')
}
body.WriteString(text)
}
sec := Section{Level: m.level, Title: m.title, Content: body.String()}
for len(stack) > 1 && stack[len(stack)-1].Level >= sec.Level {
stack = stack[:len(stack)-1]
}
parent := stack[len(stack)-1]
parent.Children = append(parent.Children, sec)
tail := &parent.Children[len(parent.Children)-1]
stack = append(stack, tail)
}
title := ""
if len(rootSec.Children) > 0 {
title = rootSec.Children[0].Title
}
return &ParsedDoc{
Title: title,
Sections: rootSec.Children,
}, true
}
// normalizeForMatch lowercases, strips punctuation, and collapses
// whitespace so outline titles match row text despite cosmetic drift.
func normalizeForMatch(s string) string {
var b strings.Builder
prevSpace := false
for _, r := range strings.ToLower(s) {
switch {
case r >= 'a' && r <= 'z', r >= '0' && r <= '9':
b.WriteRune(r)
prevSpace = false
case r == ' ' || r == '\t' || r == '\n':
if !prevSpace && b.Len() > 0 {
b.WriteByte(' ')
prevSpace = true
}
}
}
return strings.TrimSpace(b.String())
}
func looksLikeHeading(s string) bool {
// Headings are rarely > 14 words and never end with sentence punctuation
// from the middle of a paragraph.
words := strings.Fields(s)
if len(words) == 0 || len(words) > 14 {
return false
}
// Common body-text tells: trailing comma, trailing ellipsis.
if strings.HasSuffix(s, ",") {
return false
}
return true
}
func abs(f float64) float64 {
if f < 0 {
return -f
}
return f
}