Convert
Rewrite a PDF into PDF/A, check what could not be fixed, verify the pages still look right, and stream the output.
Convert applies fixups, then repeats verify-and-fix until the document converges or Options.MaxIterations is reached. Pages that can't be repaired in place are rasterized as a last resort. A result is only reported valid if an independent verification says so.
cr, err := gopdfrab.Convert(path, gopdfrab.PDFA1B)
if err != nil {
log.Fatal(err)
}
defer cr.Close() // releases the output (a large one spills to a temp file)
if err := cr.Save("out.pdf"); err != nil {
log.Fatal(err)
}
fmt.Println(cr.Iterations) // how many verify/fixup passes it took
fmt.Println(cr.Result.Valid) // true if the output is fully PDF/A conformant It also works on an open document, on bytes and on a batch. Always check Residual(): anything that couldn't be fixed, such as an unembeddable font, is listed there.
// Converting an open document
cr, err := doc.Convert(gopdfrab.PDFA1B)
defer cr.Close()
// Converting in-memory data
cr, err = gopdfrab.ConvertBytes(data, gopdfrab.PDFA1B)
// Converting multiple files concurrently
results, err := gopdfrab.ConvertAll(paths, gopdfrab.PDFA1B)
if err != nil {
log.Fatal(err)
}
for _, r := range results {
if r.Err != nil {
log.Println(r.Path, r.Err)
continue
}
fmt.Println(r.Path, r.Result.Result.Valid) // r.Result is a ConvertResult
}
// Inspecting residual issues after conversion
residual := cr.Residual()
for _, iss := range residual {
c := iss.Check()
fmt.Println(c.Clause(), c.Name())
fmt.Println(iss.Page(), iss.Messages())
}Step by step
- Open or name the document Call Convert with a path, or ConvertDocument with a document you already opened. Both take the target profile, PDFA1B or PDFA2B.
- Let the verify/fix loop run Conversion applies pre-emptive fixups, then re-verifies and fixes repeatedly until the document converges or Options.MaxIterations is reached. Pages no in-place fixer can repair are rasterized as a last resort.
- Check the residual issues Read Residual() before treating the conversion as complete. Anything that could not be repaired — an unembeddable font, an object lost during damage recovery — is reported there rather than hidden.
- Confirm the appearance survived A page blanked during conversion still verifies clean, so run the fidelity check when visual equivalence matters. It renders both sides and reports a per-page comparison.
- Write the output and close the result Save writes to a file and WriteTo streams to any io.Writer. The output spills to a temp file when it is large, so always Close() the result.
Fidelity
Conforming isn't the same as looking the same. A blanked page still verifies clean. Options.CheckFidelity renders input and output with the same rasterizer and reports a per-page comparison. Blanked() flags real content loss. Anything the rasterizer couldn't draw on a rasterized page is listed in RasterDrops. Fidelity checking is off by default because it renders both documents.
// Conforming to PDF/A is not the same as *looking* like the input -- a page
// blanked during conversion still verifies clean.
cr, err := gopdfrab.ConvertContext(ctx, path, gopdfrab.PDFA1B,
gopdfrab.Options{CheckFidelity: true})
defer cr.Close()
for _, pf := range cr.Fidelity {
if pf.Blanked() {
log.Printf("page %d lost its content during conversion", pf.Page)
}
}
// Content the rasterizer could not draw is reported, never silently omitted
for _, d := range cr.RasterDrops {
log.Printf("page %d: dropped %v", d.Page, d.Features)
}
cr.RasterizedPages // pages rebuilt as a flat imageWriting the output
Save writes to a file, WriteTo streams to any io.Writer and Output() returns the bytes. Large outputs spill to a temp file, so Close() the result.
// Save writes to a file; WriteTo streams to any io.Writer -- both without
// holding a second copy. Output returns the bytes when you need them in memory.
err := cr.Save("out.pdf")
_, err = cr.WriteTo(w) // e.g. an http.ResponseWriter or a bytes.Buffer
b, err := cr.Output()
// For a batch too large to hold every output at once, ConvertEach streams:
// it calls the callback on each result as it completes and closes it for you.
err = gopdfrab.ConvertEach(paths, gopdfrab.PDFA1B, gopdfrab.Options{Workers: 4},
func(r gopdfrab.FileResult[gopdfrab.ConvertResult]) error {
if r.Err != nil {
return nil // skip this file, keep going
}
return r.Result.Save(filepath.Join(outDir, filepath.Base(r.Path)))
}) For batches too large to hold in memory, ConvertEach calls your callback per result in completion order. Calls are serialized, so the callback needn't be concurrency-safe. Each result is closed for you. Options.Workers bounds concurrency.