2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 define ('FPDF_FONTPATH', dirname(__FILE__
).'/fonts/');
23 require_once '/usr/share/fpdf/fpdf.php';
27 class ContactsPDF
extends FPDF
29 public $title = "Mes contacts sur Polytechnique.org";
34 private $broken = false
;
35 private $error = false
;
39 public function __construct()
41 $this->report
= error_reporting(0);
43 error_reporting($this->report
);
45 $this->AddFont('Vera Sans', '', 'Vera.php');
46 $this->AddFont('Vera Sans', 'I', 'VeraIt.php');
47 $this->AddFont('Vera Sans', 'B', 'VeraBd.php');
49 $this->AddFont('Vera Mono', '', 'VeraMono.php');
51 $this->SetTitle($this->title
);
52 $this->SetCreator('Site Polytechnique.org');
56 public function Output($name='mescontacts.pdf', $dest='I')
58 Header('Pragma: public');
60 parent
::Output($name, $dest);
61 error_reporting($this->report
);
64 private function Rotate($angle, $x=-1, $y=-1)
72 if (!empty($this->angle
)) {
75 $this->angle
= $angle;
81 $cy = ($this->h
-$y)*$this->k
;
82 $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',
83 $c, $s, -$s, $c, $cx, $cy, -$cx, -$cy));
87 public function Header()
90 $this->SetFont('Vera Sans', 'B', 20);
91 $this->SetTextColor(230);
92 $this->Rotate(45, 55, 190);
93 $this->Text(55, 190, utf8_decode("informations limitées à un usage"));
94 $this->Text(40, 210, utf8_decode("strictement personnel et non commercial"));
97 $this->setLeftMargin(5);
98 $this->setRightMargin(5);
99 $this->SetFont('Vera Sans', 'B', 16);
101 $this->SetTextColor(51, 102, 153);
102 $this->SetDrawColor(102, 153, 204);
103 $this->SetLineWidth(0.2);
104 $this->SetFillColor(245, 248, 252);
105 $this->Cell(200, 10, $this->title
, 1, 1, 'C', 1);
106 $this->Image(dirname(__FILE__
).'/../../htdocs/images/logo.png',
107 5, 5, 10, 10, 'png', 'https://www.polytechnique.org/');
110 $this->y0
= $this->GetY();
111 $this->ColSetup(false
);
114 public function Footer()
116 $this->setLeftMargin(5);
117 $this->setRightMargin(5);
119 $this->SetFont('Vera Sans', 'I', 8);
120 $this->SetTextColor(128);
121 $this->Cell(0, 10, 'Page '.$this->PageNo(), 0, 0, 'C');
122 $this->Cell(0, 10, '(en date du '.strftime('%d %B %Y').')', 0, 0, 'R');
125 private function ColSetup($col)
128 $x = 10 +
$this->col
* 100;
129 $this->SetLeftMargin($x);
130 $this->SetRightMargin(120 - $x);
132 $this->SetY($this->y0
);
135 private function NextCol()
137 $this->ColSetup(1 - $this->col
);
138 if ($this->col
== 0) {
143 public function AcceptPageBreak()
145 $this->broken
= true
;
148 private function Space($w=0.1, $h=0.5)
152 $this->SetLineWidth($w);
153 $this->Line($x, $y, $x+
90, $y);
157 private function TableRow($l, $r, $font = 'Sans')
159 $this->SetFont('Vera Sans', 'B', 8);
162 $this->MultiCell(25, 4, $l, '', 1);
165 $this->SetFont('Vera '.$font, '', 8);
170 $this->MultiCell(65, 4, $r, '', 1);
172 $this->setY(max($y1, $this->getY())+
0.5);
176 private function Address($a)
182 if ($a->hasFlag('current')) {
184 } elseif ($a->hasFlag('secondary')) {
190 $this->TableRow($l, utf8_decode($a->text
));
192 foreach ($a->phones() as $phone) {
193 $this->TableRow(utf8_decode($phone->displayType()),
194 utf8_decode($phone->display
), 'Mono');
198 private function AddressPro($a)
201 $this->TableRow('Entreprise', utf8_decode($a->company
->name
));
204 $this->TableRow('adresse pro', utf8_decode($a->address()->text
));
206 foreach ($a->phones() as $phone) {
207 $this->TableRow(utf8_decode($phone->displayType()),
208 utf8_decode($phone->display
), 'Mono');
212 public function Error($msg)
217 private function wordwrap($text, $maxwidth = 90) {
219 if ($text==='') { return 0; }
220 $space = $this->GetStringWidth(' ');
221 $lines = explode("\n", $text);
225 foreach ($lines as $line) {
226 $words = preg_split('/ +/', $line);
229 foreach ($words as $word) {
230 $wordwidth = $this->GetStringWidth($word);
231 if ($width +
$wordwidth <= $maxwidth) {
232 $width +
= $wordwidth +
$space;
235 $width = $wordwidth +
$space;
236 $text = rtrim($text)."\n".$word.' ';
240 $text = rtrim($text)."\n";
243 $text = rtrim($text);
247 public static function AddContact(ContactsPDF
$self, Profile
$profile, $wp = true
)
250 1- we store the current state.
251 2- at the end, we find out if we triggered the page break,
253 -> yes ? then we have to create a col, and add the contact again.
257 $self->SetFont('Vera Sans', '', 10);
258 $self->SetDrawColor(0);
259 $self->SetFillColor(245, 248, 252);
260 $self->SetLineWidth(0.4);
262 $nom = utf8_decode($profile->full_name
);
266 $photo = $profile->getPhoto(false
, true
);
268 list(, $type) = explode('/', $photo->mimeType());
269 $type = ($type == 'jpeg') ?
'jpg' : $type;
270 if (method_exists($self, '_parse' . $type)) {
272 $width = $photo->width() * 20 / $photo->height();
275 $self->Cell(0, 20, '', '', 0, '', 1);
277 $self->Image($photo->path(), $_x, $_y, $width, 20, $type);
278 error_reporting($self->report
);
284 $self->Cell($width, 20, '', "T");
285 $h = 20 / $self->wordwrap($nom, 90 - $width);
286 $self->MultiCell(0, $h, $nom, 'T', 'C');
293 $self->MultiCell(0, 6, $nom, "T", 'C', 1);
296 if ($profile->mobile
) {
298 $self->TableRow('mobile', utf8_decode($profile->mobile
), 'Mono');
301 $it = $profile->iterAddresses(Profile
::ADDRESS_ALL
);
302 while ($a = $it->next()) {
306 $it = $profile->getJobs(Profile
::JOBS_CURRENT
);
307 foreach ($it as $a) {
309 $self->AddressPro($a);
312 $self->Space(0.4, 5);
316 $self = ContactsPDF
::AddContact($old, $profile, $wp);
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: