nexdevit / nexpdf
NexPDF for PHP: render, edit, convert, sign and validate PDF documents with the native NexPDF engine (PHP FFI).
Requires
- php: >=8.1
- ext-ffi: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-04 08:48:01 UTC
README
This repository holds the open-source (MIT) bindings of NexPDF, a commercial PDF SDK. The NexPDF engine it calls is proprietary and distributed separately as binaries; viewing works without a key, and every other feature needs a NexPDF license key. The engine for each release is attached to this repository's Releases. Synced from the NexPDF source tree at commit
b61e37f.
The NexPDF engine for PHP 8.1+ through the FFI extension. All 121 functions of the C ABI
(crates/nexpdf-ffi/include/nexpdf.h) are available, generated from the header by
scripts/gen_sdks.py: those that take a document are methods of Nexdevit\NexPdf\Document, the rest are
static methods of Nexdevit\NexPdf\NexPdf. Options and results are the same JSON strings as in every NexPDF
SDK; byte buffers are PHP binary strings.
Requirements
ext-ffiwithffi.enable=truein php.ini for web servers (the default,preload, only allows FFI in the CLI and preloaded scripts).- The native library:
cargo build --release -p nexpdf-ffi. It is loaded fromNexdevit\NexPdf\Native::load($path),$NEXPDF_LIB_PATH, the repository'starget/release, then the system loader.
Install
{
"repositories": [{ "type": "path", "url": "path/to/nexpdf/sdks/php" }],
"require": { "nexdevit/nexpdf": "*" }
}
Quick start
use Nexdevit\NexPdf\Document; use Nexdevit\NexPdf\NexPdf; NexPdf::activateLicense(file_get_contents('license.xml'), 'server', 'com.acme.app'); $doc = Document::open('contract.pdf'); // or Document::openBytes($bytes, $password) echo $doc->pageCount(), " pages\n"; file_put_contents('page1.png', $doc->renderPagePng(0, 144)); echo $doc->extractPageText(0); $doc->fillFormField('quantity', '4'); // runs keystroke, validate and calculate scripts $doc->setMetadata('Q3 report'); // trailing optional strings default to null (NULL) $doc->save('out.pdf', linearized: true); $pdf = Document::fromHtml('<h1>Invoice</h1>'); $report = json_decode(NexPdf::verifySignaturesJson(file_get_contents('signed.pdf')), true);
Conventions
- Failures throw
Nexdevit\NexPdf\NexPdfExceptionwith the engine's message;activateLicensesets the exception code (-1 … -7) for a rejected key. - A document is freed when the object is destroyed, or at once with
close(); a closed document throwsLogicException.copy()gives an independent copy. - Without a license the SDK is view-only and renders carry a watermark;
NexPdf::licenseStatus()andNexPdf::isLicensed('forms')tell what is unlocked.
Collaboration sync
Nexdevit\NexPdf\Sync keeps a document in sync with a NexPDF WebViewer server (protocol in
crates/nexpdf-annot/SYNC.md), like the other SDKs' clients: WebSocket (/api/sync/ws, a built-in
RFC 6455 client over stream sockets, tls:// with certificate and host name checks), else Server-Sent
Events + POST; reconnects with backoff, never after 401/402/403/404. PHP has one thread, so the client has
an explicit event loop: callbacks run inside connect(), poll(), run() and flush(), and the
document is edited between them.
use Nexdevit\NexPdf\{Document, Sync}; // $local: any SyncEngine or Document until the server's copy is attached $sync = new Sync($local, ['url' => 'https://host/s/<session>', 'token' => $token, 'name' => 'Ana']); $doc = Document::openBytes($sync->downloadDocument()); // the server's copy (sets seq) $sync->attach($doc); $sync->on('remote', fn (array $report) => print(count($report['applied']) . " merged\n")); $sync->on('presence', fn (array $peers) => null); $sync->connect(); // true once the server said hello $doc->addAnnotation(0, 'Square', '#FF0000', 1.0, 'note', 50, 50, 150, 120); $sync->flush(); // or let poll()/run() push every flushInterval (1 s) $sync->setPresence(0, ['x' => 10, 'y' => 20]); $sync->run(null, 30.0); // serve events for 30 s; poll($timeout) for one turn $sync->close();
Options: url, clientId (random UUID), name, color, token, since, flushInterval (seconds,
0 = only flush()), reconnectDelay (1 s, doubling to 30 s), connectTimeout, transport
(auto, websocket, sse), ssl (extra stream context options such as cafile). Events: remote,
pushed, conflict, presence, status, reload, error. A Document is used through
DocumentSyncEngine; anything implementing SyncEngine works too.
Time-stamp authority callback
Instead of a tsa_url, the application can get the time-stamps itself (nexpdf_set_tsa_callback):
the callback receives the DER RFC 3161 TimeStampReq and returns the DER TimeStampResp (or token).
It is registered process-wide and used by every call that needs a time-stamp and has no tsa_url
(B-T / B-LT / B-LTA signatures, document time-stamps); a tsa_url always wins.
NexPdf::setTsaCallback(function (string $request): string { return myTsa()->post('/tsa', $request, ['Content-Type' => 'application/timestamp-query']); }); $signed = $doc->signPkcs12($p12, $password, '{"level":"B-T"}'); NexPdf::setTsaCallback(null); // unregister
A throw or an empty answer fails the NexPDF call with "TSA callback failed";
NexPdf::tsaCallbackError() holds the exception. The callback runs inside the NexPDF call, on the
calling thread.
Changes
- 1.1 (breaking): string parameters the engine requires no longer have a default. (
fillFormField($name, $value),setAnnotationContents,addWatermark,searchJson,importXfdf,attachmentData, ...) Omitting one used to reach the engine as NULL and fail at run time with "X is NULL"; now it does not run. Pass the value (an empty string is a value). Parameters with a meaning for "absent" (options JSON, passwords,set_metadatafields, theadd_batesprefix, ...) keep their default. Which is which:STRING_PARAMSinscripts/gen_sdks.py.
Tests
cd sdks/php && php tests/run.php php tests/sync.php # the sync client against a scripted server, stub engine NEXPDF_E2E_NO_BUILD=1 sh scripts/sync-e2e.sh php # from the repository root: real engine and server NEXPDF_E2E_NO_BUILD=1 sh scripts/tsa-callback-e2e.sh php # the TSA callback with the test TSA