okami.mom/web-extract-go

git clone https://git.nkp.pet/okami.mom/web-extract-go

nekomimi.petweb-extract: the Go service running on the Mac mini (vassago)5a90548

main
1.4 KiB48 linesraw
1package app
2
3import (
4	"bytes"
5	"fmt"
6	"strings"
7
8	"github.com/ledongthuc/pdf"
9)
10
11// extractPDFText returns the plain text of an in-memory PDF. Extraction runs
12// in-process so the service has no runtime dependency on a pdftotext binary
13// being installed and present on PATH.
14//
15// The underlying library panics on malformed or hostile documents, so the
16// whole extraction is recoverable: a PDF this service cannot parse is
17// reported as a handled error, never as a crash.
18func extractPDFText(b []byte) (text string, err error) {
19	defer func() {
20		if recovered := recover(); recovered != nil {
21			text, err = "", failure(422, "PDF_EXTRACTION_FAILED", "pdf text extraction failed")
22		}
23	}()
24	r, err := pdf.NewReader(bytes.NewReader(b), int64(len(b)))
25	if err != nil {
26		return "", failure(422, "PDF_EXTRACTION_FAILED", "pdf text extraction failed")
27	}
28	var sb strings.Builder
29	total := r.NumPage()
30	for i := 1; i <= total; i++ {
31		page := r.Page(i)
32		// Damage is common in real PDFs; a page that cannot be read is
33		// skipped rather than failing the whole document.
34		if page.V.IsNull() {
35			continue
36		}
37		pageText, err := page.GetPlainText(nil)
38		if err != nil {
39			continue
40		}
41		sb.WriteString(pageText)
42	}
43	text = strings.TrimSpace(sb.String())
44	if text == "" {
45		return "", failure(422, "NO_CONTENT_FOUND", fmt.Sprintf("pdf contains no extractable text (%d pages)", total))
46	}
47	return text, nil
48}