Rendering pages
Render PDF pages and thumbnails to PNG with NexPDF for PHP, and map PDF coordinates to pixels.
License module
Rendering is part of the viewer module and works without a key. Unlicensed and trial renders carry
a watermark; commercial keys render without one.
Render a page to PNG
renderPagePng($pageIndex, $dpi) returns the PNG bytes of a page as a binary string. At 72 dpi one
pixel is one PDF point; 144 dpi is a good default for screens, 300 dpi for print.
use Nexdevit\NexPdf\Document;
$doc = Document::open('report.pdf');
for ($i = 0; $i < $doc->pageCount(); $i++) {
file_put_contents(sprintf('page-%d.png', $i + 1), $doc->renderPagePng($i, 144));
}Render a thumbnail
renderThumbPng($pageIndex, $maxDim) fits the page into a square of $maxDim pixels, keeping the
aspect ratio:
$thumb = $doc->renderThumbPng(0, 256);Page size and orientation
pageWidth($i) and pageHeight($i) return the displayed size in points, with the crop box,
/Rotate and /UserUnit applied. pageViewJson($i) adds the rotation, the crop box and the
matrix from PDF user space to the displayed page:
$w = $doc->pageWidth(0);
$h = $doc->pageHeight(0);
$view = json_decode($doc->pageViewJson(0), true);
print_r([$view['rotation'], $view['crop'], $view['to_display']]);To place a PDF rectangle (for example a search hit) on a rendered image, apply to_display
[a, b, c, d, e, f] and multiply by dpi / 72:
function toPixels(array $view, float $x, float $y, float $dpi): array
{
[$a, $b, $c, $d, $e, $f] = $view['to_display'];
$s = $dpi / 72;
return [($a * $x + $c * $y + $e) * $s, ($b * $x + $d * $y + $f) * $s];
}Overprint simulation
Print-oriented PDF/X files can use overprint. setOverprint($mode) sets how later renders treat
it: 0 simulates it for PDF/X documents only (the default), 1 always, 2 never.
$doc->setOverprint(1);
$png = $doc->renderPagePng(0, 300);Memory
Rendering caches decoded fonts and images per document. For long-running workers,
$doc->trimMemory() drops those caches and $doc->memoryUsageBytes() reports the current use.