PDF/A and PDF/UA validation
Validate PDF/A (every level from 1a to 4f) and PDF/UA-1 or PDF/UA-2 with clause ids, and convert to a chosen PDF/A or PDF/UA level, with NexPDF for Go.
License module
Validation and conversion need the standards module (Enterprise tier).
NexPDF checks documents against the machine-checkable rules of PDF/A (ISO 19005-1 to -4) and PDF/UA (ISO 14289). Each failure names the clause of the standard that requires it, with the same numbers the veraPDF rule sets use.
Validate PDF/A
ValidatePDFAJSON(level) takes 1a, 1b, 2a, 2b, 2u, 3a, 3b, 3u, 4, 4e or 4f
(or PDF/A-2b). "" uses the level the document declares, or 2b.
type Conformance struct {
Compliant bool `json:"compliant"`
Level string `json:"level"`
Failures []struct {
Clause string `json:"clause"`
Message string `json:"message"`
Page *int `json:"page"`
Object *string `json:"object"`
} `json:"failures"`
}
doc, err := nexpdf.Open("archive.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
s, err := doc.ValidatePDFAJSON("2b")
if err != nil {
log.Fatal(err)
}
var report Conformance
json.Unmarshal([]byte(s), &report)
fmt.Println(report.Level, report.Compliant)
for _, f := range report.Failures {
fmt.Println(f.Clause, f.Message)
}Each failure has clause (such as 6.2.11.4.1), message, page (1-based, or null) and
object (such as 12 0 R, or null).
The rules cover file structure, metadata (XMP, pdfaid, extension schemas), fonts (embedding,
glyphs, widths, encodings), colour (output intents, ICC profiles), graphics and transparency,
interactive features (annotations, actions, forms, embedded files) and, for level A, the tagged
structure.
Validate PDF/UA
ValidatePdfuaJSON() validates against the PDF/UA part the document declares, PDF/UA-1 by
default. The report has the same shape, with level PDF/UA-1 or PDF/UA-2.
s, err := doc.ValidatePdfuaJSON()
if err != nil {
log.Fatal(err)
}
var ua Conformance
json.Unmarshal([]byte(s), &ua)
fmt.Println(ua.Level, ua.Compliant, len(ua.Failures))PDF/UA-1 follows the Matterhorn Protocol. PDF/UA-2 validation is complete: PDF 2.0 namespaces
(/NS, /RoleMapNS, the root /RoleMap, MathML), the ISO 14289-2 structure, annotation, form,
destination and text-string rules, and the ISO/TS 32005 parent and child rules.
ValidatePdfuaPartJSON(part) validates against PDF/UA-1 (1) or PDF/UA-2 (2) whatever the
document declares, with the same report:
s, err = doc.ValidatePdfuaPartJSON(2)Convert to a level
ConvertPDFALevel(level) converts to the level you choose ("" is 2b). Level A tags the document
first. When the result validates against the level it returns 1. When some clauses still fail,
the fixes that could be made are kept and the engine reports the clauses that are left as the
error, so the call returns 0 and a *nexpdf.Error with that list:
doc, err := nexpdf.Open("report.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
if _, err := doc.ConvertPDFALevel("3a"); err != nil {
fmt.Println("still failing:", err) // the document keeps the fixes that worked
}
doc.Save("report-pdfa3a.pdf", false)Convert with a report
ConvertPDFAJSON(level, flattenDpi, substituteFonts) converts like ConvertPDFALevel but does
not fail when clauses remain: it reports them. flattenDpi is the resolution PDF/A-1
transparency is flattened at (0 = 300); substituteFonts 0 leaves non-embedded fonts that are
not standard fonts alone.
type PdfaConversion struct {
Level string `json:"level"`
Compliant bool `json:"compliant"`
Fixed []string `json:"fixed"`
Substitutions []struct {
Original string `json:"original"`
Substitute string `json:"substitute"`
Exact bool `json:"exact"`
WidthsAdjusted bool `json:"widths_adjusted"`
} `json:"substitutions"`
Remaining []struct {
Clause string `json:"clause"`
Message string `json:"message"`
Page *int `json:"page"`
Object *string `json:"object"`
} `json:"remaining"`
}
font, _ := os.ReadFile("Arimo-Regular.ttf")
if _, err := nexpdf.RegisterSubstituteFont(font); err != nil { // returns the PostScript name
log.Fatal(err)
}
s, err := doc.ConvertPDFAJSON("2b", 300, 1)
if err != nil {
log.Fatal(err)
}
var conv PdfaConversion
json.Unmarshal([]byte(s), &conv)
fmt.Println(conv.Compliant, len(conv.Remaining))The conversion flattens PDF/A-1 transparency to opaque image patches (text stays text), fixes device colour (CMYK output intent, Default colour spaces), draws missing appearance streams for widgets and markup annotations, substitutes metric-compatible fonts for non-embedded fonts and prunes unreachable objects. For PDF/A-4e it also handles U3D/PRC 3D streams and 3D and RichMedia annotations.
RegisterSubstituteFont(data) registers TrueType (glyf) font data for PDF/A and PDF/UA
substitution: non-embedded fonts with the same PostScript or family name use it.
ConvertPdfuaJSON(part, lang) converts to PDF/UA-1 or PDF/UA-2: it tags an untagged document,
embeds fonts and, for part 2, uses the PDF 2.0 namespace, structure destinations and the
annotation rules. lang ("" = en-US) applies when the document has none. It returns
{"profile","compliant","fixed","warnings","substitutions","remaining"}.
s, err = doc.ConvertPdfuaJSON(2, "en-US")OCR text layers use glyph-less fonts, so convert to PDF/A after OCR, not before.
See PDF/A, PDF/X and PDF/UA for PDF/X and accessibility tagging.