From f13953452b0be97fda5944a3afc012344c6de8b6 Mon Sep 17 00:00:00 2001 From: "Pierre Habouzit (MadCoder" Date: Wed, 29 Dec 2004 18:12:45 +0000 Subject: [PATCH] PDF + VarStream * enhance PDF with photos * VarStream : this is a real kludge, it allows to access global variables like file. every global var is accessible with 'var://varname' url. really cool for the previous since I don't want to use temporary files. git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-179 --- ChangeLog | 1 + htdocs/carnet/mescontacts_pdf.php | 3 +- include/contacts.pdf.inc.php | 53 ++++++++++++++++++++++++---- include/xorg.varstream.inc.php | 74 +++++++++++++++++++++++++++++++++++++++ templates/carnet/mescontacts.tpl | 15 ++++++-- 5 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 include/xorg.varstream.inc.php diff --git a/ChangeLog b/ChangeLog index 17f27ab..ff56d69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,7 @@ New : * Contacts : - Brand new PDF of the contact list (using FPDF). -MC + - Even include Photos in the PDF (kludgy !). -MC * Validation : - quite a lot of rewrite, simplifications. -MC diff --git a/htdocs/carnet/mescontacts_pdf.php b/htdocs/carnet/mescontacts_pdf.php index ba074c3..3dce194 100644 --- a/htdocs/carnet/mescontacts_pdf.php +++ b/htdocs/carnet/mescontacts_pdf.php @@ -40,9 +40,8 @@ $pdf = new ContactsPDF(); while (list($alias) = $citer->next()) { $user = get_user_details($alias); - $pdf->addContact($user); + $pdf->addContact($user, Env::has('photo')); } - $pdf->Output(); ?> diff --git a/include/contacts.pdf.inc.php b/include/contacts.pdf.inc.php index af06a5f..9a2342c 100644 --- a/include/contacts.pdf.inc.php +++ b/include/contacts.pdf.inc.php @@ -21,14 +21,16 @@ define ('FPDF_FONTPATH', dirname(__FILE__).'/fonts/'); require_once('/usr/share/fpdf/fpdf.php'); +require_once('xorg.varstream.inc.php'); class ContactsPDF extends FPDF { var $col = 0; var $y0; - var $title = "Mes contacts sur Polytechnique.org"; + var $title = "Mes contacts sur Polytechnique.org"; var $broken = false; + var $error = false; function ContactsPDF() { @@ -44,6 +46,12 @@ class ContactsPDF extends FPDF $this->AddPage(); } + function Output() + { + Header('Pragma: public'); + parent::Output(); + } + function Rotate($angle,$x=-1,$y=-1) { if($x==-1) { @@ -202,7 +210,12 @@ class ContactsPDF extends FPDF } } - function AddContact($x) + function Error() + { + $this->error = true; + } + + function AddContact($x, $wp = true) { global $globals; /* infamous hack : @@ -219,7 +232,36 @@ class ContactsPDF extends FPDF $this->SetLineWidth(0.4); $nom = $x['prenom'].' '.($x['epouse'] ? "{$x['epouse']} - née {$x['nom']}" : $x['nom'])." ({$x['promo']})"; - $this->Cell(0, 6, $nom, "T", 1, 'C', 1, $globals->baseurl."/fiche.php?user={$x['forlife']}"); + $ok = false; + + if ($wp) { + $res = $globals->xdb->query("SELECT * FROM photo WHERE attachmime IN ('jpeg','png') AND uid={?}", $x['user_id']); + if ($i = $res->numRows()) { + $old2 = $this; + $photo = $res->fetchOneAssoc(); + $width = $photo['x'] * 20/$photo['y']; + $GLOBALS["p{$x['user_id']}"] = $photo['attach']; + + $_x = $this->getX(); + $_y = $this->getY(); + $this->Cell(0, 20, '', '', 0, 'C', 1); + $this->Image("var://p{$x['user_id']}", $_x, $_y, $width, 20, $photo['attachmime']); + + if ($this->error) { + $this = $old2; + } else { + $this->setX($_x); + $this->Cell($width, 20, '', "T"); + $h = ( $this->GetStringWidth($nom) + $width > 90 ) ? 10 : 20; + $this->MultiCell(0, $h, $nom, 'T', 'C'); + $ok = true; + } + } + } + if (!$ok) { + $this->MultiCell(0, 6, $nom, "T", 'C', 1); + } + $this->Space(); if ($x['mobile']) { @@ -242,12 +284,11 @@ class ContactsPDF extends FPDF $this->Space(); } - $this->Ln(); - $this->Ln(); + $this->Ln(10); if ($this->broken) { $old->NextCol(); - $old->AddContact($x); + $old->AddContact($x, $wp); $this = $old; } } diff --git a/include/xorg.varstream.inc.php b/include/xorg.varstream.inc.php new file mode 100644 index 0000000..ef0909d --- /dev/null +++ b/include/xorg.varstream.inc.php @@ -0,0 +1,74 @@ +varname = $url['host']; + if(!isset($GLOBALS[$this->varname])) + { + trigger_error('Global variable '.$this->varname.' does not exist', E_USER_WARNING); + return false; + } + $this->position = 0; + return true; + } + + // }}} + // {{{ stream_read + + function stream_read($count) + { + $ret = substr($GLOBALS[$this->varname], $this->position, $count); + $this->position += strlen($ret); + return $ret; + } + + // }}} + // {{{ stream_eof + + function stream_eof() + { + return $this->position >= strlen($GLOBALS[$this->varname]); + } + + // }}} +} + +// }}} + +stream_wrapper_register('var','VarStream'); + +// vim:set et sw=4 sts=4 sws=4: +?> diff --git a/templates/carnet/mescontacts.tpl b/templates/carnet/mescontacts.tpl index 18ffdb6..2acea6a 100644 --- a/templates/carnet/mescontacts.tpl +++ b/templates/carnet/mescontacts.tpl @@ -39,10 +39,19 @@ {if $trombi || $citer->total()}

- Pour récupérer ta liste de contacts dans un PDF imprimable :
- [Triée par promo] - [Triée par noms] +Pour récupérer ta liste de contacts dans un PDF imprimable :
+(attention, les photos font beaucoup grossir les fichiers !)

+ {if $trombi} -- 2.1.4