@jaypie/textract
Prerequisites: npm install @jaypie/textract and AWS credentials
Status: Experimental - APIs may change
Overview
Section titled âOverviewâ@jaypie/textract provides utilities for processing documents with AWS Textract, including text extraction from images and PDFs.
Installation
Section titled âInstallationânpm install @jaypie/textractQuick Reference
Section titled âQuick ReferenceâExports
Section titled âExportsâ| Export | Purpose |
|---|---|
analyzeDocument |
Analyze document for text and structure |
detectText |
Simple text detection |
startDocumentAnalysis |
Async analysis for large documents |
getDocumentAnalysis |
Get async analysis results |
Basic Usage
Section titled âBasic UsageâDetect Text
Section titled âDetect Textâimport { detectText } from "@jaypie/textract";
const result = await detectText({ bucket: "my-bucket", key: "documents/invoice.pdf",});
console.log(result.text);Analyze Document
Section titled âAnalyze Documentâimport { analyzeDocument } from "@jaypie/textract";
const result = await analyzeDocument({ bucket: "my-bucket", key: "documents/form.png", features: ["TABLES", "FORMS"],});
console.log(result.tables);console.log(result.forms);Document Features
Section titled âDocument Featuresâ| Feature | Description |
|---|---|
TABLES |
Extract tabular data |
FORMS |
Extract key-value pairs |
SIGNATURES |
Detect signatures |
LAYOUT |
Analyze document layout |
Async Processing
Section titled âAsync ProcessingâFor large documents (>5 pages):
Start Analysis
Section titled âStart Analysisâimport { startDocumentAnalysis } from "@jaypie/textract";
const { jobId } = await startDocumentAnalysis({ bucket: "my-bucket", key: "documents/large-report.pdf", features: ["TABLES"],});Get Results
Section titled âGet Resultsâimport { getDocumentAnalysis } from "@jaypie/textract";
const result = await getDocumentAnalysis({ jobId });
if (result.status === "SUCCEEDED") { console.log(result.text); console.log(result.tables);}Poll for Completion
Section titled âPoll for Completionâimport { getDocumentAnalysis, sleep } from "@jaypie/textract";
async function waitForAnalysis(jobId: string) { while (true) { const result = await getDocumentAnalysis({ jobId });
if (result.status === "SUCCEEDED") { return result; }
if (result.status === "FAILED") { throw new Error("Analysis failed"); }
await sleep(5000); }}Table Extraction
Section titled âTable Extractionâconst result = await analyzeDocument({ bucket: "my-bucket", key: "documents/spreadsheet.png", features: ["TABLES"],});
for (const table of result.tables) { console.log("Table:", table.rows); for (const row of table.rows) { console.log("Row:", row.cells.map(c => c.text)); }}Form Extraction
Section titled âForm Extractionâconst result = await analyzeDocument({ bucket: "my-bucket", key: "documents/application.pdf", features: ["FORMS"],});
for (const field of result.forms) { console.log(`${field.key}: ${field.value}`);}Lambda Integration
Section titled âLambda Integrationâimport { lambdaHandler } from "jaypie";import { analyzeDocument } from "@jaypie/textract";
export const handler = lambdaHandler(async (event) => { const { bucket, key } = event;
const result = await analyzeDocument({ bucket, key, features: ["TABLES", "FORMS"], });
return { text: result.text, tables: result.tables.length, forms: result.forms.length, };});S3 Event Processing
Section titled âS3 Event Processingâimport { lambdaHandler, log } from "jaypie";import { analyzeDocument } from "@jaypie/textract";
export const handler = lambdaHandler(async (event) => { for (const record of event.Records) { const bucket = record.s3.bucket.name; const key = decodeURIComponent(record.s3.object.key);
log.trace("[process] analyzing document"); log.var({ key });
const result = await analyzeDocument({ bucket, key, features: ["TABLES"], });
await saveResults(key, result); }});Error Handling
Section titled âError Handlingâimport { BadGatewayError, log } from "jaypie";import { analyzeDocument } from "@jaypie/textract";
async function processDocument(bucket: string, key: string) { try { return await analyzeDocument({ bucket, key }); } catch (error) { log.error("Textract analysis failed"); log.var({ error: error.message }); throw BadGatewayError("Document processing failed"); }}IAM Permissions
Section titled âIAM PermissionsâLambda requires:
{ "Effect": "Allow", "Action": [ "textract:DetectDocumentText", "textract:AnalyzeDocument", "textract:StartDocumentAnalysis", "textract:GetDocumentAnalysis" ], "Resource": "*"}And S3 access:
{ "Effect": "Allow", "Action": ["s3:GetObject"], "Resource": "arn:aws:s3:::my-bucket/*"}Related
Section titled âRelatedâ- @jaypie/lambda - Lambda handlers
- CDK Infrastructure - Lambda deployment