iamamirsalehi's avatar

Validation pdf's number of pages

Hi everyone

There is form in my project that users upload their pdf files. I wanna validate the number of pages of uploaded pdf file. So is there any package for it or Laravel has any validation for this case?

0 likes
2 replies
automica's avatar
automica
Best Answer
Level 54

@isamirsalehi you should be able to do something like this

$path = 'pdf/GFG.pdf'; 
$totalPages = countPages($path); 
    
echo $totalPages; 
    
function countPages(string $path): int
 { 
  $pdf = file_get_contents($path); 
  return preg_match_all("/\/Page\W/", $pdf, $dummy); 
} 
1 like
YeZawHein's avatar
  $pdftext = file_get_contents($request->file('request_name'));
  $num = preg_match_all("/\/Page\W/", $pdftext, $matches);

dd($num);
1 like

Please or to participate in this conversation.