Official PHP SDK for the scan-forge OCR service — an on-premise, AI-powered drop-in replacement for ABBYY Recognition Server.
composer require moonforge/scanforgeRequires PHP 8.1+.
<?php
use Moonforge\ScanForge\Client;
$client = new Client('sf_live_...');
// Extract text from a PDF
$result = $client->ocr('faktura.pdf');
echo $result->text;
// Detect barcodes
$barcodes = $client->barcodes('dokument.pdf');
foreach ($barcodes as $b) {
echo $b->value . ' ' . $b->type;
}
// Convert a scan to DOCX
$client->convert('skan.png', ['output' => 'wynik.docx']);Creates a new client instance.
| Parameter | Type | Required | Default |
|---|---|---|---|
$apiKey |
string |
Yes | — |
$baseUrl |
string |
No | https://api.scanforge.tech |
$httpClient |
GuzzleHttp\Client|null |
No | null (auto-created) |
$client = new Client(
apiKey: 'sf_live_...',
baseUrl: 'https://ocr.your-server.com', // for self-hosted deployments
);Extracts text from a PDF or image file.
Options
| Key | Type | Default | Description |
|---|---|---|---|
language |
string |
'pol' |
OCR language code |
page_number |
int |
— | Process a single page (0-indexed) |
Returns OcrResult
class OcrResult {
public readonly string $text;
public readonly int $pages;
public readonly array $metadata;
}Example
$result = $client->ocr('invoice.pdf', ['language' => 'eng']);
echo $result->text; // extracted text
echo $result->pages; // number of pages processedDetects and decodes barcodes (1D and 2D) in a document.
Options
| Key | Type | Default | Description |
|---|---|---|---|
page_number |
int |
0 |
Page to scan (0 = all pages) |
Returns BarcodeResult[]
class BarcodeResult {
public readonly string $value; // decoded barcode content
public readonly string $type; // symbology e.g. 'EAN-13', 'QR-Code', 'CODE-128'
public readonly int $page; // 1-indexed page number
}Example
$barcodes = $client->barcodes('shipment.pdf');
foreach ($barcodes as $b) {
echo $b->value . ' (' . $b->type . ') on page ' . $b->page . PHP_EOL;
}Converts a PDF or image to an editable document format. The output format is determined by the extension of $options['output'] (.docx → DOCX, .xlsx → XLSX).
Options
| Key | Type | Required | Description |
|---|---|---|---|
output |
string |
Yes | Destination path (.docx or .xlsx) |
language |
string |
No | OCR language code (default 'pol') |
Returns void — the converted file is written to output on the server.
Example
// Convert to Word document
$client->convert('scan.pdf', ['output' => 'result.docx']);
// Convert to Excel spreadsheet (preserves table structure)
$client->convert('table.pdf', ['output' => 'data.xlsx']);All methods throw ScanForgeException on failure.
use Moonforge\ScanForge\Client;
use Moonforge\ScanForge\ScanForgeException;
$client = new Client('sf_live_...');
try {
$result = $client->ocr('document.pdf');
} catch (ScanForgeException $e) {
echo $e->getMessage(); // human-readable message
echo $e->statusCode; // HTTP status code (int or null for network errors)
var_dump($e->body); // raw response body from the server
}| Error condition | $statusCode |
|---|---|
| Invalid API key | 401 |
| Unsupported file type | 422 |
| Server error | 5xx |
| Network / connection failure | null |
$client = new Client(
'sf_live_...',
'https://ocr.internal.example.com',
);$client = new Client(
apiKey: $_ENV['SCANFORGE_API_KEY'],
baseUrl: $_ENV['SCANFORGE_URL'] ?? 'http://localhost:8000',
);- PHP 8.1+
- guzzlehttp/guzzle ^7.0 (installed automatically via Composer)
- A running scan-forge server — see deployment docs
MIT © Moonforge