Convert to PDF/UA - PHP Sample Code

Requirements
View Demo

Sample code for using Apryse SDK to programmatically convert generic PDF documents into ISO-compliant, VeraPDF-valid PDF/UA files. Supports PDF/UA-1. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Implementation steps

To convert to PDF/UA with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework
Step 2: Download the Data Extraction Module
Step 3: Add the sample code provided in this guide

To use this feature in production, your license key will need the Accessibility Package. Trial keys already include all packages.

Learn more about our Server SDK and PDF/UA Library.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2024 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// The following sample illustrates how to make sure a file meets the PDF/UA standard, using the PDFUAConformance class object.
11// Note: this feature is currently experimental and subject to change
12//
13// DataExtractionModule is required (Mac users can use StructuredOutputModule instead)
14// https://docs.apryse.com/documentation/core/info/modules/#data-extraction-module
15// https://docs.apryse.com/documentation/core/info/modules/#structured-output-module (Mac)
16//---------------------------------------------------------------------------------------
17function main()
18{
19 // Relative path to the folder containing the test files.
20 $input_path = "../../TestFiles/";
21 $output_path = "../../TestFiles/Output/";
22 // DataExtraction library location, replace if desired, should point to a folder that includes the contents of <DataExtractionModuleRoot>/Lib.
23 // If using default, unzip the DataExtraction zip to the parent folder of Samples, and merge with existing "Lib" folder.
24 $extraction_module_path = "../../../PDFNetC/Lib/";
25 $input_file1 = $input_path."autotag_input.pdf";
26 $input_file2 = $input_path."table.pdf";
27 $output_file1 = $output_path."autotag_pdfua.pdf";
28 $output_file2 = $output_path."table_pdfua_linearized.pdf";
29 global $LicenseKey;
30 PDFNet::Initialize($LicenseKey);
31 PDFNet::GetSystemFontList();
32 echo(nl2br("AutoConverting...\n"));
33 PDFNet::AddResourceSearchPath($extraction_module_path);
34 if (!DataExtractionModule::IsModuleAvailable(DataExtractionModule::e_DocStructure)) {
35 echo(nl2br("\n"));
36 echo(nl2br("Unable to run Data Extraction: PDFTron SDK Structured Output module not available.\n"));
37 echo(nl2br("-----------------------------------------------------------------------------\n"));
38 echo(nl2br("The Data Extraction suite is an optional add-on, available for download\n"));
39 echo(nl2br("at https://docs.apryse.com/documentation/core/info/modules/. If you have already\n"));
40 echo(nl2br("downloaded this module, ensure that the SDK is able to find the required files\n"));
41 echo(nl2br("using the PDFNet::AddResourceSearchPath() function.\n"));
42 echo(nl2br("\n"));
43 PDFNet::Terminate();
44 return;
45 }
46 try {
47 $pdf_ua = new PDFUAConformance();
48 echo(nl2br("Simple Conversion...\n"));
49 // Perform conversion using default options
50 $pdf_ua->AutoConvert($input_file1, $output_file1);
51 echo(nl2br("Converting With Options...\n"));
52 $pdf_ua_opts = new PDFUAOptions();
53 $pdf_ua_opts->SetSaveLinearized(true); // Linearize when saving output
54 // Note: if file is password protected, you can use $pdf_ua_opts->SetPassword()
55 // Perform conversion using the options we specify
56 $pdf_ua->AutoConvert($input_file2, $output_file2, $pdf_ua_opts);
57 }
58 catch(Exception $e) {
59 echo(nl2br($e->getMessage()));
60 }
61 PDFNet::Terminate();
62 echo(nl2br("PDFUAConformance test completed.\n"));
63}
64main();
65?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales