Skip to Content
NexPDF

Installation

Add the NexPDF C++ header to a CMake or other build, link the native engine library shared or static, and call it from plain C.

English

Get the headers

git clone --branch v1.1.0 https://github.com/nexdevit/nexpdf-cpp

The repository holds nexpdf.hpp and the C header nexpdf.h in include/, and a CMakeLists.txt that defines the nexdevit::nexpdf target.

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 with its import library lib/nexpdf_ffi.dll.lib, and the static lib/nexpdf_ffi.lib

Add it to your build

CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(app LANGUAGES CXX)

add_subdirectory(third_party/nexpdf-cpp)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE nexdevit::nexpdf)

The library is looked up in NEXPDF_LIB_DIR: the environment variable of that name, else the lib/ folder of nexpdf-cpp. It is a cache variable, so you can also pass it on the command line:

cmake -S . -B build -DNEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib
cmake --build build

nexdevit::nexpdf sets C++17, both include folders and the library to link.

Check the installation

main.cpp
#include <iostream>
#include <nexpdf.hpp>

int main() {
    std::cout << nexdevit::nexpdf::version() << "\n";
}

Static linking

-DNEXPDF_STATIC=ON links the static library (libnexpdf_ffi.a, or nexpdf_ffi.lib on Windows) for a binary with no run-time dependency on the NexPDF library. The target adds the system libraries it needs: -framework Security -framework CoreFoundation on macOS, pthread, dl and m on Linux, and ws2_32, userenv, bcrypt, ntdll, advapi32, crypt32 and secur32 on Windows.

cmake -S . -B build -DNEXPDF_STATIC=ON -DNEXPDF_LIB_DIR=/opt/nexpdf-ffi-1.1.0-linux-x64/lib

Shipping the shared library on macOS

The shared library's install name is @rpath/libnexpdf_ffi.dylib, so the program needs an rpath to the folder you ship it in, for example next to the executable:

set_target_properties(app PROPERTIES BUILD_RPATH "${NEXPDF_LIB_DIR}" INSTALL_RPATH "@executable_path")

On Linux add an rpath the same way or set LD_LIBRARY_PATH; on Windows put nexpdf_ffi.dll next to the executable.

Plain C

C programs include nexpdf.h and link the same library. Strings returned by the engine are freed with nexpdf_free_string, byte buffers (NexPdfBytes) with nexpdf_free_bytes and documents with nexpdf_free. A failed call returns NULL, an empty buffer or 0, and nexpdf_last_error() gives the message.

main.c
#include <stdio.h>
#include <stdlib.h>
#include "nexpdf.h"

int main(int argc, char **argv) {
    FILE *f = fopen(argc > 1 ? argv[1] : "sample.pdf", "rb");
    if (!f) return 1;
    fseek(f, 0, SEEK_END);
    long len = ftell(f);
    fseek(f, 0, SEEK_SET);
    uint8_t *data = malloc((size_t)len);
    fread(data, 1, (size_t)len, f);
    fclose(f);

    NexPdfDocument *doc = nexpdf_open_bytes(data, (size_t)len);
    free(data);
    if (!doc) {
        char *err = nexpdf_last_error();
        fprintf(stderr, "open failed: %s\n", err ? err : "unknown error");
        nexpdf_free_string(err);
        return 1;
    }

    printf("%d pages\n", nexpdf_page_count(doc));

    char *text = nexpdf_extract_page_text(doc, 0);
    if (text) {
        puts(text);
        nexpdf_free_string(text);
    }

    NexPdfBytes png = nexpdf_render_page_png(doc, 0, 144);
    if (png.ptr) {
        FILE *out = fopen("page1.png", "wb");
        fwrite(png.ptr, 1, png.len, out);
        fclose(out);
        nexpdf_free_bytes(png);
    }

    nexpdf_free(doc);
    return 0;
}
cc main.c -I /opt/nexpdf-ffi-1.1.0-linux-x64/include \
   -L /opt/nexpdf-ffi-1.1.0-linux-x64/lib -lnexpdf_ffi \
   -Wl,-rpath,/opt/nexpdf-ffi-1.1.0-linux-x64/lib -o app

On this page