|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2023 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +# [START documentai_quickstart] |
| 19 | +# Includes the autoloader for libraries installed with composer |
| 20 | +require __DIR__ . '/vendor/autoload.php'; |
| 21 | + |
| 22 | +# Imports the Google Cloud client library |
| 23 | +use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient; |
| 24 | +use Google\Cloud\DocumentAI\V1\RawDocument; |
| 25 | + |
| 26 | +$projectId = 'YOUR_PROJECT_ID'; # Your Google Cloud Platform project ID |
| 27 | +$location = 'us'; # Your Processor Location |
| 28 | +$processor = 'YOUR_PROCESSOR_ID'; # Your Processor ID |
| 29 | + |
| 30 | +# Create Client |
| 31 | +$client = new DocumentProcessorServiceClient(); |
| 32 | + |
| 33 | +# Local File Path |
| 34 | +$documentPath = 'resources/invoice.pdf'; |
| 35 | + |
| 36 | +# Read in File Contents |
| 37 | +$handle = fopen($documentPath, 'rb'); |
| 38 | +$contents = fread($handle, filesize($documentPath)); |
| 39 | +fclose($handle); |
| 40 | + |
| 41 | +# Load File Contents into RawDocument |
| 42 | +$rawDocument = new RawDocument([ |
| 43 | + 'content' => $contents, |
| 44 | + 'mime_type' => 'application/pdf' |
| 45 | +]); |
| 46 | + |
| 47 | +# Fully-qualified Processor Name |
| 48 | +$name = $client->processorName($projectId, $location, $processor); |
| 49 | + |
| 50 | +# Make Processing Request |
| 51 | +$response = $client->processDocument($name, [ |
| 52 | + 'rawDocument' => $rawDocument |
| 53 | +]); |
| 54 | + |
| 55 | +# Print Document Text |
| 56 | +printf('Document Text: %s', $response->getDocument()->getText()); |
| 57 | + |
| 58 | +# [END documentai_quickstart] |
0 commit comments