Perplexed.PdfConverter behaviour (Perplexed v0.3.0)

Copy Markdown View Source

The behaviour that turns PDF bytes into Markdown for a scrape.

Configure an implementation with config :perplexed, pdf_converter: Module. Without one, PDF pages scrape as {:error, :unsupported}.

Page budgets

A page budget is opt-in. The library defaults to pdf_max_pages: nil (unlimited) and pdf_over_limit: :skip. Configure these settings on Perplexed, or override them on each scrape or answer:

Perplexed.scrape(url, pdf_max_pages: 20)
Perplexed.answer(question, pdf_max_pages: 20, pdf_over_limit: :first_pages)

Budgeted converters must implement page_count(binary, filename, opts), returning {:ok, positive_integer} or {:error, atom}. For :first_pages, also implement convert(binary, filename, opts) and honor opts[:max_pages] by converting only that prefix, never by converting the entire document and trimming its text. Both callbacks receive opts[:timeout_ms], capped by the scrape's remaining deadline. Under-limit files may use legacy convert/2; unlimited requests continue using that callback.

An over-limit skip returns :pdf_page_limit; unknown counts return :pdf_page_count_unavailable; a converter unable to extract the prefix returns :pdf_page_limit_unsupported. These outcomes stop the scrape ladder. Scrape errors expose them in {:error, {:unreadable, %{attempts: ...}}}. Documents and answer sources include page_count, pages_read, and truncated. The summarizer is told when it has only a prefix. PdfConverter.read/3 returns {:ok, markdown, coverage}; convert/3 retains its {:ok, markdown} result.

Active budgets bypass all search-provided page content, including extensionless URLs. Only scraper modules declaring supports_pdf_budget?/0 as true run. Direct and impersonated HTTP count PDF bytes before conversion; Firecrawl is restricted to HTML with parsers: [] and rejects PDF responses. Hosted DeepSeek and Browser Run are skipped. Rung options cannot override the resolved policy. For answers, top-level budget options override scrape_options, which override application settings. An explicit pdf_max_pages: nil restores unlimited mode.

Failure atoms that mean no other scraper can help (:needs_ocr, :encrypted, :unsupported) end the scrape ladder.

Summary

Callbacks

Converts PDF binary (fetched from a URL ending in filename) to Markdown.

Converts only opts[:max_pages] when present; timeout_ms bounds work.

Counts pages without converting the document.

Functions

Converts binary with opts[:pdf_converter], else the configured converter.

Reads a PDF with a validated budget and returns coverage metadata.

Callbacks

convert(binary, filename)

@callback convert(binary(), filename :: String.t()) ::
  {:ok, String.t()} | {:error, atom()}

Converts PDF binary (fetched from a URL ending in filename) to Markdown.

convert(binary, t, keyword)

(optional)
@callback convert(binary(), String.t(), keyword()) :: {:ok, String.t()} | {:error, atom()}

Converts only opts[:max_pages] when present; timeout_ms bounds work.

page_count(binary, t, keyword)

(optional)
@callback page_count(binary(), String.t(), keyword()) ::
  {:ok, pos_integer()} | {:error, atom()}

Counts pages without converting the document.

Functions

convert(binary, filename, opts \\ [])

@spec convert(binary(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, atom() | {:invalid_request, map()}}

Converts binary with opts[:pdf_converter], else the configured converter.

read(binary, filename, opts)

@spec read(binary(), String.t(), keyword()) ::
  {:ok, String.t(), map()} | {:error, term()}

Reads a PDF with a validated budget and returns coverage metadata.