NativeElixirPdfUtilities.Merge (native_elixir_pdf_utilities v0.6.0)

View Source

PDF utilities for merging documents through the shared native reader.

Notes and constraints:

  • Emits a classic PDF 1.7 header and builds a fresh xref + trailer.
  • Resolves classic xref tables, xref streams, and object streams before copying active objects with fresh identifiers.
  • Adjusts indirect references (n g R) to the new numbering.
  • Collects Page objects and builds a new Catalog + Pages tree that references them.
  • Leaves stream bytes untouched and preserves declared /Length (direct or indirect ref), only renumbering indirect references as needed.

The merger is conservative and pragmatic, targeting structural correctness for common PDFs.

Summary

Types

Mapping from original object id to new object id.

Object record captured while indexing inputs.

Byte-offset table for xref: object id -> {byte_offset, generation}.

A single token as produced by NativeElixirPdfUtilities.Tokenizer.

A list of PDF tokens.

Functions

Merge a list of PDF binaries into a single PDF binary.

Types

error_reason()

@type error_reason() :: :empty_pdf_list | :invalid_pdf_input

id_map()

@type id_map() :: %{optional(integer()) => integer()}

Mapping from original object id to new object id.

obj_rec()

@type obj_rec() :: %{obj: integer(), gen: integer(), tokens: tokens()}

Object record captured while indexing inputs.

offsets_map()

@type offsets_map() :: %{
  optional(integer()) => {non_neg_integer(), non_neg_integer()}
}

Byte-offset table for xref: object id -> {byte_offset, generation}.

pdf_bin()

@type pdf_bin() :: binary()

token()

A single token as produced by NativeElixirPdfUtilities.Tokenizer.

tokens()

@type tokens() :: [token()]

A list of PDF tokens.

Functions

merge(bins)

@spec merge([pdf_bin()]) ::
  {:ok, pdf_bin()}
  | {:error,
     {error_reason(), NativeElixirPdfUtilities.Diagnostics.diagnostic()}}

Merge a list of PDF binaries into a single PDF binary.

It resolves active objects through the shared reader, renumbers them, collects Page objects, and emits a new Catalog/Pages tree referencing all input pages.