PHP code example of moonforge / scanforge

1. Go to this page and download the library: Download moonforge/scanforge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moonforge / scanforge example snippets



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']);

$client = new Client(
    apiKey:  'sf_live_...',
    baseUrl: 'https://ocr.your-server.com', // for self-hosted deployments
);

class OcrResult {
    public readonly string $text;
    public readonly int    $pages;
    public readonly array  $metadata;
}

$result = $client->ocr('invoice.pdf', ['language' => 'eng']);
echo $result->text;    // extracted text
echo $result->pages;   // number of pages processed

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
}

$barcodes = $client->barcodes('shipment.pdf');
foreach ($barcodes as $b) {
    echo $b->value . ' (' . $b->type . ') on page ' . $b->page . PHP_EOL;
}

// 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']);

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
}

$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',
);