PhoenixKit.Modules.Storage.PdfProcessor (phoenix_kit v2.26.0)

Copy Markdown View Source

Poppler-based PDF processing module.

Handles PDF operations using poppler-utils command-line tools:

  • pdftoppm - Convert PDF pages to images (JPEG)
  • pdfinfo - Extract PDF metadata (page count, author, title)

Dependencies

Requires poppler-utils to be installed:

  • Debian/Ubuntu: apt-get install poppler-utils
  • macOS: brew install poppler

Summary

Functions

Extract metadata from a PDF file using pdfinfo.

Convert the first page of a PDF to a JPEG image.

Functions

extract_metadata(pdf_path)

Extract metadata from a PDF file using pdfinfo.

Parameters

  • pdf_path - Path to the PDF file

Returns

  • {:ok, metadata} - Map with extracted metadata
  • {:ok, %{}} - Empty map on failure (graceful degradation)

file_attrs(existing_metadata, pdf_metadata)

@spec file_attrs(map() | nil, map()) :: %{metadata: map()}

Turn extract_metadata/1's result into attrs for PhoenixKit.Modules.Storage.File.changeset/2.

The pdfinfo fields ("page_count", "title", "author", "creator", "creation_date") are string-keyed and are not columns on the file row — they belong inside its :metadata map. Returning them at the top level next to the atom-keyed :status the job adds gives Ecto a mixed-key map, which cast/3 rejects with Ecto.CastError — that crashed every PDF job on a host with poppler.

They only fill keys the map doesn't already hold. "title" is also the user-editable title the media detail page saves into the same map, so a pdfinfo value merged over it would replace the user's title each time the file is processed again.

first_page_to_jpeg(pdf_path, output_prefix, opts \\ [])

Convert the first page of a PDF to a JPEG image.

Uses pdftoppm to render the first page at the specified DPI.

Parameters

  • pdf_path - Path to the input PDF file
  • output_prefix - Prefix for the output JPEG file (e.g., "/tmp/my_pdf")
  • opts - Options
    • :dpi - Resolution in DPI (default: 150)

Returns

  • {:ok, jpeg_path} - Path to the generated JPEG file
  • {:error, reason} - If conversion fails