Skip to Content
NexPDF

Installation

Add the nexpdf-sdk crate, download the native NexPDF engine and link it statically or dynamically, on desktop, iOS and Android.

English

Add the crate

Cargo.toml
[dependencies]
nexpdf-sdk = "1.1"                                        # once published on crates.io
# with serde_json::Value results (the *_json_value functions):
# nexpdf-sdk = { version = "1.1", features = ["serde"] }

The crate is imported as nexpdf_sdk. Until the crate is on crates.io, use the Git dependency from the public repository nexdevit/nexpdf-rust.

Most examples in these docs use the serde feature for the *_json_value functions, and serde_json itself for its json! macro:

Cargo.toml
[dependencies]
nexpdf-sdk = { version = "1.1", features = ["serde"] }
serde_json = "1"

The sync feature adds nexpdf_sdk::sync, the client for a NexPDF collaboration server (see Real-time collaboration): features = ["serde", "sync"].

Get the native library

The engine is distributed as binaries. Download the archive for your platform from the v1.1.0 release of nexdevit/nexpdf-rust. Its version must match the crate's.

PlatformArchiveLibrary
Linux x86_64nexpdf-ffi-1.1.0-linux-x64.tar.gzlib/libnexpdf_ffi.so, lib/libnexpdf_ffi.a
Linux arm64nexpdf-ffi-1.1.0-linux-arm64.tar.gzlib/libnexpdf_ffi.so, lib/libnexpdf_ffi.a
macOSnexpdf-ffi-1.1.0-macos-universal.tar.gzlib/libnexpdf_ffi.dylib, lib/libnexpdf_ffi.a
Windows x86_64 (MSVC)nexpdf-ffi-1.1.0-windows-x64.ziplib/nexpdf_ffi.dll with the import library nexpdf_ffi.dll.lib, static lib/nexpdf_ffi.lib
iOSNexPdfCore.xcframework.zipa static libnexpdf_ffi.a per slice
Androidnexpdf-android-1.1.0.aarjni/<abi>/libnexpdf_ffi.so
v=1.1.0
gh release download "v$v" --repo nexdevit/nexpdf-rust --pattern "nexpdf-ffi-$v-linux-x64.tar.gz" --pattern SHA256SUMS.txt
sha256sum -c --ignore-missing SHA256SUMS.txt
tar -xzf "nexpdf-ffi-$v-linux-x64.tar.gz" -C /opt

Builds never download anything. There is no download feature: fetching over HTTPS at build time would need a TLS stack with C code, and offline, reproducible builds are better served by an unpacked archive.

Point the build at the library

nexpdf-sys looks for the library in this order:

  1. NEXPDF_LIB_DIR_<target>, with the target triple's dashes as underscores (for example NEXPDF_LIB_DIR_aarch64_linux_android), for cross-compiling several targets;
  2. NEXPDF_LIB_DIR;
  3. NEXPDF_ROOT/lib, NEXPDF_ROOT being an unpacked nexpdf-ffi-<version>-<platform> folder;
  4. when not cross-compiling, the usual prefixes: /opt/nexpdf/lib, /usr/local/lib, /opt/homebrew/lib, /usr/lib (%ProgramFiles%\NexPDF\lib on Windows).
export NEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cargo build

If it finds nothing, the build stops with a message saying what to download and which variable to set. NEXPDF_NO_LINK=1 (and docs.rs) skips linking, for cargo doc without the engine.

Choose static or dynamic linking

PlatformDefaultNEXPDF_STATIC=1
Linux x64 / arm64sharedstatic, no run-time dependency
macOS (universal)sharedstatic (links CoreFoundation and Security)
Windows x64 (MSVC)DLLstatic
iOSstaticstatic
Androidsharednot available

Without NEXPDF_STATIC, the shared library is used when the folder has it and the static one otherwise; NEXPDF_STATIC=0 insists on the shared library. Static linking gives a self-contained executable that needs nothing at run time.

A shared library must be found again when the program runs:

  • Linux and macOS: add an rpath, for example in .cargo/config.toml:

    .cargo/config.toml
    [build]
    rustflags = ["-C", "link-arg=-Wl,-rpath,/opt/nexpdf-ffi-1.1.0-linux-x64/lib"]

    Use -Wl,-rpath,$ORIGIN (Linux) or @executable_path (macOS) to ship the library next to the executable, or set LD_LIBRARY_PATH / DYLD_LIBRARY_PATH.

  • Windows: put nexpdf_ffi.dll next to the .exe or on PATH.

Check the installation

src/main.rs
fn main() {
    println!("NexPDF {}", nexpdf_sdk::version());
}
cargo run

iOS and Android

Cross-compiling takes the library built for the target, never the host's:

# iOS: the slices of NexPdfCore.xcframework (attached to the same release) are static libraries.
unzip NexPdfCore.xcframework.zip
export NEXPDF_LIB_DIR_aarch64_apple_ios=$PWD/NexPdfCore.xcframework/ios-arm64
export NEXPDF_LIB_DIR_aarch64_apple_ios_sim=$PWD/NexPdfCore.xcframework/ios-arm64_x86_64-simulator
cargo build --target aarch64-apple-ios

# Android: the AAR (attached to the same release) holds one libnexpdf_ffi.so per ABI.
unzip nexpdf-android-1.1.0.aar 'jni/*' -d nexpdf-android
export NEXPDF_LIB_DIR_aarch64_linux_android=$PWD/nexpdf-android/jni/arm64-v8a
export NEXPDF_LIB_DIR_x86_64_linux_android=$PWD/nexpdf-android/jni/x86_64
cargo ndk -t arm64-v8a -t x86_64 build    # or your own NDK linker setup

On Android, package libnexpdf_ffi.so with your library in the APK's jniLibs/<abi>/. Slice folder names inside the XCFramework follow its Info.plist. See Platform notes.

Docker

A static build needs nothing at run time, so the final image only holds the executable:

Dockerfile
FROM rust:1-bookworm AS build
ADD https://github.com/nexdevit/nexpdf-rust/releases/download/v1.1.0/nexpdf-ffi-1.1.0-linux-x64.tar.gz /tmp/
RUN tar -xzf /tmp/nexpdf-ffi-1.1.0-linux-x64.tar.gz -C /opt
ENV NEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib NEXPDF_STATIC=1
WORKDIR /src
COPY . .
RUN cargo build --release

FROM debian:bookworm-slim
COPY --from=build /src/target/release/my-app /usr/local/bin/my-app
CMD ["my-app"]

On this page