Converts documents between formats for LLM consumption.
Currently supports PDF to PNG conversion for the first page. The default adapter bounds the source and result sizes, conversion duration, diagnostic output, and ImageMagick resources.
Usage
PDF-to-image conversion is used for:
- OpenAI: Chat API only supports images (PDFs work in Assistants API)
- Ollama: Vision models require image formats
Native PDF support:
- Anthropic Claude 4.5+: Supports PDFs via document API
Requirements
Requires ImageMagick to be installed:
- macOS:
brew install imagemagick - Ubuntu/Debian:
apt-get install imagemagick - Docker: Install in runtime image
Configuration
The conversion adapter can be configured in config files:
config :aludel, :document_converter,
adapter: Aludel.Interfaces.DocumentConverter.Adapters.Imagemagick,
density: 150,
timeout_ms: 30_000,
max_input_bytes: 10_485_760,
max_output_bytes: 20_971_520,
max_diagnostic_bytes: 16_384density accepts values from 72 through 300. Size and diagnostic limits can
be lowered from their defaults, while timeout_ms accepts 100 through 60,000.
An explicit :executable path and an existing :temporary_directory can also
be supplied by trusted application configuration.
For testing, use a stub adapter.
Summary
Functions
Converts a PDF document to PNG format.
Types
Functions
@spec pdf_to_image(document()) :: convert_result()
Converts a PDF document to PNG format.
Only converts the first page at 150 DPI for optimal text readability. Creates temporary files for conversion and cleans them up afterwards.
Examples
iex> pdf_doc = %{data: pdf_binary, content_type: "application/pdf"}
iex> {:ok, png_doc} = DocumentConverter.pdf_to_image(pdf_doc)
iex> png_doc.content_type
"image/png"
iex> image_doc = %{data: png_binary, content_type: "image/png"}
iex> {:ok, ^image_doc} = DocumentConverter.pdf_to_image(image_doc)
:ok