Voici une routine qui devrait être utile à tout les développeurs d’applications pré-presse. Le principe peut-être retranscrit dans n’importe quel langage et s’appuie sur l’outil ghostscript.
<?php
/**
* Created by Dominique FERET.
* Date: 26/08/2014
* Time: 09:56
*/
/**
* Fonction qui analyse la nature des pages d'un pdf.
* @param $nomfichier => chemin et nom du fichier.
* @return $this => retourne un tableau encodé en JSON contenant la liste des pages de chaque type et leur comptes
*/
function analysepdf($nomfichier)
{
header('content-type: application/json');
mb_internal_encoding("UTF-8");
$o = new stdClass();
if (file_exists($nomfichier)){
$commande = 'gs -o - -q -sDEVICE=inkcov "' . $nomfichier . '"';
$lignes = array();
exec($commande, $lignes);
$noir = 0;
$couleur = 0;
$blanche = 0;
$pagesblanches=array();
$pagesnoires=array();
$pagescouleurs=array();
$numeropage=0;
foreach ($lignes as $ligne) {
$cyan = substr($ligne, 1, 7);
$magenta = substr($ligne, 10, 7);
$yellow = substr($ligne, 19, 7);
$black = substr($ligne, 28, 7);
if (is_numeric($cyan)&& is_numeric($magenta) && is_numeric($yellow) && is_numeric($black)){ // si la ligne contient bien une ligne de valeurs
$numeropage++; // on ajoute 1 a la ligne en cours
if((($cyan+$magenta+$yellow)==0)){
$noir++;
if($black==0) {
$pagesblanches[]=$numeropage;
$blanche++;
}
$pagesnoires[]=$numeropage;
}else{
$couleur++;
$pagescouleurs[]=$numeropage;
}
}
}
$o->listepagescouleurs=implode(",",$pagescouleurs);
$o->listepagesnoires=implode(",",$pagesnoires);
$o->listepagesblanches=implode(",",$pagesblanches);
$o->totalpages = $numeropage;
$o->pagesnoires = $noir;
$o->pagescouleurs = $couleur;
$o->pagesblanches = $blanche;
}
return json_encode($o);
}
