PdfInspector (pdf_inspector_ex v0.1.0)

Copy Markdown View Source

Elixir bindings for the pdf-inspector PDF classification / markdown-extraction library (Rustler dirty-CPU NIFs over the shared ffi-core facade).

All functions take the PDF contents as a binary — file IO is the caller's job — and return {:ok, result} or {:error, %PdfInspector.Error{code: code, message: message}} where code is one of :io, :parse, :encrypted, :invalid_structure, :not_a_pdf, :internal_panic.

Page indexing (upstream convention, kept as-is)

  • classify/1 and per-page entries (%PdfInspector.PageMarkdown{page: n}, %PdfInspector.PageOcrReasons{page: n} inside %PdfInspector.PagesResult{}) are 0-indexed. The pages argument of extract_pages/2 is also 0-indexed; nil extracts every page in document order.
  • Process-level page lists (pages_needing_ocr, pages_with_tables, pages_with_columns, and ocr_reasons_by_page in %PdfInspector.Result{}) are 1-indexed.

Requesting an out-of-range page does not error: the entry comes back with empty markdown and needs_ocr: true (upstream placeholder behaviour).

All NIFs run on dirty-CPU schedulers (extraction takes 10–200 ms, far past the 1 ms normal-NIF budget), so calling them never stalls the BEAM.

Summary

Types

PDF contents as an in-memory binary.

Functions

Lightest routing entry: PDF type + confidence + pages needing OCR (0-indexed).

Detection-only pipeline: classification + layout, no markdown (%PdfInspector.Result{markdown: nil}).

Per-page markdown for hybrid pipelines.

Full pipeline: classify the document and extract markdown for every page.

Types

pdf_binary()

@type pdf_binary() :: binary()

PDF contents as an in-memory binary.

Functions

classify(data)

@spec classify(pdf_binary()) ::
  {:ok, PdfInspector.Classification.t()} | {:error, PdfInspector.Error.t()}

Lightest routing entry: PDF type + confidence + pages needing OCR (0-indexed).

detect(data)

@spec detect(pdf_binary()) ::
  {:ok, PdfInspector.Result.t()} | {:error, PdfInspector.Error.t()}

Detection-only pipeline: classification + layout, no markdown (%PdfInspector.Result{markdown: nil}).

extract_pages(data, pages \\ nil)

@spec extract_pages(pdf_binary(), [non_neg_integer()] | nil) ::
  {:ok, PdfInspector.PagesResult.t()} | {:error, PdfInspector.Error.t()}

Per-page markdown for hybrid pipelines.

pages is a list of 0-indexed page numbers; nil (the default) extracts every page in document order. Out-of-range pages come back as empty-markdown / needs_ocr: true placeholders.

process(data)

@spec process(pdf_binary()) ::
  {:ok, PdfInspector.Result.t()} | {:error, PdfInspector.Error.t()}

Full pipeline: classify the document and extract markdown for every page.

iex> {:ok, %PdfInspector.Result{pdf_type: :text_based}} =
...>   PdfInspector.process(File.read!("document.pdf"))