Streaming output and batch conversion
Save writes the output to a file and WriteTo streams it to any io.Writer — both without holding a second copy. Output() returns the bytes when you need them in memory. All three error when there is no output. A large output spills to a temp file rather than staying resident, so call Close() when done.
// 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 a batch too large to hold every output in memory at once, ConvertEach streams instead: it calls a callback on each result as it completes, serialized and in completion order, and closes each result for you. Returning a non-nil error from the callback stops the batch. Options.Workers bounds the concurrency of both batch forms.
Serialized callbacks are a deliberate choice: it means your callback does not have to be safe for concurrent use, which is the mistake most easily made when writing results to a shared destination. The work still happens in parallel — only the delivery is ordered.