i am trying to use DocAI Api to parse invoices. I am using Laravel, but i dont know why the validation for string doesn't work correctly - my variable is a string but doesnt pass the checkstring function, it returns me:
InvalidArgumentException
Expect string.
at vendor\google\protobuf\src\Google\Protobuf\Internal\GPBUtil.php:97
93▕
94▕ public static function checkString(&$var, $check_utf8)
95▕ {
96▕ if (is_array($var) || is_object($var)) {
➜ 97▕ throw new \InvalidArgumentException("Expect string.");
98▕ }
99▕ if (!is_string($var)) {
100▕ $var = strval($var);
101▕ }
1 vendor\google\cloud-document-ai\src\V1\ProcessRequest.php:252
Google\Protobuf\Internal\GPBUtil::checkString(Object(Google\Cloud\DocumentAI\V1\ProcessRequest))
2 vendor\google\cloud-document-ai\src\V1\Gapic\DocumentProcessorServiceGapicClient.php:1807
Google\Cloud\DocumentAI\V1\ProcessRequest::setName(Object(Google\Cloud\DocumentAI\V1\ProcessRequest))
This is my command code, i tried in multiple ways to solve that - but i think i have to change the vendor package....
<?php
namespace App\Console\Commands;
use App\Jobs\ProcessDocumentAI;
use Google\Cloud\DocumentAI\V1\DocumentProcessorServiceClient;
use Google\Cloud\DocumentAI\V1\GcsOutputConfig;
use Illuminate\Console\Command;
use Google_Service_Storage;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Cloud\DocumentAI\V1\ProcessRequest;
use Google\Cloud\DocumentAI\V1\GcsDocument;
class ProcessExistingDocuments extends Command
{
protected $signature = 'process:existing-documents';
protected $description = 'Process existing documents in the Google Cloud Storage bucket using Document AI';
public function handle()
{
$keyFilePath = 'app/credentials/sunlsds.json';
$credentials = new ServiceAccountCredentials(
'https://www.googleapis.com/auth/cloud-platform',
$keyFilePath
);
// Create a Google Cloud Storage service instance
$storage = new Google_Service_Storage(['credentials' => $credentials]);
$documentAiClient = new DocumentProcessorServiceClient([
'credentials' => $credentials,
]);
$bucketName = 'a';
$storageObjects = $storage->objects->listObjects($bucketName);
foreach ($storageObjects->getItems() as $object) {
$gcsUri = 'gs://' . $bucketName . '/' . $object->name;
$objectName = $object->name;
// Skip processing if the object is a directory
if ($object->size === 0 || substr($objectName, -1) === '/') {
continue;
}
$allowedExtensions = ['pdf', 'jpg', 'jpeg', 'png', 'gif'];
$fileExtension = pathinfo($objectName, PATHINFO_EXTENSION);
if (!in_array(strtolower($fileExtension), $allowedExtensions)) {
continue; // Skip files with unsupported extensions
}
$gcsDocument = (new GcsDocument())
->setGcsUri($gcsUri);
$processRequest = (new ProcessRequest())
->setGcsDocument($gcsDocument)
->setName('projects/projectid/locations/us/processors/id8');
$operationResponse = $documentAiClient->processDocument($processRequest);
dd($operationResponse);
// Wait for the operation to complete
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$processedDocument = $operationResponse->getResult();
// Now you can extract information from $processedDocument
// Implement your logic to extract supplier_name and recipient_name
// based on the extracted data
// Once you have the supplier_name and recipient_name, you can update
// the file path in Google Cloud Storage and move the document
$newFilePath = 'invoices/an/luna/companie/' . $object->name();
$newObject = $storage->objects->copy($bucketName, $object->name(), $bucketName, $newFilePath);
$storage->objects->delete($bucketName, $object->name());
} else {
$this->error('Document processing failed: ' . $operationResponse->getError());
}
}
$this->info('Document processing completed.');
}
}
Well, the value has to be string, i changed the processRequest from package.
Instead of return $this, i changed the function with return $this->name..
public function setName($var) {
GPBUtil::checkString($var, True);
$this->name = $var;
return $this->name; }