HomeLabOffice / Retail Space AvailableContactFor HireCodeArticles/Blog /0/0 35.172.224.102
< >

PHP Shell Exec, pdfinfo example

2022-03-15 by: Mike

I swear I recreate a variant of parsing the returned text of a PHP shell_exec from scratch once a month for something. Today it was for PDFInfo, just to get the pages of a PDF and other attributes as a bonus.

I'm sticking this here, so a month from now I can forget I saved a generic example and can recreate it again. There are more efficient ways, but this is how my brain works and I can read it and modify to suit. That's important.
pdfinfo = shell_exec("/usr/bin/pdfinfo $file") ; 
$pdi = preg_split('/\n/', $pdfinfo);
foreach($pdi as $pi) { 
  list($key,$val) = preg_split('/\:s+/', $pi);
  if(!empty($key)) { $pdf[$key] = $val ; } ; 
} ; 
print "Pages: $pdf[Pages]n" ;