Platform notes
PHP-FPM and worker processes, memory, packaging and deployment notes for NexPDF for PHP on servers.
Processes and requests
PHP runs each request on one thread, so the usual concern is processes, not threads:
- The native library is loaded once per PHP process, on the first NexPDF call, and stays loaded for the life of that process.
- The license is process-wide. Each PHP-FPM worker, Apache
mod_phpchild, queue worker or CLI run is a separate process and needs its ownactivateLicensecall. Call it in your bootstrap code (for example a service provider or the front controller), which runs in every worker. The key is verified offline, so activating on every request is harmless. - Documents belong to the request that opened them. They are freed when the object is destroyed, at the end of the request at the latest, and cannot be shared between requests or workers. Pass file paths or bytes between processes instead.
- Errors are tracked per thread, and the SDK reads the error right after each call, so an error never belongs to another call.
Under a thread-safe (ZTS) PHP build, a Document is still not thread-safe: use each document
from one thread at a time. Different documents can be used on different threads.
Long-running workers
Queue workers (Laravel queues, Symfony Messenger and similar), RoadRunner, Swoole and other long-running PHP processes handle many jobs in one process:
- activate the license once when the worker starts;
- call
close()on each document when the job is done, so native memory is released at once instead of when the garbage collector destroys the object; - documents that stay open for a long time can call
trimMemory()after large edits.
Memory
- A document is read fully into memory when it is opened.
Document::openreads the file withfile_get_contents, the bytes are copied into a native buffer for the call, and the engine keeps its own copy. Budget a few times the file size while opening, plus the decoded pages you touch. - The engine allocates its own memory outside PHP's memory manager, so
memory_limitdoes not cap it andmemory_get_usage()does not show it. Use$doc->memoryUsageBytes()to estimate a document's native memory, and size the worker count to the memory of the machine. - 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, and stream each PNG to the client or to storage instead of keeping every one.
- The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by
every document in the worker 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 worker that renders many large, image-heavy files. Memory that stays high after
a rendering batch is this cache being reused, not a leak. Recycling workers
(
pm.max_requestsin PHP-FPM) gives the memory back to the system. memoryUsageBytes()andtrimMemory()let long-lived documents give memory back; see Compare and optimise.
Packaging and size
- The package itself is PHP code with no Composer dependencies. The engine is one shared 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). It is not in the Composer package: ship it in your image or server, and point the SDK at it withNEXPDF_LIB_PATH,NEXPDF_LIB_DIRorNative::load. See Installation. - The default engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard
fonts and PDF/A.
NexPdf::hasBundledFonts()tells you which build you have. - The
ext-ffiextension must be installed andffi.enable=trueset for web server SAPIs.
Containers
Use a Debian-based PHP image (for example php:8.3-fpm), not Alpine: the Linux release libraries
need glibc 2.35 or later and there is no musl build. The
Installation page has a Dockerfile. With PHP-FPM, pass
NEXPDF_LIB_PATH or NEXPDF_LIB_DIR and the license key to the workers with env[...] in the pool
configuration, because clear_env drops the container's environment by default.
Responses
Results are binary strings, so you can send them without temporary files:
$pdf = $doc->saveBytes();
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($pdf));
echo $pdf;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 NexPdf::addLtv contact the servers named in the certificates.