Go CLI that turns the markdown currency of this stack (Redmine notes, Discourse posts, briefing output) into typeset PDFs: two embedded typst templates (report with title page/TOC/headers, dense brief), front-matter (title/subtitle/author/date/classification/template), GFM tables, and bar charts rendered in pure Go (go-chart) from fenced chart data blocks. Engine is a prebuilt typst 0.15.1 container pinned by digest and run --network none; PDF bytes to stdout or -o. Exit codes 0/1/2. All dev in docker (dev.sh/Makefile); unit tests + golden typst fixtures plus a host-side smoke against the real engine container. Generated with Crush Assisted-by: Crush:glm-5.2
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
// Package pdfinfo extracts basic facts from PDF bytes with a tiny parser —
|
|
// no third-party PDF library, just enough to count pages in golden tests
|
|
// and smoke checks.
|
|
package pdfinfo
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
)
|
|
|
|
// Pages returns the page count of a PDF by scanning for the /Type /Pages
|
|
// objects' /Count entries (the catalog root carries the total; we take the
|
|
// maximum to be safe with nested page trees).
|
|
func Pages(pdf []byte) (int, error) {
|
|
if len(pdf) == 0 || !bytes.HasPrefix(pdf, []byte("%PDF-")) {
|
|
return 0, fmt.Errorf("pdfinfo: not a PDF (missing %%%%PDF- header)")
|
|
}
|
|
re := regexp.MustCompile(`/Type\s*/Pages[^>]*?/Count\s+(\d+)`)
|
|
best := 0
|
|
for _, m := range re.FindAllStringSubmatch(string(pdf), -1) {
|
|
if n, err := strconv.Atoi(m[1]); err == nil && n > best {
|
|
best = n
|
|
}
|
|
}
|
|
if best == 0 {
|
|
// Compressed object streams can hide the count; require at least
|
|
// the header then fall back to /Type /Page occurrences (\b keeps
|
|
// /Pages from matching).
|
|
pageRe := regexp.MustCompile(`/Type\s*/Page\b`)
|
|
best = len(pageRe.FindAll(pdf, -1))
|
|
if best == 0 {
|
|
return 0, fmt.Errorf("pdfinfo: no page count found")
|
|
}
|
|
}
|
|
return best, nil
|
|
}
|