Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion documentai/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"require": {
"google/cloud-document-ai": "^1.0.1"
"php": "^8.1",
"google/cloud-document-ai": "^2.1.3"
}
}
43 changes: 22 additions & 21 deletions documentai/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<phpunit bootstrap="../testing/bootstrap.php">
<testsuites>
<testsuite name="PHP documentai test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<file>quickstart.php</file>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="../testing/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: I don't think any of these changes in phpunit.xml are needed... we don't care about code coverage for samples.

<include>
<directory suffix=".php">./src</directory>
<file>quickstart.php</file>
</include>
<exclude>
<directory>./vendor</directory>
</exclude>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="PHP documentai test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging/>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
54 changes: 31 additions & 23 deletions documentai/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,51 @@
*/

# [START documentai_quickstart]
# Includes the autoloader for libraries installed with composer
# Include the autoloader for libraries installed with Composer.
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient;
# Import the Google Cloud client library.
use Google\Cloud\DocumentAI\V1\Client\DocumentProcessorServiceClient;
use Google\Cloud\DocumentAI\V1\RawDocument;
use Google\Cloud\DocumentAI\V1\ProcessRequest;

$projectId = 'YOUR_PROJECT_ID'; # Your Google Cloud Platform project ID
$location = 'us'; # Your Processor Location
$processor = 'YOUR_PROCESSOR_ID'; # Your Processor ID
# TODO(developer): Update the following lines before running the sample.
# Your Google Cloud Platform project ID.
$projectId = 'YOUR_PROJECT_ID';

# Create Client
$client = new DocumentProcessorServiceClient();
# Your Processor Location.
$location = 'us';

# Your Processor ID as hexadecimal characters.
# Not to be confused with the Processor Display Name.
$processorId = 'YOUR_PROCESSOR_ID';

# Local File Path
# Path for the file to read.
$documentPath = 'resources/invoice.pdf';

# Read in File Contents
# Create Client.
$client = new DocumentProcessorServiceClient();

# Read in file.
$handle = fopen($documentPath, 'rb');
$contents = fread($handle, filesize($documentPath));
fclose($handle);

# Load File Contents into RawDocument
$rawDocument = new RawDocument([
'content' => $contents,
'mime_type' => 'application/pdf'
]);
# Load file contents into a RawDocument.
$rawDocument = (new RawDocument())
->setContent($contents)
->SetMimeType('application/pdf');

# Fully-qualified Processor Name
$name = $client->processorName($projectId, $location, $processor);
# Get the Fully-qualified Processor Name.
$fullProcessorName = $client->processorName($projectId, $location, $processorId);

# Make Processing Request
$response = $client->processDocument($name, [
'rawDocument' => $rawDocument
]);
# Send a ProcessRequest and get a ProcessResponse.
$request = (new ProcessRequest())
->setName($fullProcessorName)
->setRawDocument($rawDocument);

# Print Document Text
printf('Document Text: %s', $response->getDocument()->getText());
$response = $client->processDocument($request);

# Show the text found in the document.
printf('Document Text: %s', $response->getDocument()->getText());
# [END documentai_quickstart]