3

I'm trying to run Document OCR from Google with a Node.js app. So I used the client library for Node JavaScript @google-cloud/documentai

I did everything like in documentation sample

There is my code

const projectId = '*******';
const location = 'eu'; // Format is 'us' or 'eu'
const processor = '******'; // Create processor in Cloud Console
const keyFilename = './secret/******.json';

const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;

const client = new DocumentProcessorServiceClient({projectId, keyFilename});

async function start(encodedImage) {

  console.log("Google AI Started")
  const name = `projects/${projectId}/locations/${location}/processors/${processor}`;

  const request = {
    name,
    document: {
      content: encodedImage,
      mimeType: 'application/pdf',
    },
  }

  try {
    const [result] = await client.processDocument(request);

    const { document } = result;

    const { text } = document;

    const getText = textAnchor => {
      if (!textAnchor.textSegments || textAnchor.textSegments.length === 0) {
        return '';
      }

      // First shard in document doesn't have startIndex property
      const startIndex = textAnchor.textSegments[0].startIndex || 0;
      const endIndex = textAnchor.textSegments[0].endIndex;

      return text.substring(startIndex, endIndex);
    };

    const [page1] = document;
    const { paragraphs } = page1;

    for (const paragraph of paragraphs) {
      const paragraphText = getText(paragraph.layout.textAnchor);
      console.log(`Paragraph text:\n${paragraphText}`);
    }
    return paragraphs;
  }
  catch (error) {
    console.error(error);
  }
}

module.exports = {
  start
}

Image encoding is here

const {start: google} = require('./document-ai/index')

if (mimeType === 'application/pdf') {
        pdf = true;
        encoded = Buffer.from(file).toString('base64');
      }

await google(encoded);

In result I get this error

Google AI Started
Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
    at Object.callErrorFromStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call.js:31:26)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client.js:176:52)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:342:141)
    at Object.onReceiveStatus (C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:305:181)
    at C:\Users\NIKIGAN\WebstormProjects\papper-project\server\node_modules\google-gax\node_modules\@grpc\grpc-js\build\src\call-stream.js:124:78
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  code: 3,
  details: 'Request contains an invalid argument.',
  metadata: Metadata {
    internalRepr: Map { 'grpc-server-stats-bin' => [Array] },
    options: {}
  },
  note: 'Exception occurred in retry method that was not classified as transient'
}

What invalid arguments do I have in my request?

Environment details

  • OS: Windows 10
  • Node.js version: 12.18.3
  • NPM version: 6.14.8
  • @google-cloud/documentai version: 2.2.1
double-beep
  • 5,031
  • 17
  • 33
  • 41
nikigan
  • 41
  • 3
  • Are you using the ***v1beta3*** API version? Also, how big is the targeted document file (in number of pages)? According to the [documentation](https://cloud.google.com/document-ai/docs/ocr), for small files ( ideally less than 5 pages, maximum of 10) you can use online processing, which is the code you are using. However, for larger files you need to use *"offline"* processing. – Alexandre Moraes Nov 19 '20 at 09:35
  • Yes, I'm using the v1beta3, my document is only one page length. – nikigan Nov 20 '20 at 04:09
  • Your code differs a bit from the link you shared. Since the error is related to invalid argument, could you use image enconding such as in line 46 `const encodedImage = Buffer.from(imageFile).toString('base64');` instead? – Alexandre Moraes Nov 20 '20 at 09:44
  • Does this answer your question? [Document AI: google.api\_core.exceptions.InvalidArgument: 400 Request contains an invalid argument](https://stackoverflow.com/questions/66456548/document-ai-google-api-core-exceptions-invalidargument-400-request-contains-an) – Holt Skinner Aug 05 '22 at 17:36

1 Answers1

5

I've strugglid with this as well and the solution is quite simple as it turns out: you have to set the parameter apiEndpoint when your location is not "us".

Here's an example for location "eu":

const client = new DocumentProcessorServiceClient({ 
  keyFilename, 
  apiEndpoint: 'eu-documentai.googleapis.com' 
});

More information here: GitHub: googleapis / nodejs-document-ai

Dharman
  • 30,962
  • 25
  • 85
  • 135
Julius B
  • 81
  • 1
  • 3