1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
| <?php
/*******************************************************************************
* Software: class PDF extends FPDF *
* Version: 1 *
* Date: 2006-12-06 *
* Author: Cédric Loïodice *
* License: Freeware *
* You may use, modify and redistribute this software as you wish. *
* Role : *
* Cette classe permet d'exporter des tables de MySQL en pdf avec un *
* dimmensionnement des colonnes et des tailles de caractères automatique *
* afin de tenir sur une largeur de page. *
*******************************************************************************/
class PDF extends FPDF
{
var $width;
var $height;
//Connection à une base de données mysql
function connect($host='localhost',$username='',$password='',$db=''){
$this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
mysql_select_db($db,$this->conn) or die( mysql_error() );
return true;
}
//Execute une requete sql
function query($query){
$this->results = mysql_query($query,$this->conn);
$this->numFields = mysql_num_fields($this->results);
}
//Regle la largeur d'une cellule
function SetWidth($width)
{
$this->width = $width;
}
//Regle la largeur d'une cellule en fonction du nombre de cellule
function AutoWidth($numFields)
{
// starting col width
$this->width = (($this->w-$this->lMargin-$this->rMargin))/$numFields;
}
//regle la hauteur d'une cellule
function SetHeight($height)
{
$this->height = $height;
}
//Ajuste la taille de la police en fonction de la cellule et ecrit verticalement
function VCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0)
{
//Output a cell
$k=$this->k;
if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
{
$x=$this->x;
$ws=$this->ws;
if($ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->AddPage($this->CurOrientation);
$this->x=$x;
if($ws>0)
{
$this->ws=$ws;
$this->_out(sprintf('%.3f Tw',$ws*$k));
}
}
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$s='';
// begin change Cell function
if($fill==1 or $border>0)
{
if($fill==1)
$op=($border>0) ? 'B' : 'f';
else
$op='S';
if ($border>1) {
$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}
else
$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}
if(is_string($border))
{
$x=$this->x;
$y=$this->y;
if(is_int(strpos($border,'L')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'l')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
if(is_int(strpos($border,'T')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
else if(is_int(strpos($border,'t')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
if(is_int(strpos($border,'R')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'r')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
if(is_int(strpos($border,'B')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'b')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
}
if(trim($txt)!='')
{
$cr=substr_count($txt,"\n");
if ($cr>0) { // Multi line
$txts = explode("\n", $txt);
$lines = count($txts);
for($l=0;$l<$lines;$l++) {
$txt=$txts[$l];
$w_txt=$this->GetStringWidth($txt);
if ($align=='U')
$dy=$this->cMargin+$w_txt;
elseif($align=='D')
$dy=$h-$this->cMargin;
else
$dy=($h+$w_txt)/2;
$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
if($this->ColorFlag)
$s.='q '.$this->TextColor.' ';
$s.=sprintf('BT 0 1 -1 0 %.2f %.2f Tm (%s) Tj ET ',
($this->x+.5*$w+(.7+$l-$lines/2)*$this->FontSize)*$k,
($this->h-($this->y+$dy))*$k,$txt);
if($this->ColorFlag)
$s.=' Q ';
}
}
else { // Single line
$w_txt=$this->GetStringWidth($txt);
$Tz=100;
if ($w_txt>$h-2*$this->cMargin) {
$Tz=($h-2*$this->cMargin)/$w_txt*100;
$w_txt=$h-2*$this->cMargin;
}
if ($align=='U')
$dy=$this->cMargin+$w_txt;
elseif($align=='D')
$dy=$h-$this->cMargin;
else
$dy=($h+$w_txt)/2;
$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
if($this->ColorFlag)
$s.='q '.$this->TextColor.' ';
$s.=sprintf('q BT 0 1 -1 0 %.2f %.2f Tm %.2f Tz (%s) Tj ET Q ',
($this->x+.5*$w+.3*$this->FontSize)*$k,
($this->h-($this->y+$dy))*$k,$Tz,$txt);
if($this->ColorFlag)
$s.=' Q ';
}
}
// end change Cell function
if($s)
$this->_out($s);
$this->lasth=$h;
if($ln>0)
{
//Go to next line
$this->y+=$h;
if($ln==1)
$this->x=$this->lMargin;
}
else
$this->x+=$w;
}
//Ajuste la taille de la police en fonction de la cellule et ecrit horizontalement
function HCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
{
//Output a cell
$k=$this->k;
if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
{
$x=$this->x;
$ws=$this->ws;
if($ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
$this->AddPage($this->CurOrientation);
$this->x=$x;
if($ws>0)
{
$this->ws=$ws;
$this->_out(sprintf('%.3f Tw',$ws*$k));
}
}
if($w==0)
$w=$this->w-$this->rMargin-$this->x;
$s='';
// begin change Cell function 12.08.2003
if($fill==1 or $border>0)
{
if($fill==1)
$op=($border>0) ? 'B' : 'f';
else
$op='S';
if ($border>1) {
$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}
else
$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}
if(is_string($border))
{
$x=$this->x;
$y=$this->y;
if(is_int(strpos($border,'L')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'l')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
if(is_int(strpos($border,'T')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
else if(is_int(strpos($border,'t')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
if(is_int(strpos($border,'R')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'r')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
if(is_int(strpos($border,'B')))
$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
else if(is_int(strpos($border,'b')))
$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
}
if (trim($txt)!='') {
$cr=substr_count($txt,"\n");
if ($cr>0) { // Multi line
$txts = explode("\n", $txt);
$lines = count($txts);
//$dy=($h-2*$this->cMargin)/$lines;
for($l=0;$l<$lines;$l++) {
$txt=$txts[$l];
$w_txt=$this->GetStringWidth($txt);
if($align=='R')
$dx=$w-$w_txt-$this->cMargin;
elseif($align=='C')
$dx=($w-$w_txt)/2;
else
$dx=$this->cMargin;
$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
if($this->ColorFlag)
$s.='q '.$this->TextColor.' ';
$s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ',
($this->x+$dx)*$k,
($this->h-($this->y+.5*$h+(.7+$l-$lines/2)*$this->FontSize))*$k,
$txt);
if($this->underline)
$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
if($this->ColorFlag)
$s.=' Q ';
if($link)
$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
}
}
else { // Single line
$w_txt=$this->GetStringWidth($txt);
$Tz=100;
if ($w_txt>$w-2*$this->cMargin) { // Need compression
$Tz=($w-2*$this->cMargin)/$w_txt*100;
$w_txt=$w-2*$this->cMargin;
}
if($align=='R')
$dx=$w-$w_txt-$this->cMargin;
elseif($align=='C')
$dx=($w-$w_txt)/2;
else
$dx=$this->cMargin;
$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
if($this->ColorFlag)
$s.='q '.$this->TextColor.' ';
$s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ',
($this->x+$dx)*$k,
($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,
$Tz,$txt);
if($this->underline)
$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
if($this->ColorFlag)
$s.=' Q ';
if($link)
$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
}
}
// end change Cell function 12.08.2003
if($s)
$this->_out($s);
$this->lasth=$h;
if($ln>0)
{
//Go to next line
$this->y+=$h;
if($ln==1)
$this->x=$this->lMargin;
}
else
$this->x+=$w;
}
//Ajoute des retours à la ligne si le texte est trop long
function WordWrap(&$text, $maxwidth)
{
$text = trim($text);
if ($text==='')
return 0;
$space = $this->GetStringWidth(' ');
$lines = explode("\n", $text);
$text = '';
$count = 0;
foreach ($lines as $line)
{
$words = preg_split('/ +/', $line);
$width = 0;
foreach ($words as $word)
{
$wordwidth = $this->GetStringWidth($word);
if ($wordwidth > $maxwidth)
{
// Word is too long, we cut it
for($i=0; $i<strlen($word); $i++)
{
$wordwidth = $this->GetStringWidth(substr($word, $i, 1));
if($width + $wordwidth <= $maxwidth)
{
$width += $wordwidth;
$text .= substr($word, $i, 1);
}
else
{
$width = $wordwidth;
$text = rtrim($text)."\n".substr($word, $i, 1);
$count++;
}
}
}
elseif($width + $wordwidth <= $maxwidth)
{
$width += $wordwidth + $space;
$text .= $word.' ';
}
else
{
$width = $wordwidth + $space;
$text = rtrim($text)."\n".$word.' ';
$count++;
}
}
$text = rtrim($text)."\n";
$count++;
}
$text = rtrim($text);
return $count;
}
//Ajoute un chagement de page si necessaire
function CheckPageBreak($h)
{
//Si la hauteur h provoque un débordement, saut de page manuel
if($this->GetY()+$h>$this->PageBreakTrigger)
{
$this->AddPage($this->CurOrientation);
return true;
}
else return false;
}
function Footer() {
// footer for this page
$this->SetY(-15);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
//Transforme une requete sql en pdf
function Mysql2pdf($requete)
{
// like every other '2' util, converts some data to other data.
// In this case, we're taking a query and building a pdf export.
// Configuration
$header = true; // If we want header
$listnull = true;
$fill = 1;
// Execute query
$this->query($requete);
$this->AutoWidth($this->numFields);
//Build PDF
while ($row = mysql_fetch_row($this->results))
{
$string = "";
//print headers, if we need to
if ($header)
{
for ($i = 0; $i < ($this->numFields); $i++)
{
$this->setFillColor(180,180,180);
$colTitles = mysql_field_name($this->results,$i);
//Sauve la position courante
$x=$this->GetX();
$y=$this->GetY();
$this->VCell($this->width,$this->height*2,$colTitles,'LR',0,'L',$fill);
//Dessine le cadre
$this->Rect($x,$y,$this->width,$this->height*2);
}
$fill=!$fill;
$this->Ln();
}
//Fetch all rows till the last one
for ($i = 0; $i < ($this->numFields); $i++)
{
$this->setFillColor(225,225,225);
if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
$string = $row[$i];
//Sauve la position courante
$x=$this->GetX();
$y=$this->GetY();
$this->HCell($this->width,$this->height,$string,'LR',0,'L',$fill);
//Dessine le cadre
$this->Rect($x,$y,$this->width,$this->height);
}
$fill=!$fill;
$this->Ln();
//Effectue un saut de page si nécessaire
$header = $this->CheckPageBreak($this->height) and ($header);
}
}
}
/////PDF creation///////
$pdf = new PDF('L','pt','A3');
$pdf->SetFont('Arial','',8);
$pdf->AliasNbPages();
$pdf->AddPage();
// Connexion à la base de données
$pdf->connect(SERVEUR,NOM,PASSE,BASE);
//$pdf->SetWidth (25); // We use Autowidth
$pdf->SetHeight(20);
$pdf->Mysql2Pdf("SELECT * FROM MyTable");
$pdf->Output('.//FichiersPdf//FichierPdf.pdf');
?> |
Partager