Sanitize PDFs - PHP Sample Code

This is sample code for using Apryse SDK to remove hidden, non-visual content within PDF documents. Using pdftron.PDF.Sanitizer ensures that if metadata, form data, bookmarks, hidden layers, markup annotations, JavaScript, or file attachments are present in a document, that content is permanently destroyed and is not simply disabled or obscured. Sample code is provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, and VB.

Implementation steps

To sanitize files with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework.
Step 2: Add the sample code provided in this guide.

Learn more about Apryse Server SDK.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
4// Consult legal.txt regarding legal and license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//------------------------------------------------------------------------------
11// PDFNet's Sanitizer is a security-focused feature that permanently removes
12// hidden, sensitive, or potentially unsafe content from a PDF document.
13// While redaction targets visible page content such as text or graphics,
14// sanitization focuses on non-visual elements and embedded structures.
15//
16// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
17// not merely obscured or disabled. This prevents leakage of sensitive
18// data such as authoring details, editing history, private identifiers,
19// and residual form entries, and neutralizes scripts or attachments.
20//
21// Sanitization is recommended prior to external sharing with clients,
22// partners, or regulatory bodies. It helps align with privacy policies
23// and compliance requirements by permanently removing non-visual data.
24//------------------------------------------------------------------------------
25
26 global $LicenseKey;
27 PDFNet::Initialize($LicenseKey);
28 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.
29
30 // Relative paths to folders containing test files.
31 $input_path = getcwd()."/../../TestFiles/";
32 $output_path = $input_path."Output/";
33
34 // The following example illustrates how to retrieve the existing
35 // sanitizable content categories within a document.
36 try
37 {
38 $doc = new PDFDoc($input_path."numbered.pdf");
39 $doc->InitSecurityHandler();
40
41 $opts = Sanitizer::GetSanitizableContent($doc);
42 if ($opts->GetMetadata())
43 {
44 echo(nl2br("Document has metadata.\n"));
45 }
46 if ($opts->GetMarkups())
47 {
48 echo(nl2br("Document has markups.\n"));
49 }
50 if ($opts->GetHiddenLayers())
51 {
52 echo(nl2br("Document has hidden layers.\n"));
53 }
54 echo(nl2br("Done...\n"));
55 }
56 catch(Exception $e)
57 {
58 echo(nl2br($e->getMessage()."\n"));
59 }
60
61 // The following example illustrates how to sanitize a document with default options,
62 // which will remove all sanitizable content present within a document.
63 try
64 {
65 $doc = new PDFDoc($input_path."financial.pdf");
66 $doc->InitSecurityHandler();
67
68 Sanitizer::SanitizeDocument($doc, null);
69 $doc->Save($output_path."financial_sanitized.pdf", SDFDoc::e_linearized);
70 echo(nl2br("Done...\n"));
71 }
72 catch(Exception $e)
73 {
74 echo(nl2br($e->getMessage()."\n"));
75 }
76
77 // The following example illustrates how to sanitize a document with custom set options,
78 // which will only remove the content categories specified by the options object.
79 try
80 {
81 $options = new SanitizeOptions();
82 $options->SetMetadata(true);
83 $options->SetFormData(true);
84 $options->SetBookmarks(true);
85
86 $doc = new PDFDoc($input_path."form1.pdf");
87 $doc->InitSecurityHandler();
88
89 Sanitizer::SanitizeDocument($doc, $options);
90 $doc->Save($output_path."form1_sanitized.pdf", SDFDoc::e_linearized);
91 echo(nl2br("Done...\n"));
92 }
93 catch(Exception $e)
94 {
95 echo(nl2br($e->getMessage()."\n"));
96 }
97
98 PDFNet::Terminate();
99?>
100
101

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales