GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - hosmelq/laravel-anydoc: Convert PDFs, Office files, and more to Markdown from Laravel.

BranchesTags
Open more actions menu

Latest commit

 

History

18 Commits
18 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
config
config
 
 
src
src
 
 
stubs
stubs
 
 
tests
tests
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
composer-dependency-analyser.php
composer-dependency-analyser.php
 
 
composer.json
composer.json
 
 
ecs.php
ecs.php
 
 
extension.neon
extension.neon
 
 
phpstan.neon.dist
phpstan.neon.dist
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 
rector.php
rector.php
 
 
testbench.yaml
testbench.yaml
 
 

Repository files navigation

Laravel anydoc

Convert documents to GitHub-Flavored Markdown in Laravel applications, with access to anydoc's structured document model when available.

Requirements

  • ext-anydoc 1.0+
  • Laravel 13+
  • PHP 8.4+

Installation

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 documents

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.

Use OCR for scanned PDFs

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 files from disks

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.

Read structured documents

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.

Supported formats

Format Extensions
CSV .csv
EPUB .epub
Excel .xls, .xlsb, .xlsm, .xlsx
OpenDocument .odp, .ods, .odt
PDF .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 formats

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.

Use dependency injection

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();
    }
}

Handle errors

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.

Test conversions

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.

Development

Run the test suite with:

composer test

Changelog

See CHANGELOG.md for a list of changes.

Contributing

Pull requests are welcome. Please run the test suite before submitting changes.

License

The MIT License (MIT). Please see License File for more information.

About

Convert PDFs, Office files, and more to Markdown from Laravel.

Topics

anydocconvertercsvdocumentdocument-conversionepubexcellaravelmarkdownopen-documentpdfphpphp-extensionpowerpointrtfword

Resources

Readme
MIT license
Activity

Stars

5 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.