Platform notes
Threading, memory ownership, linking, binary size and deployment notes for NexPDF for C and C++ on servers and desktops.
Threading
- A
Document(anNexPdfDocument*in C) is not thread-safe. Use each document from one thread at a time, or guard it with astd::mutex. - Different documents can be used on different threads at the same time, so threads scale across cores for independent files. The state shared between documents (the license and the renderer's caches) is thread-safe.
doc.copy()gives an independent document you can move to another thread.Documentis move-only, so moving it into a worker transfers ownership.- The license is process-wide: one
activate_licensecall covers every thread. - Errors are tracked per thread, so an error on one thread never leaks into another. In C, read
nexpdf_last_error()on the thread that made the call.
#include <future>
#include <nexpdf.hpp>
namespace nexpdf = nexdevit::nexpdf;
std::vector<std::vector<uint8_t>> render_first_pages(const std::vector<std::string>& paths) {
std::vector<std::future<std::vector<uint8_t>>> jobs;
for (const auto& path : paths) {
jobs.push_back(std::async(std::launch::async, [path] {
auto doc = nexpdf::Document::open(path); // one document per task
return doc.render_page_png(0, 144);
}));
}
std::vector<std::vector<uint8_t>> pngs;
for (auto& job : jobs) pngs.push_back(job.get()); // rethrows nexpdf::Error
return pngs;
}In a GUI application, keep rendering and conversion off the UI thread: run them on a worker thread and pass the PNG bytes back to the UI thread to display.
Memory
- A document is read fully into memory when it is opened. Budget roughly the file size plus the
decoded pages you touch.
Document::openreads the file into astd::vector, and the engine keeps its own copy, so the vector can go once the document is open. - A rendered page at 144 dpi of an A4 page is about 1190 x 1684 pixels before PNG compression. Render large documents page by page instead of keeping every PNG.
- The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a logo or font used in many files is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB in a process that renders many large, image-heavy files. Memory that stays high after a rendering batch is this cache being reused, not a leak.
- In C++,
Documentfrees itself in its destructor and returned strings and buffers are copied intostd::stringandstd::vector, so there is nothing to free by hand. In C, free every result: strings withnexpdf_free_string,NexPdfByteswithnexpdf_free_bytes, documents withnexpdf_free. memory_usage_bytes()andtrim_memory()let long-lived documents give memory back; see Compare and optimise.
Linking and size
nexpdf.hppis header-only and needs C++17. Only the C ABI crosses the library boundary, so it works with any C++17 compiler and standard library, and C programs usenexpdf.h(C99) directly.- The engine is one native library per platform:
libnexpdf_ffi.so,libnexpdf_ffi.dylibornexpdf_ffi.dll, about 20 MB for one architecture (the macOS library is universal and holds two). - Link it shared and ship it with your program, or link the static library (
libnexpdf_ffi.a,nexpdf_ffi.lib) with-DNEXPDF_STATIC=ONfor a binary with no run-time dependency on NexPDF. See Installation. - The static library removes the dependency on
libnexpdf_ffi, not on the C library: Linux programs still need glibc 2.35 or later. There is no musl build. - The default engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard
fonts and PDF/A.
nexpdf::has_bundled_fonts()tells you which build you have.
Deployment
| Platform | Ship | So the program finds it |
|---|---|---|
| Linux | libnexpdf_ffi.so | An rpath ($ORIGIN for the executable's folder) or LD_LIBRARY_PATH |
| macOS | libnexpdf_ffi.dylib | An rpath, such as @executable_path; the install name is @rpath/libnexpdf_ffi.dylib |
| Windows | nexpdf_ffi.dll | Next to the executable, or in a folder on PATH |
With the static library there is nothing to ship besides your executable. In containers, use a
glibc-based image (for example debian:bookworm-slim or ubuntu:22.04).
Network
The engine makes no network calls of its own: license keys are verified offline and documents
never leave the process. Only signing options you set (tsa_url for a timestamp, ltv for OCSP
and CRL lookups) and add_ltv contact the servers named in the certificates.