PHP code example of avvertix / liteparse-php

1. Go to this page and download the library: Download avvertix/liteparse-php 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/ */

    

avvertix / liteparse-php example snippets


use LiteParse\Config;
use LiteParse\OutputFormat;
use LiteParse\LiteParse;

$parser = new LiteParse(new Config(outputFormat: OutputFormat::Markdown));

$result = $parser->parseFile('/path/to/document.pdf');

echo $result->pageCount();   // int
echo $result->text();        // plain text, "--- Page N ---" headers
echo $result->markdown();    // headings/lists/tables/figure refs reconstructed from layout
echo json_encode($result->json());  // structured per-page text items: bbox + font size + fill/stroke color, per item
echo json_encode($result->lines());  // structured per-page projected lines: merged bbox + style + column geometry

use LiteParse\Config;
use LiteParse\LiteParse;
use LiteParse\Layout\BlockKind;

$parser = new LiteParse(new Config(extractBlocks: true));
$result = $parser->parseFile('/path/to/document.pdf');

// Grouped by page — the default, familiar shape:
foreach ($result->blocks() as $page) {
    foreach ($page['blocks'] as $block) {
        if ($block->kind === BlockKind::Heading) {
            echo str_repeat('#', $block->level ?? 1)." {$block->text}\n";
        }
    }
}

// Or flattened across the whole document, each block still tagged with its own page:
foreach ($result->flatBlocks() as $block) {
    printf("p%d %s: %s\n", $block->pageNumber, $block->kind->value, $block->text ?? '');
}

$parser = new LiteParse(new Config(
    ocrServerUrl: 'http://localhost:8828/ocr', // implies ocrEnabled: true
));

$result = $parser->parseFile('scan.png'); // or a scanned/photographed PDF page
echo $result->text();

$parser = new LiteParse(new Config(dpi: 150.0)); // keep parse and screenshot DPI in sync
$result = $parser->parseFile('report.pdf');

$matches = $result->search('quarterly revenue');
$screenshots = $parser->screenshotFile('report.pdf', pageNumbers: array_column($matches, 'page_number'));

// Complexity pre-check
$stats = $parser->isComplexFile('scan.pdf');
$needsOcr = array_filter($stats, fn ($page) => $page['needs_ocr']);

// Screenshots
$screenshots = $parser->screenshotFile('doc.pdf', pageNumbers: [1, 2]); // null = all pages
foreach ($screenshots as $shot) {
    file_put_contents("page-{$shot->pageNumber}.png", $shot->bytes);
}

// Search
$result = $parser->parseFile('doc.pdf');
foreach ($result->search('quarterly revenue') as $match) {
    printf("page %d at (%.0f, %.0f)\n", $match['page_number'], $match['x'], $match['y']);
}
bash
composer iteparse-php install
bash
./scripts/build.sh release   # cargo build --release + stage lib/liteparse_php.* and lib/libpdfium.*
composer install
composer test