Skip to content
ComPDF

Set DocumentAI Model

Overview

Before using OCR, Layout Analysis, Table Recognition, PDF to Searchable PDF, or PDF to OFD, the SDK needs to load the DocumentAI model. Starting with the PHP SDK v4.1.0, the model lifecycle is exposed through three static methods on LibraryManager:

MethodPurpose
setDocumentAIModel(string $modelPath, int $gpuId = -1): intLoad the model. Pass -1 for $gpuId to disable GPU acceleration, or a non-negative GPU device index to enable it.
setDocumentAIModelCount(int $layoutModelCount, int $tableModelCount): voidConfigure how many concurrent Layout / Table model instances the SDK keeps in memory. Increase the counts to improve throughput when many conversions run in parallel.
releaseDocumentAIModel(): voidFree the model (and its GPU resources) without shutting down the rest of the SDK. After this call AI features become unavailable until setDocumentAIModel() is invoked again.

Notes

  • setDocumentAIModel() must be called after LibraryManager::initialize() and before any conversion that relies on AI features.
  • The method returns an int error code; use ErrorCode::isSuccess() / ErrorCode::describe() to interpret it.

Example

php
use ComPDFKit\Conversion\ErrorCode;
use ComPDFKit\Conversion\LibraryManager;

LibraryManager::initialize(__DIR__ . '/../');

$rc = LibraryManager::setDocumentAIModel(__DIR__ . '/../resource/models/documentai.model', -1);
if (!ErrorCode::isSuccess($rc)) {
    throw new RuntimeException('setDocumentAIModel failed: ' . ErrorCode::describe($rc));
}

// Keep one layout model and one table model in memory.
LibraryManager::setDocumentAIModelCount(1, 1);

If you want to run OCR, Layout Analysis or Table Recognition with your own model or a third-party service instead of the bundled DocumentAI model, see Use Custom AI Models via Callbacks.