PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - PHP Sample Code

Sample code for using Apryse SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF. Apryse SDK has full support for Forms Data Format (FDF). Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our full PDF Data Extraction SDK Capabilities.

To start your free trial, get stated with Server SDK.

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// PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract
12// forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
13// available in PDFNet.
14//---------------------------------------------------------------------------------------
15 PDFNet::Initialize($LicenseKey);
16 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.
17
18 // Relative path to the folder containing the test files.
19 $input_path = getcwd()."/../../TestFiles/";
20 $output_path = $input_path."Output/";
21
22 // Example 1:
23 // Iterate over all form fields in the document. Display all field names.
24
25 $doc = new PDFDoc($input_path."form1.pdf");
26 $doc->InitSecurityHandler();
27
28 for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next())
29 {
30 echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
31 echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");
32
33 echo "Field type: ";
34 $type = $itr->Current()->GetType();
35 switch($type)
36 {
37 case Field::e_button: echo nl2br("Button"."\n"); break;
38 case Field::e_check: echo nl2br("Check"."\n"); break;
39 case Field::e_radio: echo nl2br("Radio"."\n"); break;
40 case Field::e_text: echo nl2br("Text"."\n"); break;
41 case Field::e_choice: echo nl2br("Choice"."\n"); break;
42 case Field::e_signature: echo nl2br("Signature"."\n"); break;
43 case Field::e_null: echo nl2br("Null"."\n"); break;
44 }
45
46 echo nl2br("------------------------------\n");
47 }
48
49 $doc->Close();
50 echo nl2br("Done.\n");
51
52 // Example 2) Import XFDF into FDF, then merge data from FDF into PDF
53
54 // XFDF to FDF
55 // form fields
56 echo nl2br("Import form field data from XFDF to FDF.\n");
57
58 $fdf_doc1 = FDFDoc::CreateFromXFDF($input_path."form1_data.xfdf");
59 $fdf_doc1->Save($output_path."form1_data.fdf");
60
61 // annotations
62 echo nl2br("Import annotations from XFDF to FDF.\n");
63
64 $fdf_doc2 = FDFDoc::CreateFromXFDF($input_path."form1_annots.xfdf");
65 $fdf_doc2->Save($output_path."form1_annots.fdf");
66
67 // FDF to PDF
68 // form fields
69 echo nl2br("Merge form field data from FDF.\n");
70
71 $doc = new PDFDoc($input_path."form1.pdf");
72 $doc->InitSecurityHandler();
73 $doc->FDFMerge($fdf_doc1);
74
75 // Refreshing missing appearances is not required here, but is recommended to make them
76 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
77 $doc->RefreshAnnotAppearances();
78
79 $doc->Save(($output_path."form1_filled.pdf"), SDFDoc::e_linearized);
80
81 // annotations
82 echo nl2br("Merge annotations from FDF.\n");
83
84 $doc->FDFMerge($fdf_doc2);
85 // Refreshing missing appearances is not required here, but is recommended to make them
86 // visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
87 $doc->RefreshAnnotAppearances();
88 $doc->Save(($output_path."form1_filled_with_annots.pdf"), SDFDoc::e_linearized);
89 $doc->Close();
90 echo nl2br("Done.\n");
91
92
93 // Example 3) Extract data from PDF to FDF, then export FDF as XFDF
94
95 // PDF to FDF
96 $in_doc = new PDFDoc($output_path."form1_filled_with_annots.pdf");
97 $in_doc->InitSecurityHandler();
98
99 // form fields only
100 echo nl2br("Extract form fields data to FDF.\n");
101
102 $doc_fields = $in_doc->FDFExtract(PDFDoc::e_forms_only);
103 $doc_fields->SetPDFFileName("../form1_filled_with_annots.pdf");
104 $doc_fields->Save($output_path."form1_filled_data.fdf");
105
106 // annotations only
107 echo nl2br("Extract annotations to FDF.\n");
108
109 $doc_annots = $in_doc->FDFExtract(PDFDoc::e_annots_only);
110 $doc_annots->SetPDFFileName("../form1_filled_with_annots.pdf");
111 $doc_annots->Save($output_path."form1_filled_annot.fdf");
112
113 // both form fields and annotations
114 echo nl2br("Extract both form fields and annotations to FDF.\n");
115
116 $doc_both = $in_doc->FDFExtract(PDFDoc::e_both);
117 $doc_both->SetPDFFileName("../form1_filled_with_annots.pdf");
118 $doc_both->Save($output_path."form1_filled_both.fdf");
119
120 // FDF to XFDF
121 // form fields
122 echo nl2br("Export form field data from FDF to XFDF.\n");
123
124 $doc_fields->SaveAsXFDF($output_path."form1_filled_data.xfdf");
125
126 // annotations
127 echo nl2br("Export annotations from FDF to XFDF.\n");
128
129 $doc_annots->SaveAsXFDF($output_path."form1_filled_annot.xfdf");
130
131 // both form fields and annotations
132 echo nl2br("Export both form fields and annotations from FDF to XFDF.\n");
133
134 $doc_both->SaveAsXFDF($output_path."form1_filled_both.xfdf");
135
136 $in_doc->Close();
137 echo nl2br("Done.\n");
138
139 // Example 4) Merge/Extract XFDF into/from PDF
140
141 // Merge XFDF from string
142 $in_doc = new PDFDoc($input_path."numbered.pdf");
143 $in_doc->InitSecurityHandler();
144
145 echo nl2br("Merge XFDF string into PDF.\n");
146
147 $str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";
148 $fdoc = FDFDoc::CreateFromXFDF($str);
149 $in_doc->FDFMerge($fdoc);
150 $in_doc->Save(($output_path."numbered_modified.pdf"), SDFDoc::e_linearized);
151 echo nl2br("Merge complete.\n");
152
153 // Extract XFDF as string
154 echo nl2br("Extract XFDF as a string.\n");
155
156 $fdoc_new = $in_doc->FDFExtract(PDFDoc::e_both);
157 $XFDF_str = $fdoc_new->SaveAsXFDF();
158 echo nl2br("Extracted XFDF: \n");
159 echo nl2br($XFDF_str);
160 $in_doc->Close();
161 echo nl2br("\nExtract complete.\n");
162
163 // Example 5) Read FDF files directly
164
165 $doc = new FDFDoc($output_path."form1_filled_data.fdf");
166
167 for($itr = $doc->GetFieldIterator(); $itr->HasNext(); $itr->Next())
168 {
169 echo nl2br("Field name: ".$itr->Current()->GetName()."\n");
170 echo nl2br("Field partial name: ".$itr->Current()->GetPartialName()."\n");
171 echo nl2br("------------------------------\n");
172 }
173
174 $doc->Close();
175 echo nl2br("Done.\n");
176
177 // Example 6) Direct generation of FDF.
178
179 $doc = new FDFDoc();
180 // Create new fields (i.e. key/value pairs).
181 $doc->FieldCreate("Company", Field::e_text, "PDFTron Systems");
182 $doc->FieldCreate("First Name", Field::e_text, "John");
183 $doc->FieldCreate("Last Name", Field::e_text, "Doe");
184 // ...
185
186 // $doc->SetPdfFileName("mydoc.pdf");
187
188 $doc->Save($output_path."sample_output.fdf");
189 $doc->Close();
190 PDFNet::Terminate();
191 echo nl2br("Done. Results saved in sample_output.fdf");
192
193
194?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales