Skip to Content
NexPDF

Installation

Add the NexPDF Go module, point cgo at the native engine library, and link it shared or static.

English

Add the module

go get github.com/nexdevit/nexpdf-go@v1.1.0

Import it as nexpdf "github.com/nexdevit/nexpdf-go". The module uses cgo, so builds need CGO_ENABLED=1 (the default when a C compiler is installed) and a C compiler such as gcc or clang.

Get the native library

Download the archive for your platform from the v1.1.0 release. Each archive holds a folder nexpdf-ffi-1.1.0-<platform> with include/nexpdf.h and the shared and static libraries in lib/:

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_64nexpdf-ffi-1.1.0-windows-x64.ziplib/nexpdf_ffi.dll, lib/nexpdf_ffi.dll.lib, lib/nexpdf_ffi.lib
tar -xzf nexpdf-ffi-1.1.0-linux-x64.tar.gz -C /opt

Tell cgo where the library is

The module's cgo directives look for the header and the library inside the module folder, which is read-only in the Go module cache. Point cgo at the extracted archive instead, with an rpath so the program finds the shared library at run time:

export CGO_ENABLED=1
export CGO_CFLAGS="-I/opt/nexpdf-ffi-1.1.0-linux-x64/include"
export CGO_LDFLAGS="-L/opt/nexpdf-ffi-1.1.0-linux-x64/lib -Wl,-rpath,/opt/nexpdf-ffi-1.1.0-linux-x64/lib"

When you ship the program, ship the shared library with it and set the rpath to where it is installed, or use LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (macOS).

Check the installation

main.go
package main

import (
	"fmt"

	nexpdf "github.com/nexdevit/nexpdf-go"
)

func main() {
	fmt.Println(nexpdf.Version())
}
go run .

Static linking

Linking the static libnexpdf_ffi.a gives a binary with no run-time dependency on the NexPDF library. The linker prefers the shared library when both are in the same folder, so put only libnexpdf_ffi.a in the folder you pass with -L, and add the system libraries it needs:

mkdir -p /opt/nexpdf-static && cp /opt/nexpdf-ffi-1.1.0-linux-x64/lib/libnexpdf_ffi.a /opt/nexpdf-static/
export CGO_CFLAGS="-I/opt/nexpdf-ffi-1.1.0-linux-x64/include"
export CGO_LDFLAGS="-L/opt/nexpdf-static -lpthread -ldl -lm"
go build .

Docker

Dockerfile
FROM golang:1.22-bookworm AS build
ADD https://github.com/nexdevit/nexpdf-go/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 CGO_ENABLED=1 \
    CGO_CFLAGS=-I/opt/nexpdf-ffi-1.1.0-linux-x64/include \
    CGO_LDFLAGS="-L/opt/nexpdf-ffi-1.1.0-linux-x64/lib -Wl,-rpath,/opt/nexpdf-ffi-1.1.0-linux-x64/lib"
WORKDIR /src
COPY . .
RUN go build -o /app .

FROM debian:bookworm-slim
COPY --from=build /opt/nexpdf-ffi-1.1.0-linux-x64/lib/libnexpdf_ffi.so /opt/nexpdf-ffi-1.1.0-linux-x64/lib/
COPY --from=build /app /app
ENTRYPOINT ["/app"]

On Windows, cgo builds with a MinGW toolchain, while the release's Windows library is built with MSVC. Linking it from Go on Windows is not covered by these instructions.

On this page