Installation
Add the nexpdf-sdk crate, download the native NexPDF engine and link it statically or dynamically, on desktop, iOS and Android.
Add the crate
[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:
[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.
| Platform | Archive | Library |
|---|---|---|
| Linux x86_64 | nexpdf-ffi-1.1.0-linux-x64.tar.gz | lib/libnexpdf_ffi.so, lib/libnexpdf_ffi.a |
| Linux arm64 | nexpdf-ffi-1.1.0-linux-arm64.tar.gz | lib/libnexpdf_ffi.so, lib/libnexpdf_ffi.a |
| macOS | nexpdf-ffi-1.1.0-macos-universal.tar.gz | lib/libnexpdf_ffi.dylib, lib/libnexpdf_ffi.a |
| Windows x86_64 (MSVC) | nexpdf-ffi-1.1.0-windows-x64.zip | lib/nexpdf_ffi.dll with the import library nexpdf_ffi.dll.lib, static lib/nexpdf_ffi.lib |
| iOS | NexPdfCore.xcframework.zip | a static libnexpdf_ffi.a per slice |
| Android | nexpdf-android-1.1.0.aar | jni/<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 /optBuilds 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:
NEXPDF_LIB_DIR_<target>, with the target triple's dashes as underscores (for exampleNEXPDF_LIB_DIR_aarch64_linux_android), for cross-compiling several targets;NEXPDF_LIB_DIR;NEXPDF_ROOT/lib,NEXPDF_ROOTbeing an unpackednexpdf-ffi-<version>-<platform>folder;- when not cross-compiling, the usual prefixes:
/opt/nexpdf/lib,/usr/local/lib,/opt/homebrew/lib,/usr/lib(%ProgramFiles%\NexPDF\libon Windows).
export NEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cargo buildIf 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
| Platform | Default | NEXPDF_STATIC=1 |
|---|---|---|
| Linux x64 / arm64 | shared | static, no run-time dependency |
| macOS (universal) | shared | static (links CoreFoundation and Security) |
| Windows x64 (MSVC) | DLL | static |
| iOS | static | static |
| Android | shared | not 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 setLD_LIBRARY_PATH/DYLD_LIBRARY_PATH. -
Windows: put
nexpdf_ffi.dllnext to the.exeor onPATH.
Check the installation
fn main() {
println!("NexPDF {}", nexpdf_sdk::version());
}cargo runiOS 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 setupOn 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:
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"]