Extracting PDF with OCR

Description

PDFs extraction system that processes documents with a 3 step workflow designed for efficient file management up to 50 MB. It uses fragmented loads (1 MB per chunk) with Base64 encoding and returns JSON structured data extracted by OCR and AI.

Specializes in: complex documents such as constitutional acts, notary documents and CFE receipts.

Specifications

SpecificationValue
Maximum file size50 MB
Input formatPDF encoded at Base64
Output formatJSON structured
Expiry of sitting1 hour

Processing Flow

INIT → UPLOAD-CHUNK (×N) → COMPLETE → Resultado JSON

Endpoints

1. INIT - Session initiation

POST /v4/document/extract/pdf/init

Headers:

  • Request-Id (UUID v4, required)
  • Language (in / es, optional)
  • Authorization (Bearer token)

Body: empty

Answer:

  • upload_id - Unique session identifier
  • file_size - Expected total Bytes
  • chunk_size - Individual chunk size (typically 1 MB)
  • total_chunks - Number of necessary chunks
  • expires_at - Expirand time

2. UPLOAD-CHUNK - Loading of Fragments

POST /v4/document/extract/pdf/upload-chunk

Important: The chunks use zero base indexation (0, 1, 2... N-1).

Body:

  • upload_id - INIT's response
  • chunk_number - Retirement (base -0)
  • chunk_data - PDF segment encoded at Base64

Answer:

  • progress_percent - Percentage of completeness
  • uploaded_chunks - count of chunks received
  • remaining_chunks - Still necessary
  • is_complete - Flag boolan when all chunks are received

3. COMPLETE - Completion of processing

POST /v4/document/extract/pdf/complete

Body:

  • upload_id - Session identifier
  • name - Optional user identifier

Answer (example Constitutional Act):

{
  "eventId": "UUID",
  "status": "SUCCESS",
  "content": {
    "data": {
      "capital_shareholders": {},
      "company_data": {},
      "initial_governing_bodies": {},
      "main_corporate_purpose": []
    },
    "extra": {
      "document_type": "ACTA_CONSTITUTIVA",
      "country_code": "MX",
      "page_count": 22
    }
  },
  "processingTime": 1234
}

Types of Supported Documents

  • Constitutional acts
  • Notary
  • CFE copies
  • Commercial contracts
  • Financial statements
  • Legal agreements

Note: For identification documents (INE), JAAK provides specialized endpoints.

Critical Points of Implementation

Indexation Ground Zero

First chunk's always 0No. 1. To 19 total chunks, send 0-18.

Auto- Server Setup

Never compute manually chunk_size or total_chunks - INIT response provides these values.

Expiration of Session

Each session expires 1 hour after creation.

Flexibility in Loading Chunks

Send chunks in any order but they have to get here before calling COMPLETE.

JavaScript implementation

class JAKPDFExtractor {
    async extractPDF(file) {
        // 1. Validar archivo
        // 2. Convertir a Base64
        // 3. INIT: get chunk parameters
        const initRes = await this._initUpload();

        // 4. UPLOAD-CHUNK: send fragments
        for (let i = 0; i < initRes.total_chunks; i++) {
            await this._uploadChunk(i, chunks[i]);
        }

        // 5. COMPLETE: finalizar procesamiento
        return await this._completeUpload();
    }
}

Timeouts Recommended

OperationTimeout
INIT10 seconds
UPLOAD-CHUNK30 seconds per chunk
COMPLETE120 seconds (processing) OCR)

Errors Management

CodeDescription
400Invalid file size / format or incomplete chunks
401Unauthorized / expired Token
404Expiry or non-existent meeting
429Rate limitation (100 req / min by API )
500Failed processing OCR

Example error:

{
  "errorCode": "INCOMPLETE_UPLOAD",
  "message": "Missing chunks: 3, 7, 12",
  "statusCode": 400
}

Best Practices

  1. Revalidation: Check PDF type, size (max 50MB) and extension before loading
  2. Session management: Storage upload_id for incremental loads
  3. Retry logic: Exponential backoff (1s, 2s, 4s) for failed chunks
  4. Monitoring of progress: Show percentage and count of chunks to users
  5. Security: Never explain API front keys; use proxy backend
  6. HTTPS only: All communications should use TLS 1.3

Security and Privacy

  • No persisting document storage and memory processing
  • Automatic login after 1 hour
  • Full-track audit track Request-Id and eventId
  • Compliance with Mexican data protection regulations