Skip to content
Hosmel Quintana

Formats and Output

Name or detect a document's format, and choose between Markdown and anydoc's document model.

The package detects each document's format unless you name it, and converts it to Markdown or to anydoc's document model.

Formats

Without a format, the package detects it from the content, then from the extension: the path's for local and disk files, and the client's file name for uploads. Most formats carry a signature, so a .docx file with the wrong extension still converts. CSV has no signature, so CSV bytes, or a CSV file without the .csv extension, need a format named with .format():

rust
use laravel::prelude::*;
use laravel_anydoc::Anydoc;
use laravel_anydoc::anydoc::Format;

pub async fn export(anydoc: Inject<Anydoc>, csv: Vec<u8>) -> laravel::Result<(String, String)> {
    let export = anydoc
        .file("storage/app/export.txt")
        .format(Format::Csv)
        .markdown()
        .await?;
    let table = anydoc.bytes(csv).format(Format::Csv).markdown().await?;

    Ok((export, table))
}

To detect a format yourself, call Format::from_bytes, Format::from_extension, or Format::from_path.

Markdown and documents

.markdown() returns GitHub-Flavored Markdown as a String. .document() returns anydoc's Document model, with the content's blocks, notes, and assets:

rust
use laravel::prelude::*;
use laravel_anydoc::Anydoc;
use laravel_anydoc::anydoc::model::Document;

pub async fn slides(anydoc: Inject<Anydoc>) -> laravel::Result<usize> {
    let document: Document = anydoc.file("storage/app/slides.pptx").document().await?;

    Ok(document.blocks.len())
}

PDFs convert to Markdown only: they have no document model, so .document() fails for them.