Open a PDF document in Go
Open parses the file and builds the structures needed for verification. Always close the document when you're done with it — it may hold a file handle and rebuildable caches.
doc, err := gopdfrab.Open(path)
if err != nil {
log.Fatal(err)
}
defer doc.Close()OpenBytes does the same for a PDF you already hold in memory — an upload, an object-store fetch, a document you just generated — parsing it exactly as Open does but without touching disk. It still needs closing.
doc, err := gopdfrab.OpenBytes(data)
if err != nil {
log.Fatal(err)
}
defer doc.Close() Opening is deliberately tolerant. A cross-reference offset pointing at the wrong byte, a truncated body, a missing startxref — none of these fail the open. The parser recovers what it can and records the damage as a verification issue, so you get a document you can still inspect rather than an error and nothing else. That behaviour is described in full under typed errors.
A file that is not a PDF at all, or that needs a password you haven't supplied, is a genuine error and is reported as one. For password-protected input, see encrypted PDFs.
If you only need a one-shot answer about a file on disk, you don't have to open it yourself: Verify and Convert both accept a path directly and manage the document lifecycle for you.