Barcode Extraction - PHP Sample Code

This sample shows how to use the Apryse Barcode Module to detect and extract barcodes from PDF documents; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

BarcodeTest.php

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 Barcode Module is an optional PDFNet add-on that can be used to extract
11// various types of barcodes from PDF documents.
12//
13// The Apryse SDK Barcode Module can be downloaded from http://dev.apryse.com/
14//---------------------------------------------------------------------------------------
15function WriteTextToFile($outputFile, $text)
16{
17 $outfile = fopen($outputFile, "w");
18 fwrite($outfile, $text);
19 fclose($outfile);
20}
21function main()
22{
23 // Relative path to the folder containing the test files.
24 $input_path = getcwd()."/../../TestFiles/Barcode/";
25 $output_path = getcwd()."/../../TestFiles/Output/";
26 // The first step in every application using PDFNet is to initialize the
27 // library and set the path to common PDF resources. The library is usually
28 // initialized only once, but calling Initialize() multiple times is also fine.
29 global $LicenseKey;
30 PDFNet::Initialize($LicenseKey);
31 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.
32 // The location of the Barcode Module
33 PDFNet::AddResourceSearchPath("../../../PDFNetC/Lib/");
34 if (!BarcodeModule::IsModuleAvailable()) {
35 echo(nl2br("\n"));
36 echo(nl2br("Unable to run BarcodeTest: Apryse SDK Barcode Module not available.\n"));
37 echo(nl2br("---------------------------------------------------------------\n"));
38 echo(nl2br("The Barcode Module is an optional add-on, available for download\n"));
39 echo(nl2br("at https://dev.apryse.com/. If you have already downloaded this\n"));
40 echo(nl2br("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 }
44 else {
45 try {
46 //--------------------------------------------------------------------------------
47 // Example 1) Detect and extract all barcodes from a PDF document into a JSON file
48 echo(nl2br("Example 1: extracting barcodes from barcodes.pdf to barcodes.json\n"));
49 // A) Open the .pdf document
50 $doc = new PDFDoc($input_path."barcodes.pdf");
51 // B) Detect PDF barcodes with the default options
52 BarcodeModule::ExtractBarcodes($doc, $output_path."barcodes.json");
53 $doc->Close();
54 //--------------------------------------------------------------------------------
55 // Example 2) Limit barcode extraction to a range of pages, and retrieve the JSON into a
56 // local string variable, which is then written to a file in a separate function call
57 echo(nl2br("Example 2: extracting barcodes from pages 1-2 to barcodes_from_pages_1-2.json\n"));
58 // A) Open the .pdf document
59 $doc = new PDFDoc($input_path."barcodes.pdf");
60 // B) Detect PDF barcodes with custom options
61 $options = new BarcodeOptions();
62 // Convert only the first two pages
63 $options->SetPages("1-2");
64 $json = BarcodeModule::ExtractBarcodesAsString($doc, $options);
65 // C) Save JSON to file
66 WriteTextToFile($output_path."barcodes_from_pages_1-2.json", $json);
67 $doc->Close();
68 //--------------------------------------------------------------------------------
69 // Example 3) Narrow down barcode types and allow the detection of both horizontal
70 // and vertical barcodes
71 echo(nl2br("Example 3: extracting basic horizontal and vertical barcodes\n"));
72 // A) Open the .pdf document
73 $doc = new PDFDoc($input_path."barcodes.pdf");
74 // B) Detect only basic 1D barcodes, both horizontal and vertical
75 $options = new BarcodeOptions();
76 // Limit extraction to basic 1D barcode types, such as EAN 13, EAN 8, UPCA, UPCE,
77 // Code 3 of 9, Code 128, Code 2 of 5, Code 93, Code 11 and GS1 Databar.
78 $options->SetBarcodeSearchTypes(BarcodeOptions::e_linear);
79 // Search for barcodes oriented horizontally and vertically
80 $options->SetBarcodeOrientations(
81 BarcodeOptions::e_horizontal |
82 BarcodeOptions::e_vertical);
83 BarcodeModule::ExtractBarcodes($doc, $output_path."barcodes_1D.json", $options);
84 $doc->Close();
85 }
86 catch (Exception $e) {
87 echo(nl2br("Unable to extract form fields data, error: " . $e->getMessage() . "\n"));
88 }
89 }
90 PDFNet::Terminate();
91 echo(nl2br("Done.\n"));
92}
93main();
94?>
95

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales