Skip to content

Commit eba5b71

Browse files
authored
feat: [DocumentAI] add quickstart sample (GoogleCloudPlatform#1767)
1 parent d5dd7e6 commit eba5b71

File tree

9 files changed

+201
-0
lines changed

9 files changed

+201
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ credentials.*
1212
.vscode/
1313
.kokoro/secrets.sh
1414
.phpunit.result.cache
15+
.DS_Store

.kokoro/secrets-example.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export DATASTORE_EVENTUALLY_CONSISTENT_RETRY_COUNT=
7474
export DLP_DEID_WRAPPED_KEY=
7575
export DLP_DEID_KEY_NAME=projects/$GOOGLE_PROJECT_ID/locations/global/keyRings/ci/cryptoKeys/ci
7676

77+
# DocumentAI
78+
export GOOGLE_DOCUMENTAI_PROCESSOR_ID=
79+
7780
# Firestore
7881
export FIRESTORE_PROJECT_ID=
7982

.kokoro/secrets.sh.enc

69 Bytes
Binary file not shown.

documentai/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Google Cloud Document AI Samples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=documentai
7+
8+
These samples show how to use the [Google Cloud Document AI][document-ai]
9+
10+
This repository contains samples that use the [Cloud Document AI Client
11+
Library for PHP][google-cloud-php-documentai] to make REST calls as well as
12+
contains samples using the more-efficient (though sometimes more
13+
complex) [GRPC][grpc] API. The GRPC API also allows streaming requests.
14+
15+
## Installation
16+
17+
Install the dependencies for this library via [composer](https://getcomposer.org)
18+
19+
$ cd /path/to/php-docs-samples/documentai
20+
$ composer install
21+
22+
Configure your project using [Application Default Credentials][adc]
23+
24+
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
25+
26+
## Usage
27+
28+
Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments
29+
are provided:
30+
31+
## Troubleshooting
32+
33+
If you get the following error, set the environment variable `GCLOUD_PROJECT` to your project ID:
34+
35+
```
36+
[Google\Cloud\Core\Exception\GoogleException]
37+
No project ID was provided, and we were unable to detect a default project ID.
38+
```
39+
40+
If you have not set a timezone you may get an error from php. This can be resolved by:
41+
42+
1. Finding where the php.ini is stored by running php -i | grep 'Configuration File'
43+
1. Finding out your timezone from the list on this page: http://php.net/manual/en/timezones.php
44+
1. Editing the php.ini file (or creating one if it doesn't exist)
45+
1. Adding the timezone to the php.ini file e.g., adding the following line: date.timezone = "America/Los_Angeles"
46+
47+
[document-ai]: https://cloud.google.com/document-ai/docs/overview
48+
[google-cloud-php-documentai]: https://cloud.google.com/php/docs/reference/cloud-document-ai/latest
49+
[grpc]: http://grpc.io
50+
[adc]: https://developers.google.com/identity/protocols/application-default-credentials

documentai/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/cloud-document-ai": "^1.0.1"
4+
}
5+
}

documentai/phpunit.xml.dist

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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+
<phpunit bootstrap="../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP documentai test">
20+
<directory>test</directory>
21+
</testsuite>
22+
</testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
</logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">./src</directory>
29+
<file>quickstart.php</file>
30+
<exclude>
31+
<directory>./vendor</directory>
32+
</exclude>
33+
</whitelist>
34+
</filter>
35+
<php>
36+
<env name="PHPUNIT_TESTS" value="1"/>
37+
</php>
38+
</phpunit>

documentai/quickstart.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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]

documentai/resources/invoice.pdf

57.6 KB
Binary file not shown.

documentai/test/quickstartTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
use Google\Cloud\TestUtils\TestTrait;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class quickstartTest extends TestCase
21+
{
22+
use TestTrait;
23+
24+
protected static $tempFile;
25+
26+
public function setUp(): void
27+
{
28+
$processorId = $this->requireEnv('GOOGLE_DOCUMENTAI_PROCESSOR_ID');
29+
self::$tempFile = sys_get_temp_dir() . '/documentai_quickstart.php';
30+
$contents = file_get_contents(__DIR__ . '/../quickstart.php');
31+
$contents = str_replace(
32+
['YOUR_PROJECT_ID', 'YOUR_PROCESSOR_ID', '__DIR__'],
33+
[self::$projectId, $processorId, sprintf('"%s/.."', __DIR__)],
34+
$contents
35+
);
36+
file_put_contents(self::$tempFile, $contents);
37+
}
38+
39+
public function testQuickstart()
40+
{
41+
// Invoke quickstart.php
42+
$output = $this->runSnippet(self::$tempFile);
43+
44+
$this->assertStringContainsString('Invoice', $output);
45+
}
46+
}

0 commit comments

Comments
 (0)