Convert to PDF/A

Sample code for using Apryse Server SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/A files, or to validate PDF/A compliance. Supports all three PDF/A parts (PDF/A-1, PDF/A-2, PDF/A-3), and covers all conformance levels (A, B, U). Code available in Learn more about our Server SDK and PDF/A Library. A command-line tool for batch conversion and validation is also available.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample illustrates how to parse and check if a PDF document meets the
14# PDFA standard, using the PDFACompliance class object.
15#---------------------------------------------------------------------------------------
16
17# Relative path to the folder containing the test files.
18input_path = "../../TestFiles/"
19output_path = "../../TestFiles/Output/"
20
21def PrintResults(pdf_a, filename)
22 err_cnt = pdf_a.GetErrorCount
23 if err_cnt == 0
24 puts filename + ": OK."
25 else
26 puts filename + " is NOT a valid PDFA."
27 i = 0
28 while i < err_cnt do
29 c = pdf_a.GetError(i)
30 str1 = " - e_PDFA " + c.to_s + ": " + PDFACompliance.GetPDFAErrorMessage(c) + "."
31 if true
32 num_refs = pdf_a.GetRefObjCount(c)
33 if num_refs > 0
34 str1 = str1 + "\n Objects: "
35 j = 0
36 while j < num_refs do
37 str1 = str1 + pdf_a.GetRefObj(c, j).to_s
38 if j < num_refs-1
39 str1 = str1 + ", "
40 end
41 j = j + 1
42 end
43 end
44 end
45 puts str1
46 i = i + 1
47 end
48 puts "\n"
49 end
50end
51
52 PDFNet.Initialize(PDFTronLicense.Key)
53 PDFNet.SetColorManagement # Enable color management (required for PDFA validation).
54
55 #-----------------------------------------------------------
56 # Example 1: PDF/A Validation
57 #-----------------------------------------------------------
58 filename = "newsletter.pdf"
59 # The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
60 # of object numbers that are collected for particular error codes. The default value is 10
61 # in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs.
62 pdf_a = PDFACompliance.new(false, input_path+filename, nil, PDFACompliance::E_Level2B, 0, 0, 10)
63 PrintResults(pdf_a, filename)
64 pdf_a.Destroy
65
66 #-----------------------------------------------------------
67 # Example 2: PDF/A Conversion
68 #-----------------------------------------------------------
69 filename = "fish.pdf"
70 pdf_a = PDFACompliance.new(true, input_path + filename, nil, PDFACompliance::E_Level2B, 0, 0, 10)
71 filename = "pdfa.pdf"
72 pdf_a.SaveAs(output_path + filename, false)
73 pdf_a.Destroy
74
75 # Re-validate the document after the conversion...
76 pdf_a = PDFACompliance.new(false, output_path + filename, nil, PDFACompliance::E_Level2B, 0, 0, 10)
77 PrintResults(pdf_a, filename)
78 pdf_a.Destroy
79 PDFNet.Terminate
80 puts "PDFACompliance test completed."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales