Typed errors and damage recovery
Open, verify and convert failures can be matched with errors.Is instead of inspecting message text.
| Error | Meaning |
|---|---|
| ErrNotPDF | the input is not a PDF (no %PDF- header) |
| ErrDamaged | a PDF whose cross-reference or trailer structure could not be parsed |
| ErrEncrypted | an encryption scheme gopdfrab does not implement |
| ErrPasswordRequired | a correct password is required to open the file |
| ErrUnresolvableGraph | Convert could not resolve the object graph, so no output was produced |
res, err := gopdfrab.Verify(path, gopdfrab.PDFA1B)
switch {
case errors.Is(err, gopdfrab.ErrNotPDF):
log.Println("not a PDF -- no %PDF- header")
case errors.Is(err, gopdfrab.ErrPasswordRequired):
log.Println("a correct password is required")
case errors.Is(err, gopdfrab.ErrEncrypted):
log.Println("unsupported encryption scheme")
case errors.Is(err, gopdfrab.ErrDamaged):
log.Println("xref or trailer structure could not be parsed")
case err != nil:
log.Println(err)
default:
fmt.Println(res.Valid)
}Damage is reported, not fatal
An individual object that fails to parse — a wrong cross-reference offset, a corrupt body — does not fail the whole document. The object is re-located by scanning for its real N G obj header, or resolved to null when no intact copy exists; either way the damage is reported as a 6.1.6 issue and every other check still runs.
The same applies to whole-table damage: a missing or unusable startxref triggers a full-file object scan that rebuilds the cross-reference table and recovers the trailer from the document catalog, reported as a 6.1.4 issue rather than a hard error. A badly damaged file still verifies and converts — and a conversion that had to null an unrecoverable object keeps that loss in Residual() and never reports the result as valid.
The design rule is that a corrupt PDF should tell you what is wrong with it. Returning an error and nothing else is the least useful possible answer when the thing you are trying to establish is why a file is broken.