Getting started
Activate a license, open a PDF, count its pages, render a page to PNG and extract its text with NexPDF for PHP.
This example opens a PDF, prints the page count, renders the first page to a PNG file and prints the text of every page. It runs without a license key too: viewing is always allowed, and rendered pages then carry a watermark.
Install the SDK
composer require nexdevit/nexpdfEnable FFI and make the native library available as described in Installation.
Write the script
<?php
require __DIR__ . '/vendor/autoload.php';
use Nexdevit\NexPdf\Document;
use Nexdevit\NexPdf\NexPdf;
// 1. Activate the license once per process (optional for viewing).
$licenseFile = getenv('NEXPDF_LICENSE');
if ($licenseFile) {
NexPdf::activateLicense(file_get_contents($licenseFile), 'server', 'com.acme.quickstart');
}
// 2. Open the document.
$path = $argv[1] ?? 'sample.pdf';
$doc = Document::open($path);
// 3. Page count and size.
$count = $doc->pageCount();
printf("%s: %d pages, first page %.0f x %.0f pt\n", $path, $count, $doc->pageWidth(0), $doc->pageHeight(0));
// 4. Render the first page at 144 dpi.
file_put_contents('page1.png', $doc->renderPagePng(0, 144));
// 5. Extract the text of every page.
for ($i = 0; $i < $count; $i++) {
echo '--- page ', $i + 1, " ---\n";
echo $doc->extractPageText($i), "\n";
}
$doc->close();What the code does
NexPdf::activateLicense($keyOrXml, $platform, $appId)accepts the license XML or the bare key. The app id must be one the key was issued for. See License.Document::open($path, $password = null)opens a PDF. Word, Excel and PowerPoint files are converted when the license includesconversion; JPEG, PNG and TIFF pictures become one page each.pageWidth($i)andpageHeight($i)are the displayed size in points: crop box, rotation and/UserUnitapplied.renderPagePng($i, $dpi)returns the PNG bytes as a binary string. 72 dpi is one pixel per point.extractPageText($i)returns the page text in reading order.