Skip to content

OcrClient ​

Category: Web

Source: ocr_client.dart

Classes ​

OcrClient ​

Client for OCR-based PDF text extraction using LLM vision APIs.

Supports Mistral OCR Small (pixtral-based) and OpenAI vision models. Used as a fallback when pdftotext returns empty text (scanned PDFs).

Constructor ​

dart
OcrClient({
    required this.provider,
    required this.apiKey,
    required this.model,
    this.timeoutSeconds = 120,
    http.Client? client,
  })
dart
factory OcrClient.fromConfig(PdfConfig config, {http.Client? client})
dart
OcrClient(
        provider: config.ocrProvider,
        apiKey: config.ocrProvider == OcrProviderType.mistral
            ? config.mistralApiKey ?? ''
            : config.openaiApiKey ?? '',
        model: config.ocrProvider == OcrProviderType.mistral
            ? config.mistralModel
            : config.openaiModel,
        client: client,
      )

Properties ​

PropertyTypeDescription
providerOcrProviderType
apiKeyString
modelString
timeoutSecondsint
headersMap<String, String> get
openaiEndpointString getThe OpenAI API endpoint for OCR requests. Uses the Responses API which natively supports PDF file inputs, unlike the Chat Completions API which only accepts images.
openaiEndpointString getThe OpenAI API endpoint for OCR requests. Uses the Responses API which natively supports PDF file inputs, unlike the Chat Completions API which only accepts images.

Methods ​

Future<String?> extractText(Uint8List pdfBytes) ​

Extract text from PDF bytes via OCR API.

String buildMistralRequestBody(Uint8List pdfBytes) ​
String buildOpenAIRequestBody(Uint8List pdfBytes) ​

Released under the MIT License.