use it in a condition outside the script.
where does this script tag live? inside the Dom pdf object?
if you want to count the pages in a pdf, once you have made it, the following code snippet will do:
$path = 'path/to/your/document.pdf';
$totoalPages = countPages($path);
echo $totoalPages;
function countPages($path) {
$pdftext = file_get_contents($path);
$num = preg_match_all("//PageW/", $pdftext, $dummy);
return $num;
}