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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//---------------------------------------------------------------------------------------
11// The following sample illustrates how to parse and check if a PDF document meets the
12// PDFA standard, using the PDFACompliance class object.
13//---------------------------------------------------------------------------------------
14
15
16function PrintResults($pdf_a, $filename)
17{
18 $err_cnt = $pdf_a->GetErrorCount();
19 if ($err_cnt == 0)
20 {
21 echo nl2br($filename.": OK.\n");
22 }
23 else
24 {
25 echo nl2br($filename." is NOT a valid PDFA.\n");
26 for ($i=0; $i<$err_cnt; ++$i)
27 {
28 $c = $pdf_a->GetError($i);
29 $str1 = " - e_PDFA ".$c.": ".PDFACompliance::GetPDFAErrorMessage($c).".";
30 if (true)
31 {
32 $num_refs = $pdf_a->GetRefObjCount($c);
33 if ($num_refs > 0)
34 {
35 $str1 = $str1."\n Objects: ";
36 for ($j=0; $j<$num_refs; ++$j)
37 {
38 $str1 = $str1.$pdf_a->GetRefObj($c, $j);
39 if ($j<$num_refs-1)
40 $str1 = $str1. ", ";
41 }
42 }
43 }
44 echo nl2br($str1."\n");
45 }
46 echo nl2br("\n");
47 }
48}
49
50 // Relative path to the folder containing the test files.
51 $input_path = getcwd()."/../../TestFiles/";
52 $output_path = getcwd()."/../../TestFiles/Output/";
53
54 PDFNet::Initialize($LicenseKey);
55 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
56 PDFNet::SetColorManagement(); // Enable color management (required for PDFA validation).
57
58 //-----------------------------------------------------------
59 // Example 1: PDF/A Validation
60 //-----------------------------------------------------------
61 $filename = "newsletter.pdf";
62 // The max_ref_objs parameter to the PDFACompliance constructor controls the maximum number
63 // of object numbers that are collected for particular error codes. The default value is 10
64 // in order to prevent spam. If you need all the object numbers, pass 0 for max_ref_objs.
65 $pdf_a = new PDFACompliance(false, $input_path.$filename, "", PDFACompliance::e_Level2B, 0, 0, 10);
66 PrintResults($pdf_a, $filename);
67 $pdf_a->Destroy();
68
69 //-----------------------------------------------------------
70 // Example 2: PDF/A Conversion
71 //-----------------------------------------------------------
72 $filename = "fish.pdf";
73
74 $pdf_a = new PDFACompliance(true, $input_path.$filename, "", PDFACompliance::e_Level2B, 0, 0, 10);
75 $filename = "pdfa.pdf";
76 $pdf_a->SaveAs($output_path.$filename, false);
77 $pdf_a->Destroy();
78
79 // Re-validate the document after the conversion...
80 $pdf_a = new PDFACompliance(false, $output_path.$filename, "", PDFACompliance::e_Level2B, 0, 0, 10);
81 PrintResults($pdf_a, $filename);
82 $pdf_a->Destroy();
83 PDFNet::Terminate();
84 echo nl2br("PDFACompliance test completed.\n");
85?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales