Convert to PDF/A, validate it, and repair what fails
Convert a PDF to PDF/A-1b, PDF/A-2b, or PDF/A-3b in Rust
use pdfluent::{PdfAProfile, PdfDocument};
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("report.pdf")?;
// A new document. The source is not modified.
let archived = doc.convert_to_pdfa(PdfAProfile::A2b)?;
let report = archived.validate_pdfa(PdfAProfile::A2b)?;
println!("compliant: {}", report.is_compliant());
archived.save("report_pdfa2b.pdf")?;
Ok(())
}- Step 1
Open the source PDF
Open the document you want to archive. Conversion reads it and builds a second document, so a shared reference is enough.
rustuse pdfluent::PdfDocument; let doc = PdfDocument::open("report.pdf")?; - Step 2
Choose the conformance level
PdfAProfile has three variants: A1b, A2b, and A3b. A2b is the usual choice; pick A1b only when a policy demands the older, stricter profile, and A3b when the archive has to carry attachments.
rustuse pdfluent::PdfAProfile; let profile = PdfAProfile::A2b; - Step 3
Run the conversion
convert_to_pdfa runs the whole pipeline in one call: font embedding and repair, colour normalisation with an output intent, removal of JavaScript and other forbidden constructs, and the XMP identification block. Damaged input is repaired first where that is possible.
rustlet archived = doc.convert_to_pdfa(profile)?; - Step 4
Validate what came out
Conversion is best effort, so check the result rather than assume it. Some documents cannot be made conformant without changing how they look, and an archival converter must not do that quietly.
rustlet report = archived.validate_pdfa(profile)?; for violation in &report.violations { eprintln!("[{}] {}", violation.rule, violation.message); } - Step 5
Save the converted document
Write the archival copy to its own path. The original file on disk is untouched.
rustarchived.save("report_pdfa2b.pdf")?;
- PDF/A-1b does not allow transparency. If your pages use it, PDF/A-2b or PDF/A-3b is the honest target; forcing A1b flattens artwork and changes how the page looks.
- The output intent -- the ICC profile that says which colour space the page was made for -- is written by the conversion. PDFluent 1.0 has no separate API for setting one on an existing document.
- JavaScript actions, launch actions and embedded movies are removed, because PDF/A forbids them.
- Fonts that may not be embedded under their licence cannot be embedded here either. Those documents come back with violations that no converter can clear; replacing the font is the only route.
- What is the difference between PDF/A-1b, PDF/A-2b, and PDF/A-3b?
- PDF/A-1b is the strictest: no transparency, no layers, no JPEG 2000. PDF/A-2b allows all three. PDF/A-3b additionally allows arbitrary attachments, such as the XML invoice alongside its human-readable rendering.
- Can every PDF be converted to PDF/A?
- No. A font that may not be embedded under its licence, or content that cannot be decrypted, leaves a document that will not validate however it is converted. That is why the example above validates the result instead of trusting it.
- Does the conversion change the source document?
- No. convert_to_pdfa returns a new document and leaves the one you opened alone, in memory and on disk.
- Should I validate after converting?
- Yes. Call validate_pdfa on the converted document and check is_compliant before you archive it.
Validate PDF/A compliance in Rust
use pdfluent::{PdfAProfile, PdfDocument};
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("archive_candidate.pdf")?;
let report = doc.validate_pdfa(PdfAProfile::A2b)?;
println!("compliant: {}", report.is_compliant());
for violation in &report.violations {
println!("[{}] {}", violation.rule, violation.message);
}
Ok(())
}- Step 1
Open the PDF
Validation reads the whole structure, so a large file costs more than a small one. No mutable access is needed.
rustuse pdfluent::PdfDocument; let doc = PdfDocument::open("archive_candidate.pdf")?; - Step 2
Choose the level to validate against
The profile you pass is the profile the report is about. Validating an A1b document against A2b tells you nothing about the claim it makes.
rustuse pdfluent::PdfAProfile; let profile = PdfAProfile::A2b; - Step 3
Run the validator
validate_pdfa returns a PdfAValidationReport. The Err case means the document could not be read at all, not that it failed validation -- a non-conformant document is a successful run with findings in it.
rustlet report = doc.validate_pdfa(profile)?; - Step 4
Read the findings
is_compliant is true when nothing of Severity::Error was found. Warnings are reported as well and do not block conformance, so count the two apart if your pipeline treats them differently.
rustuse pdfluent::compliance::Severity; let errors = report .violations .iter() .filter(|violation| violation.severity == Severity::Error) .count(); println!("{} error(s) out of {} finding(s)", errors, report.violations.len()); println!("compliant: {}", report.is_compliant()); - Step 5
Fail the build on a violation
In a pipeline, turn a non-conformant document into a non-zero exit code so the run stops instead of archiving it.
rustif !report.is_compliant() { eprintln!("{} finding(s)", report.violations.len()); std::process::exit(1); }
- Every finding carries `rule`, the identifier from the ISO 19005 clause it comes from, so a report can be looked up rather than guessed at.
- A missing font embedding is the most common PDF/A-1b failure by a wide margin.
- The validator reads structure and metadata. It does not re-render pages, so it cannot tell you that a page renders differently in another viewer.
- `report.profile` records which profile the report was made for. Keep it with the report if you store findings; a list of violations without its level is unreadable later.
- What is the most common PDF/A-1b violation?
- Missing font embedding. Every font used has to be fully embedded; partial embedding, or leaning on a font installed on the machine, is not allowed.
- Does an Err from validate_pdfa mean the document is not conformant?
- No. Err means the document could not be validated at all -- it could not be read. A document that fails PDF/A returns Ok with a report whose is_compliant is false.
- Does validation check XMP metadata?
- Yes. PDF/A requires an XMP stream with the pdfaid part and conformance fields, and a document missing them is reported like any other violation.
- Is a warning a failure?
- No. is_compliant looks only at findings of Severity::Error. Warnings are reported so you can see them, and they do not block conformance.
Find out which PDF/A level a document actually meets
use pdfluent::{PdfAProfile, PdfDocument};
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("archive.pdf")?;
for profile in [PdfAProfile::A1b, PdfAProfile::A2b, PdfAProfile::A3b] {
let report = doc.validate_pdfa(profile)?;
println!("{:?}: {}", profile, report.is_compliant());
}
Ok(())
}- Step 1
Open the document
Read-only access is enough; nothing here writes.
rustuse pdfluent::PdfDocument; let doc = PdfDocument::open("archive.pdf")?; - Step 2
List the levels, strictest first
PDF/A-1b is the narrowest of the three and PDF/A-3b the widest, so this order lets the first pass be the answer.
rustuse pdfluent::PdfAProfile; let levels = [PdfAProfile::A1b, PdfAProfile::A2b, PdfAProfile::A3b]; - Step 3
Take the strictest level that passes
Stop at the first profile the document conforms to. None means it meets none of the three, which is the useful answer when a document claims otherwise.
rustlet mut strictest = None; for profile in levels { if doc.validate_pdfa(profile)?.is_compliant() { strictest = Some(profile); break; } } println!("{:?}", strictest); - Step 4
Print what stands in the way
When a level fails, the report says why. The rule identifier is the clause of ISO 19005 that was broken, so it can be looked up.
rustlet report = doc.validate_pdfa(PdfAProfile::A1b)?; for violation in &report.violations { println!( "[{}] {} ({:?})", violation.rule, violation.message, violation.severity ); } - Step 5
Keep the level with the report
A stored list of findings is unreadable without the profile it was measured against, and the report carries it.
rustprintln!("checked against {:?}", report.profile);
- Metadata is a claim, not a verdict. A document with a pdfaid block saying PDF/A-1 can still break the rules of PDF/A-1, which is exactly the case this section is for.
- Transparency is forbidden in PDF/A-1 and allowed from PDF/A-2 on, so a document that fails only A1b is usually a transparency case.
- PDFluent validates the b (visual) levels: A1b, A2b, A3b. The a levels, which additionally require tagged structure and reading order, are not among the profiles you can pass.
- Validating against all three costs three passes over the document. Validate against the single level your policy asks for when you already know which one that is.
- What is the difference between PDF/A-1a and PDF/A-1b?
- PDF/A-1b is about visual reproducibility: fonts and colour. PDF/A-1a adds logical structure -- tagging, reading order, alternate text. PDFluent validates and converts the b levels.
- Why validate against every level instead of reading the metadata?
- Because the metadata says what the producer intended and the validator says what the file is. When those two disagree, the file is what matters.
- Can a PDF/A-2b document be digitally signed?
- Yes. PDF/A-2 and PDF/A-3 allow signatures, and a signature does not by itself break conformance.
- Is PDFluent's verdict the same as veraPDF's?
- They implement the same ISO 19005 rules, and our own PDF/A gate measures our output against veraPDF. For a regulated archive, running both is still the safer habit.
Repair a PDF that fails PDF/A validation
use pdfluent::{PdfAProfile, PdfDocument};
fn main() -> pdfluent::Result<()> {
let doc = PdfDocument::open("noncompliant.pdf")?;
let before = doc.validate_pdfa(PdfAProfile::A2b)?;
println!("before: {} finding(s)", before.violations.len());
// Conversion is the repair; there is no fix-in-place call.
let repaired = doc.convert_to_pdfa(PdfAProfile::A2b)?;
let after = repaired.validate_pdfa(PdfAProfile::A2b)?;
println!("after: {} finding(s)", after.violations.len());
if after.is_compliant() {
repaired.save("repaired_a2b.pdf")?;
}
Ok(())
}- Step 1
Measure before you repair
Validate first, so that afterwards you can say what changed rather than hope something did.
rustuse pdfluent::{PdfAProfile, PdfDocument}; let doc = PdfDocument::open("noncompliant.pdf")?; let before = doc.validate_pdfa(PdfAProfile::A2b)?; - Step 2
Convert to the level you need
The conversion pipeline is what repairs the document: fonts are embedded, colour is normalised with an output intent, forbidden constructs are removed, and the identification metadata is written.
rustlet repaired = doc.convert_to_pdfa(PdfAProfile::A2b)?; - Step 3
Measure again
The difference between the two counts is the part of the problem that conversion could solve.
rustlet after = repaired.validate_pdfa(PdfAProfile::A2b)?; println!( "{} -> {} finding(s)", before.violations.len(), after.violations.len() ); - Step 4
Look at what is left
Whatever survives conversion needs a decision from a person: a font that may not be embedded has to be replaced, and encrypted content has to be unlocked at the source.
rustfor violation in &after.violations { println!("[{}] {}", violation.rule, violation.message); } - Step 5
Only archive a document that passes
Writing a file that still fails validation puts the problem in the archive instead of in the pipeline.
rustif after.is_compliant() { repaired.save("repaired_a2b.pdf")?; } else { eprintln!("still not conformant -- not writing an archive copy"); }
- Converting to PDF/A-1b flattens transparency, which can change how a page looks. When the source uses transparency, PDF/A-2b keeps the artwork intact.
- A font whose licence forbids embedding stays a violation after conversion. The document has to be re-made with a font that may be embedded.
- Conversion always produces a second document. Nothing is repaired in place, so the file you started from stays available for comparison.
- Best effort includes structural repair: a broken cross-reference table or page tree is rebuilt first where that can be done safely.
- Is there a fix_pdf_a_errors call?
- No. PDFluent 1.0 ships two PDF/A entry points on PdfDocument -- validate_pdfa and convert_to_pdfa -- and conversion is where the repairs happen.
- Can conversion produce a PDF/A-1a (accessible) document?
- No. PDF/A-1a requires tagged structure, reading order and alternate text, which cannot be generated reliably without a person reviewing them. The profiles you can convert to are A1b, A2b and A3b.
- What if a font cannot be embedded?
- It stays in the report after conversion. Replacing the font in the source document is the only way to clear it.
- Does repairing change the layout?
- Embedding fonts and writing metadata do not move anything. Flattening transparency for PDF/A-1b can change blending, which is why A2b is the better target for documents that use it.