【问题标题】:Textract async read PDFTextract 异步读取 PDF
【发布时间】:2021-06-19 16:31:44
【问题描述】:

来自文本documentationDocuments for synchronous operations can be in PNG or JPEG format. Documents for asynchronous operations can also be in PDF format.

我有一个 Node.js 应用程序,我在其中使用异步 Textract 读取 PDF 文件。我的代码如下所示:

import * as AWS from 'aws-sdk';

const textract = new AWS.Textract({ region: '<REGION>' });

export const callTextract = (file: File, uuid: string): Promise<any> => {
  return new Promise<any>((resolve, reject) => {
    const params = {
      Document: {
        Bytes: file,
      },
    };
    textract.detectDocumentText(params, (err, data) => {
      ....
      resolve(data);
    });
  })
}

这里的文件已经从操作系统中读取,是Buffer格式。由于前 4 个字节(Detecting file type from buffer in node js?),我可以确认它是 PDF 文件:

 <Buffer 25 50 44 46 ... >

我收到的错误是UnsupportedDocumentException

【问题讨论】:

    标签: node.js typescript amazon-web-services pdf amazon-textract


    【解决方案1】:

    detectDocumentText() 是同步的。异步版本为startDocumentTextDetection()

    doc:

    检测输入文档中的文本。 Amazon Textract 可以检测文本行和构成文本行的单词。 输入文档必须是 JPEG 或 PNG 格式的图像。

    ...

    DetectDocumentText 是一个同步操作。 要异步分析文档,请使用 StartDocumentTextDetection。

    请注意,语言的异步机制与 API 的异步调用不同。对于异步 API,总会有至少两个调用。在这种情况下,另一个是getDocumentTextAnalysis()


    ...尽管我认为这是另一个糟糕的 AWS 文档示例。

    【讨论】:

      【解决方案2】:

      您可以在同步和异步 API 中提供字节字段,但字节字段定义在两个 API 中是相同的

      base64 编码的文档字节块。以 blob 字节提供的文档的最大大小为 5 MB。 文档字节必须为 PNG 或 JPEG 格式

      因此您不能上传 PDF 格式的字节字段值

      来自文档:https://docs.aws.amazon.com/textract/latest/dg/API_Document.html#API_Document_Contents

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-11
        • 2022-10-14
        • 1970-01-01
        • 2017-08-29
        • 1970-01-01
        • 2019-06-09
        • 1970-01-01
        相关资源
        最近更新 更多