PDFium

Elixir bindings for the pdfium library.

Installing

Add the dependency:

{:pdfium, "~> 0.2"}

Or in a script:

Mix.install([pdfium: "~> 0.2"])

Precompiled NIFs cover OTP 26 and newer on x86_64 and aarch64 Linux, glibc or musl, and on arm64 macOS. Anywhere else the package compiles from source and needs a C++ compiler, make and curl.

Usage

Open a document:

{:ok, document} = PDFium.open_document("file.pdf")

Count its pages:

{:ok, pages} = PDFium.get_page_count(document)

Render a page to a file, with Vix:

{:ok, {binary, w, h}} = PDFium.get_page_bitmap(document, 0, 300)
{:ok, image} = Vix.Vips.Image.new_from_binary(binary, w, h, 4, :VIPS_FORMAT_UCHAR)
{:ok, _image} = Image.write(image, "/tmp/sample.png")

Development

Enter the shell, then use mix as usual:

nix develop
mix compile
mix test
mix dialyzer
mix docs

Running tests

Run the tests for every target this machine can run, and dialyzer:

nix flake check

The visual tests compare renders with the images in test/fixtures/visual. After a change that is meant to draw differently, rewrite them and review the image diff:

UPDATE_VISUALS=1 mix test --only visual

Build the NIF for a target, or the release archives:

nix build .#nif-aarch64-linux-musl
nix build .#archives

Releasing

Every Monday a workflow opens a pull request that updates libpdfium and bumps the version. Merging it releases. To do that now:

gh workflow run update-libpdfium.yaml

To update to a specific libpdfium tag:

nix run .#update -- chromium/8138

Every push to main that touches mix.exs releases, so bump @version with it. If a release fails, bump @version again. Tags cannot be reused.

License

See LICENSE.