ExHtmltopdf.Options (ExHtmltopdf v0.1.0)

Copy Markdown View Source

Turns an Elixir options keyword list into the CLI argv the native engine consumes.

sghtmltopdf exposes one option surface — the CLI flags — shared by the CLI, the HTTP server, and the Ruby gem. This module is the Elixir equivalent of the gem's options layer: option names are the CLI flag names with underscores (:page_size--page-size), and no option semantics live on this side. Run sghtmltopdf --help (or see the upstream docs) for the full list.

Conversion rules:

page_size: "A4"           ["--page-size", "A4"]
grayscale: true           ["--grayscale"]
grayscale: false          []  (also for nil)
allow: ["/a", "/b"]       ["--allow", "/a", "--allow", "/b"]
margin_top: 20            ["--margin-top", "20"]

:font is the one special case, because --font-index binds to the preceding --font by position:

font: "a.ttf"                       ["--font", "a.ttf"]
font: %{path: "a.ttc", index: 1}    ["--font", "a.ttc", "--font-index", "1"]
font: ["a.ttf", %{path: "b.ttc", index: 2}]
   ["--font", "a.ttf", "--font", "b.ttc", "--font-index", "2"]

Two keys are rejected: :input and :output are owned by the NIF (the HTML bytes cross the boundary directly, and the output destination is the return value or the render_to_file/3 path), so passing them would be silently ignored — an error is clearer.

Charlists are also rejected: a charlist is indistinguishable from a "repeat this option" list of values, so title: ~c"Report" would silently become per-character flags. Pass binaries ("Report").

Summary

Functions

Builds the argv list for the native option parser.

Functions

to_argv(options)

@spec to_argv(keyword() | map()) :: [String.t()]

Builds the argv list for the native option parser.

iex> ExHtmltopdf.Options.to_argv(page_size: "A4", grayscale: true)
["sghtmltopdf", "-", "--output", "-", "--page-size", "A4", "--grayscale"]

iex> ExHtmltopdf.Options.to_argv([])
["sghtmltopdf", "-", "--output", "-"]