Convert documents to GitHub-Flavored Markdown in Laravel applications, with access to anydoc's structured document model when available.
- ext-anydoc 1.0+
- Laravel 13+
- PHP 8.4+
Install the native extension with PIE:
pie install hosmelq/ext-anydoc:^1.0
PHP 8.6 needs PIE 1.5 or newer.
Install Laravel anydoc with Composer:
composer require hosmelq/laravel-anydoc
Publish the configuration file:
php artisan vendor:publish --tag="anydoc-config"Convert a local file to Markdown with the Anydoc facade:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $markdown = Anydoc::file('report.docx')->markdown();
file() also accepts an uploaded file:
$markdown = Anydoc::file($uploadedFile)->markdown();
Convert document bytes directly:
$bytes = file_get_contents('report.docx'); $markdown = Anydoc::bytes($bytes)->markdown();
CSV has no content signature, so CSV bytes require an explicit format:
use HosmelQ\Anydoc\Laravel\Enums\Format; use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $bytes = file_get_contents('data.csv'); $markdown = Anydoc::bytes($bytes, Format::Csv)->markdown();
All conversions run synchronously.
Pass ocr: true to enable OCR for a Markdown conversion:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $markdown = Anydoc::file('scan.pdf')->markdown(ocr: true);
The same option works with bytes, uploaded files, and filesystem disks.
Conversion runs locally first. If the PDF requires OCR, the entire file is sent
to Firecrawl Parse, including pages that already contain text. OCR is disabled
by default. Without it, scanned PDFs throw a NeedsOcrException.
Convert a file stored on a Laravel filesystem disk:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $markdown = Anydoc::disk('s3') ->file('documents/report.docx') ->markdown();
Call Anydoc::disk() without a name to use Laravel's default disk.
Disk files and uploaded files are read into memory before conversion.
Call document() to access anydoc's readonly document model:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $document = Anydoc::file('presentation.pptx')->document(); $assets = $document->assets; $blocks = $document->blocks; $notes = $document->notes;
The document model includes blocks, checkboxes, embedded assets, inline content, lists, math, notes, and tables.
PDF supports Markdown conversion only. Calling document() for a PDF throws an
Anydoc\Exception\UnsupportedException.
| Format | Extensions |
|---|---|
| CSV | .csv |
| EPUB | .epub |
| Excel | .xls, .xlsb, .xlsm, .xlsx |
| OpenDocument | .odp, .ods, .odt |
.pdf |
|
| PowerPoint | .pot, .pps, .ppsm, .ppsx, .ppt, .pptm, .pptx |
| Rich Text Format | .rtf |
| Word | .doc, .docm, .docx |
Pass a Format enum case when the format cannot be detected from the content:
use HosmelQ\Anydoc\Laravel\Enums\Format; use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $markdown = Anydoc::bytes($bytes, Format::Docx)->markdown();
Detect a format from bytes, an extension, or a path:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; $fromBytes = Anydoc::formatFromBytes($bytes); $fromExtension = Anydoc::formatFromExtension('.DOCX'); $fromPath = Anydoc::formatFromPath('documents/report.docx');
Each method returns a Format enum case or null. Extension detection is
case-insensitive and accepts an optional leading dot.
Uploaded files use their contents first and their original name as a fallback. Filesystem files use their contents first and their path as a fallback.
Inject the Anydoc contract when a class should not depend on the facade:
use HosmelQ\Anydoc\Laravel\Contracts\Anydoc; final class ConvertDocument { public function __construct(private Anydoc $anydoc) {} public function handle(string $path): string { return $this->anydoc->file($path)->markdown(); } }
Native conversion errors extend Anydoc\Exception\ConvertException:
use Anydoc\Exception\ConvertException; use Anydoc\Exception\PanicException; use HosmelQ\Anydoc\Laravel\Facades\Anydoc; try { $markdown = Anydoc::file('report.docx')->markdown(); } catch (ConvertException $exception) { report($exception); } catch (PanicException $exception) { report($exception); }
Conversion exceptions include EncryptedException, IoException,
MalformedException, MissingPartException, NeedsOcrException,
ResourceLimitException, and UnsupportedException. NeedsOcrException
provides the 1-indexed PDF pages requiring OCR through $pages and the total
number of pages through $pageCount. Filesystem sources may also throw Laravel
filesystem exceptions when their contents cannot be read.
PanicException represents a panic from the native Rust library and does not
extend ConvertException.
OCR network failures, rejected requests, and invalid responses throw
HosmelQ\Anydoc\Laravel\Exceptions\HostedOcrException. It extends
RuntimeException and must be caught separately from ConvertException.
Call Anydoc::fake() to test application behavior without reading files or
running conversions:
use HosmelQ\Anydoc\Laravel\Facades\Anydoc; use HosmelQ\Anydoc\Laravel\RecordedConversion; Anydoc::fake([ 'markdown' => '# Converted document', ]); Anydoc::file('report.docx')->markdown(); Anydoc::assertConvertedToMarkdown( fn (RecordedConversion $conversion): bool => $conversion->input === 'report.docx', );
Use a closure when the response depends on the recorded conversion:
Anydoc::fake([ 'markdown' => fn (RecordedConversion $conversion): string => "# {$conversion->input}", ]); $markdown = Anydoc::file('report.docx')->markdown();
The fake provides assertions and access to recorded conversions:
Anydoc::assertConverted(); Anydoc::assertConvertedTimes(2); Anydoc::assertConvertedToDocument(); Anydoc::assertConvertedToMarkdown(); Anydoc::assertNotConverted($callback); Anydoc::assertNothingConverted(); $conversions = Anydoc::conversions();
Assertion and filtering callbacks receive a RecordedConversion containing
its disk, format, input, output type, source type, and ocr option. A
conversion is recorded when markdown() or document() is called.
Use the recorded ocr option to check whether OCR was requested:
Anydoc::file('scan.pdf')->markdown(ocr: true); Anydoc::assertConvertedToMarkdown( fn (RecordedConversion $conversion): bool => $conversion->ocr, );
The default fake Markdown response is an empty string. Document conversions
require a configured Anydoc\Document response.
Run the test suite with:
composer testSee CHANGELOG.md for a list of changes.
Pull requests are welcome. Please run the test suite before submitting changes.
The MIT License (MIT). Please see License File for more information.