652 |
mathilde |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Classe PdfExport, réalise des exportations pdf des fiches de taxons.
|
1006 |
jpm |
4 |
* Les fonctionnalités proposées sont l'export de toutes les parties de la fiche pdf
|
664 |
mathilde |
5 |
* ou bien des parties choisies.
|
|
|
6 |
* (voir le squelette fiche_pdf_lien.tpl.html pour le formulaire dans le module fiche.)
|
1006 |
jpm |
7 |
*
|
664 |
mathilde |
8 |
* manuel wkhtmltopdf : http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html
|
|
|
9 |
* pour changer de librairie : changer la fonction transformerHtmlEnPdf()
|
652 |
mathilde |
10 |
*
|
1006 |
jpm |
11 |
* @category php 5.2
|
652 |
mathilde |
12 |
* @package eflore-consultation
|
1006 |
jpm |
13 |
* @author Mathilde Salthun-Lassalle <mathilde@tela-botanica.org>
|
652 |
mathilde |
14 |
* @copyright 2012 Tela-Botanica
|
1006 |
jpm |
15 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
|
|
|
16 |
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
|
652 |
mathilde |
17 |
* @version $Id$
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
class PdfExport extends aControleur {
|
1029 |
jpm |
21 |
|
|
|
22 |
const DUREE_DE_VIE_PDF = 86400;// 3600 * 24 * 2 = 172 800
|
1034 |
jpm |
23 |
const DUREE_DE_VIE_HTML = 60;
|
1029 |
jpm |
24 |
|
652 |
mathilde |
25 |
private $Conteneur;
|
|
|
26 |
private $parametres = array();
|
|
|
27 |
private $chemin_pdf;
|
|
|
28 |
private $html;
|
|
|
29 |
private $wkhtml;
|
|
|
30 |
private $piedepage;
|
|
|
31 |
private $fichier_pdf;
|
|
|
32 |
private $blocs = array();
|
686 |
mathilde |
33 |
|
652 |
mathilde |
34 |
private $Desc;
|
|
|
35 |
private $Ecolo;
|
|
|
36 |
private $Ethno;
|
|
|
37 |
private $Illus;
|
|
|
38 |
private $Nomen;
|
|
|
39 |
private $Reparti;
|
|
|
40 |
private $Stat;
|
|
|
41 |
private $Biblio;
|
|
|
42 |
|
|
|
43 |
public function initialiser() {
|
695 |
mathilde |
44 |
spl_autoload_register(array($this, 'chargerClassesOnglets'));
|
652 |
mathilde |
45 |
$this->capturerParametres();
|
|
|
46 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
47 |
$this->chemin_pdf = Config::get('dossier_pdf');
|
|
|
48 |
$this->wkhtml = Config::get('WKHTMLTOPDF');
|
|
|
49 |
$this->Desc = new Description($this->conteneur);
|
660 |
mathilde |
50 |
$this->Ecolo = new Ecologie($this->conteneur);
|
|
|
51 |
$this->Ethno = new Ethnobotanique($this->conteneur);
|
652 |
mathilde |
52 |
$this->Illus = new Illustrations($this->conteneur);
|
|
|
53 |
$this->Nomen = new Nomenclature($this->conteneur);
|
|
|
54 |
$this->Stat = new Statut($this->conteneur);
|
660 |
mathilde |
55 |
$this->Biblio = new Bibliographie($this->conteneur);
|
|
|
56 |
$this->Reparti = new Repartition($this->conteneur);
|
652 |
mathilde |
57 |
$this->piedepage = Config::get('chemin_modules').'pdf_export/squelettes/footer.html';
|
1006 |
jpm |
58 |
}
|
|
|
59 |
|
|
|
60 |
private function chargerClassesOnglets($classe) {
|
|
|
61 |
$base = dirname(__FILE__).DS;
|
|
|
62 |
$cheminFormateurs = $base.'../fiche/formateurs'.DS;
|
|
|
63 |
$dossiers = array($base, $cheminFormateurs);
|
|
|
64 |
foreach ($dossiers as $chemin) {
|
|
|
65 |
$fichierATester = $chemin.$classe.'.php';
|
|
|
66 |
if (file_exists($fichierATester)) {
|
|
|
67 |
include_once $fichierATester;
|
|
|
68 |
return null;
|
695 |
mathilde |
69 |
}
|
|
|
70 |
}
|
1006 |
jpm |
71 |
}
|
|
|
72 |
|
701 |
mathilde |
73 |
private function getNomRetenu() {
|
686 |
mathilde |
74 |
$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
|
|
|
75 |
return $nom_retenu;
|
|
|
76 |
}
|
1006 |
jpm |
77 |
|
701 |
mathilde |
78 |
private function getNomRetenuHTML() {
|
686 |
mathilde |
79 |
$nom_retenu = '<span class="italique">'
|
1006 |
jpm |
80 |
.$this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci')
|
|
|
81 |
.'</span> '.$this->conteneur->getNomCourant()->getNomRetenu()->get('auteur');
|
686 |
mathilde |
82 |
return $nom_retenu;
|
|
|
83 |
}
|
1006 |
jpm |
84 |
|
686 |
mathilde |
85 |
private function getNomFichierValide(){
|
|
|
86 |
$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
|
|
|
87 |
$nom_retenu = str_replace(' ','_',$nom_retenu );
|
|
|
88 |
$nom_retenu = preg_replace('/[\(\)\.\[\]]/','',$nom_retenu );
|
|
|
89 |
return $nom_retenu;
|
|
|
90 |
}
|
1006 |
jpm |
91 |
|
652 |
mathilde |
92 |
private function capturerParametres() {
|
|
|
93 |
if (isset($_GET['num_nom'])) {
|
|
|
94 |
$this->parametres['num_nom'] = $_GET['num_nom'];
|
|
|
95 |
}
|
|
|
96 |
if (isset($_GET['nom'])) {
|
|
|
97 |
$this->parametres['nom'] = $_GET['nom'];
|
|
|
98 |
}
|
|
|
99 |
if (isset($_GET['type_nom'])) {
|
|
|
100 |
$this->parametres['type_nom'] = $_GET['type_nom'];
|
|
|
101 |
}
|
|
|
102 |
if (isset($_GET['referentiel'])) {
|
|
|
103 |
$this->parametres['referentiel'] = $_GET['referentiel'];
|
|
|
104 |
}
|
|
|
105 |
if (isset($_GET['niveau'])) {
|
|
|
106 |
Registre::set('parametres.niveau', $_GET['niveau']);
|
|
|
107 |
}
|
|
|
108 |
if (isset($_GET['onglet'])) {
|
|
|
109 |
$this->onglet = $_GET['onglet'];
|
|
|
110 |
}
|
|
|
111 |
}
|
1006 |
jpm |
112 |
|
652 |
mathilde |
113 |
private function capturerParametresFormulaire() {
|
1031 |
jpm |
114 |
$this->blocs = array('description', 'ecologie', 'ethnobotanique', 'statuts', 'illustrations',
|
|
|
115 |
'bibliographie', 'repartition', 'nomenclature');
|
652 |
mathilde |
116 |
if (!empty($_POST['bloc'])) {
|
|
|
117 |
$this->blocs = $_POST['bloc'];
|
|
|
118 |
}
|
|
|
119 |
}
|
1006 |
jpm |
120 |
|
652 |
mathilde |
121 |
public function executerActionParDefaut() {
|
|
|
122 |
$this->executerPdfExport();
|
|
|
123 |
}
|
1006 |
jpm |
124 |
|
652 |
mathilde |
125 |
public function executerPdfExport(){
|
1031 |
jpm |
126 |
$this->capturerParametresFormulaire();
|
1032 |
jpm |
127 |
$hash = crc32($this->parametres['referentiel'].'-'.implode('-', $this->blocs));
|
1031 |
jpm |
128 |
$nom_fichier = $this->getNomFichierValide().'-'.$hash;
|
|
|
129 |
|
1030 |
jpm |
130 |
$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'.html';
|
|
|
131 |
$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'.pdf';
|
|
|
132 |
|
|
|
133 |
if (file_exists($this->fichier_pdf) == false) {
|
|
|
134 |
$donnees = $this->obtenirDonnees();
|
|
|
135 |
$this->construireHtml($donnees);
|
|
|
136 |
if ($this->transformerHtmlEnPdf() == false) {
|
|
|
137 |
die('Erreur de generation du fichier PDF');
|
|
|
138 |
}
|
1006 |
jpm |
139 |
}
|
1030 |
jpm |
140 |
$this->envoyerPdfAuNavigateur();
|
1033 |
jpm |
141 |
$this->nettoyerFichiersTmp();
|
1030 |
jpm |
142 |
// ATTENTION : on doit stopper l'exécution de Papyrus sinon du HTML inutile est ajouté à la fin du fichier PDF
|
1011 |
raphael |
143 |
exit;
|
652 |
mathilde |
144 |
}
|
1006 |
jpm |
145 |
|
652 |
mathilde |
146 |
private function construireHtml($donnees) {
|
681 |
mathilde |
147 |
$this->html = $this->getVue('pdf_header', $donnees);
|
652 |
mathilde |
148 |
foreach ($this->blocs as $bloc) {
|
|
|
149 |
$this->ajouterHtml($bloc, $donnees);
|
|
|
150 |
}
|
|
|
151 |
$this->html .= '</body></html>';
|
|
|
152 |
}
|
1006 |
jpm |
153 |
|
652 |
mathilde |
154 |
private function ajouterHtml($bloc, $donnees) {
|
|
|
155 |
if (!empty($donnees[$bloc])) {
|
|
|
156 |
$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
|
|
|
157 |
}
|
|
|
158 |
}
|
1006 |
jpm |
159 |
|
|
|
160 |
//+-------------------------------------récupération des données---------------------------------------------+
|
652 |
mathilde |
161 |
private function obtenirDonnees() {
|
|
|
162 |
$donnees = array();
|
715 |
mathilde |
163 |
$version = $this->Nomen->obtenirVersionDonnees();
|
1006 |
jpm |
164 |
$donnees['donnees_pdf'] = array(
|
|
|
165 |
'nom' => $this->getNomRetenuHTML(),
|
|
|
166 |
'chemin_css' => Config::get('url_css_pdf'),
|
|
|
167 |
'version' => $version['version']);
|
695 |
mathilde |
168 |
$donnees['description'] = $this->Desc->obtenirDonneesExport();
|
|
|
169 |
$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
|
652 |
mathilde |
170 |
$donnees['statuts'] = $this->Stat->obtenirDonnees();
|
|
|
171 |
$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
|
695 |
mathilde |
172 |
$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
|
652 |
mathilde |
173 |
$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
|
695 |
mathilde |
174 |
$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
|
652 |
mathilde |
175 |
$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
|
672 |
mathilde |
176 |
$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
|
652 |
mathilde |
177 |
return $donnees;
|
|
|
178 |
}
|
664 |
mathilde |
179 |
|
1006 |
jpm |
180 |
//+------------------------------------------------export en pdf---------------------------------------------+
|
652 |
mathilde |
181 |
private function envoyerPdfAuNavigateur() {
|
686 |
mathilde |
182 |
$nom_fichier = $this->getNomFichierValide();
|
652 |
mathilde |
183 |
header('Content-type: application/pdf');
|
686 |
mathilde |
184 |
header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
|
652 |
mathilde |
185 |
readfile($this->fichier_pdf);
|
|
|
186 |
}
|
1006 |
jpm |
187 |
|
1029 |
jpm |
188 |
private function nettoyerFichiersTmp() {
|
|
|
189 |
$dossierStockage = $this->chemin_pdf;
|
|
|
190 |
if (is_dir($dossierStockage)) {
|
|
|
191 |
$objets = scandir($dossierStockage);
|
|
|
192 |
if ($objets !== false) {
|
|
|
193 |
foreach ($objets as $objet) {
|
|
|
194 |
$chemin = $dossierStockage.$objet;
|
|
|
195 |
if (is_file($chemin)) {
|
1034 |
jpm |
196 |
$filemtime = @filemtime($chemin);
|
|
|
197 |
if ($filemtime !== false) {
|
|
|
198 |
if (substr($chemin, -5) == '.html') {
|
|
|
199 |
$suppression = (time() - $filemtime >= self::DUREE_DE_VIE_HTML) ? true : false;
|
|
|
200 |
} else {
|
1033 |
jpm |
201 |
$suppression = (time() - $filemtime >= self::DUREE_DE_VIE_PDF) ? true : false;
|
1029 |
jpm |
202 |
}
|
1034 |
jpm |
203 |
if ($suppression === true) {
|
|
|
204 |
unlink($chemin);
|
|
|
205 |
}
|
1029 |
jpm |
206 |
}
|
|
|
207 |
}
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
652 |
mathilde |
213 |
//version WKHTMLtoPDF en ligne de commande
|
|
|
214 |
private function transformerHtmlEnPdf() {
|
701 |
mathilde |
215 |
$nom = $this->getNomRetenu();
|
664 |
mathilde |
216 |
file_put_contents($this->fichier_html, $this->html);
|
703 |
mathilde |
217 |
$commande = "{$this->wkhtml} --replace 'nom' '$nom' --encoding utf-8 --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
|
1005 |
raphael |
218 |
$ret = -1;
|
|
|
219 |
$debug = array();
|
|
|
220 |
exec($commande, $debug, $ret);
|
|
|
221 |
//print_r( $debug ); echo $ret;
|
1030 |
jpm |
222 |
$ok = ($ret == 0 || $ret == 2) ? true : false;
|
|
|
223 |
return $ok;
|
652 |
mathilde |
224 |
}
|
|
|
225 |
}
|
|
|
226 |
?>
|