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 {
|
|
|
21 |
private $Conteneur;
|
|
|
22 |
private $parametres = array();
|
|
|
23 |
private $chemin_pdf;
|
|
|
24 |
private $html;
|
|
|
25 |
private $wkhtml;
|
|
|
26 |
private $piedepage;
|
|
|
27 |
private $fichier_pdf;
|
|
|
28 |
private $blocs = array();
|
686 |
mathilde |
29 |
|
652 |
mathilde |
30 |
private $Desc;
|
|
|
31 |
private $Ecolo;
|
|
|
32 |
private $Ethno;
|
|
|
33 |
private $Illus;
|
|
|
34 |
private $Nomen;
|
|
|
35 |
private $Reparti;
|
|
|
36 |
private $Stat;
|
|
|
37 |
private $Biblio;
|
|
|
38 |
|
|
|
39 |
public function initialiser() {
|
695 |
mathilde |
40 |
spl_autoload_register(array($this, 'chargerClassesOnglets'));
|
652 |
mathilde |
41 |
$this->capturerParametres();
|
|
|
42 |
$this->conteneur = new Conteneur($this->parametres);
|
|
|
43 |
$this->chemin_pdf = Config::get('dossier_pdf');
|
|
|
44 |
$this->wkhtml = Config::get('WKHTMLTOPDF');
|
|
|
45 |
$this->Desc = new Description($this->conteneur);
|
660 |
mathilde |
46 |
$this->Ecolo = new Ecologie($this->conteneur);
|
|
|
47 |
$this->Ethno = new Ethnobotanique($this->conteneur);
|
652 |
mathilde |
48 |
$this->Illus = new Illustrations($this->conteneur);
|
|
|
49 |
$this->Nomen = new Nomenclature($this->conteneur);
|
|
|
50 |
$this->Stat = new Statut($this->conteneur);
|
660 |
mathilde |
51 |
$this->Biblio = new Bibliographie($this->conteneur);
|
|
|
52 |
$this->Reparti = new Repartition($this->conteneur);
|
652 |
mathilde |
53 |
$this->piedepage = Config::get('chemin_modules').'pdf_export/squelettes/footer.html';
|
1006 |
jpm |
54 |
}
|
|
|
55 |
|
|
|
56 |
private function chargerClassesOnglets($classe) {
|
|
|
57 |
$base = dirname(__FILE__).DS;
|
|
|
58 |
$cheminFormateurs = $base.'../fiche/formateurs'.DS;
|
|
|
59 |
$dossiers = array($base, $cheminFormateurs);
|
|
|
60 |
foreach ($dossiers as $chemin) {
|
|
|
61 |
$fichierATester = $chemin.$classe.'.php';
|
|
|
62 |
if (file_exists($fichierATester)) {
|
|
|
63 |
include_once $fichierATester;
|
|
|
64 |
return null;
|
695 |
mathilde |
65 |
}
|
|
|
66 |
}
|
1006 |
jpm |
67 |
}
|
|
|
68 |
|
701 |
mathilde |
69 |
private function getNomRetenu() {
|
686 |
mathilde |
70 |
$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
|
|
|
71 |
return $nom_retenu;
|
|
|
72 |
}
|
1006 |
jpm |
73 |
|
701 |
mathilde |
74 |
private function getNomRetenuHTML() {
|
686 |
mathilde |
75 |
$nom_retenu = '<span class="italique">'
|
1006 |
jpm |
76 |
.$this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci')
|
|
|
77 |
.'</span> '.$this->conteneur->getNomCourant()->getNomRetenu()->get('auteur');
|
686 |
mathilde |
78 |
return $nom_retenu;
|
|
|
79 |
}
|
1006 |
jpm |
80 |
|
686 |
mathilde |
81 |
private function getNomFichierValide(){
|
|
|
82 |
$nom_retenu = $this->conteneur->getNomCourant()->getNomRetenu()->get('nom_sci');
|
|
|
83 |
$nom_retenu = str_replace(' ','_',$nom_retenu );
|
|
|
84 |
$nom_retenu = preg_replace('/[\(\)\.\[\]]/','',$nom_retenu );
|
|
|
85 |
return $nom_retenu;
|
|
|
86 |
}
|
1006 |
jpm |
87 |
|
652 |
mathilde |
88 |
private function capturerParametres() {
|
|
|
89 |
if (isset($_GET['num_nom'])) {
|
|
|
90 |
$this->parametres['num_nom'] = $_GET['num_nom'];
|
|
|
91 |
}
|
|
|
92 |
if (isset($_GET['nom'])) {
|
|
|
93 |
$this->parametres['nom'] = $_GET['nom'];
|
|
|
94 |
}
|
|
|
95 |
if (isset($_GET['type_nom'])) {
|
|
|
96 |
$this->parametres['type_nom'] = $_GET['type_nom'];
|
|
|
97 |
}
|
|
|
98 |
if (isset($_GET['referentiel'])) {
|
|
|
99 |
$this->parametres['referentiel'] = $_GET['referentiel'];
|
|
|
100 |
}
|
|
|
101 |
if (isset($_GET['niveau'])) {
|
|
|
102 |
Registre::set('parametres.niveau', $_GET['niveau']);
|
|
|
103 |
}
|
|
|
104 |
if (isset($_GET['onglet'])) {
|
|
|
105 |
$this->onglet = $_GET['onglet'];
|
|
|
106 |
}
|
|
|
107 |
}
|
1006 |
jpm |
108 |
|
652 |
mathilde |
109 |
private function capturerParametresFormulaire() {
|
|
|
110 |
if (!empty($_POST['bloc'])) {
|
|
|
111 |
$this->blocs = $_POST['bloc'];
|
|
|
112 |
} else {
|
1012 |
raphael |
113 |
$this->blocs = array('description', 'ecologie', 'ethnobotanique',
|
|
|
114 |
'statuts', 'illustrations', 'bibliographie',
|
|
|
115 |
'repartition', 'nomenclature');
|
652 |
mathilde |
116 |
}
|
|
|
117 |
}
|
1006 |
jpm |
118 |
|
652 |
mathilde |
119 |
public function executerActionParDefaut() {
|
|
|
120 |
$this->executerPdfExport();
|
|
|
121 |
}
|
1006 |
jpm |
122 |
|
652 |
mathilde |
123 |
public function executerPdfExport(){
|
1006 |
jpm |
124 |
$this->capturerParametresFormulaire();
|
|
|
125 |
$donnees = $this->obtenirDonnees();
|
|
|
126 |
$this->construireHtml($donnees);
|
|
|
127 |
if ($this->transformerHtmlEnPdf()) {
|
|
|
128 |
$this->envoyerPdfAuNavigateur();
|
|
|
129 |
} else {
|
|
|
130 |
die('Erreur de generation du fichier PDF');
|
|
|
131 |
}
|
1011 |
raphael |
132 |
// important, autrement l'exécution de papyrus continue
|
|
|
133 |
// et du HTML inutile est ajouté à la fin du fichier PDF
|
|
|
134 |
exit;
|
652 |
mathilde |
135 |
}
|
1006 |
jpm |
136 |
|
652 |
mathilde |
137 |
private function construireHtml($donnees) {
|
681 |
mathilde |
138 |
$this->html = $this->getVue('pdf_header', $donnees);
|
652 |
mathilde |
139 |
foreach ($this->blocs as $bloc) {
|
|
|
140 |
$this->ajouterHtml($bloc, $donnees);
|
|
|
141 |
}
|
|
|
142 |
$this->html .= '</body></html>';
|
|
|
143 |
}
|
1006 |
jpm |
144 |
|
652 |
mathilde |
145 |
private function ajouterHtml($bloc, $donnees) {
|
|
|
146 |
if (!empty($donnees[$bloc])) {
|
|
|
147 |
$this->html .= $this->getVue('pdf_'.$bloc, $donnees);
|
|
|
148 |
}
|
|
|
149 |
}
|
1006 |
jpm |
150 |
|
|
|
151 |
//+-------------------------------------récupération des données---------------------------------------------+
|
652 |
mathilde |
152 |
private function obtenirDonnees() {
|
|
|
153 |
$donnees = array();
|
715 |
mathilde |
154 |
$version = $this->Nomen->obtenirVersionDonnees();
|
1006 |
jpm |
155 |
$donnees['donnees_pdf'] = array(
|
|
|
156 |
'nom' => $this->getNomRetenuHTML(),
|
|
|
157 |
'chemin_css' => Config::get('url_css_pdf'),
|
|
|
158 |
'version' => $version['version']);
|
695 |
mathilde |
159 |
$donnees['description'] = $this->Desc->obtenirDonneesExport();
|
|
|
160 |
$donnees['ecologie'] = $this->Ecolo->obtenirDonneesExport();
|
652 |
mathilde |
161 |
$donnees['statuts'] = $this->Stat->obtenirDonnees();
|
|
|
162 |
$donnees['ethnobotanique'] = $this->Ethno->obtenirDonnees();
|
695 |
mathilde |
163 |
$donnees['illustrations'] = $this->Illus->obtenirDonneesExport();
|
652 |
mathilde |
164 |
$donnees['nomenclature'] = $this->Nomen->obtenirDonnees();
|
695 |
mathilde |
165 |
$donnees['repartition'] = $this->Reparti->obtenirDonneesExport();
|
652 |
mathilde |
166 |
$donnees['bibliographie'] = $this->Biblio->obtenirDonnees();
|
672 |
mathilde |
167 |
$donnees['premier'] = $this->blocs[0];// pour page-break : éviter 1ere page vide si bloc trop long
|
652 |
mathilde |
168 |
return $donnees;
|
|
|
169 |
}
|
664 |
mathilde |
170 |
|
1006 |
jpm |
171 |
//+------------------------------------------------export en pdf---------------------------------------------+
|
652 |
mathilde |
172 |
private function envoyerPdfAuNavigateur() {
|
686 |
mathilde |
173 |
$nom_fichier = $this->getNomFichierValide();
|
652 |
mathilde |
174 |
header('Content-type: application/pdf');
|
686 |
mathilde |
175 |
header('Content-Disposition: attachment; filename='.$nom_fichier.'.pdf');
|
652 |
mathilde |
176 |
readfile($this->fichier_pdf);
|
1016 |
raphael |
177 |
unlink($this->fichier_html);
|
|
|
178 |
unlink($this->fichier_pdf);
|
652 |
mathilde |
179 |
}
|
1006 |
jpm |
180 |
|
652 |
mathilde |
181 |
//version WKHTMLtoPDF en ligne de commande
|
|
|
182 |
private function transformerHtmlEnPdf() {
|
686 |
mathilde |
183 |
$nom_fichier = $this->getNomFichierValide();
|
652 |
mathilde |
184 |
$time = $_SERVER['REQUEST_TIME'] ;
|
686 |
mathilde |
185 |
$this->fichier_html = $this->chemin_pdf.''.$nom_fichier.'-'.$time.'.html';
|
|
|
186 |
$this->fichier_pdf = $this->chemin_pdf.''.$nom_fichier.'-'.$time.'.pdf';
|
701 |
mathilde |
187 |
$nom = $this->getNomRetenu();
|
664 |
mathilde |
188 |
file_put_contents($this->fichier_html, $this->html);
|
703 |
mathilde |
189 |
$commande = "{$this->wkhtml} --replace 'nom' '$nom' --encoding utf-8 --footer-html {$this->piedepage} {$this->fichier_html} {$this->fichier_pdf}";
|
1005 |
raphael |
190 |
$ret = -1;
|
|
|
191 |
$debug = array();
|
|
|
192 |
exec($commande, $debug, $ret);
|
|
|
193 |
//print_r( $debug ); echo $ret;
|
|
|
194 |
return ( $ret == 0 );
|
652 |
mathilde |
195 |
}
|
|
|
196 |
}
|
|
|
197 |
?>
|