PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/app/vendors/html2pdf/html2pdf.class.php

https://github.com/ata/steak
PHP | 4302 lines | 2750 code | 560 blank | 992 comment | 403 complexity | af7fa387a21226aff0240c7c665bd32d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Logiciel : HTML2PDF
  4. *
  5. * Convertisseur HTML => PDF, utilise fpdf de Olivier PLATHEY
  6. * Distribué sous la licence GPL.
  7. *
  8. * @author Laurent MINGUET <webmaster@spipu.net>
  9. * @version 3.21 - 05/05/2009
  10. */
  11. if (!defined('__CLASS_HTML2PDF__'))
  12. {
  13. define('__CLASS_HTML2PDF__', '3.21');
  14. // vous pouvez utiliser cette fonction de debug comme suit
  15. // pour voir le temps et la mémoire utilisés (sous linux) pour la conversion :
  16. // echo HTML2PDFgetTimerDebug();
  17. // $html2pdf->WriteHTML($content);
  18. // echo HTML2PDFgetTimerDebug();
  19. function HTML2PDFgetTimerDebug($debug=false)
  20. {
  21. global $TIMER_ACTION_LAST;
  22. list($usec, $sec) = explode(" ", microtime());
  23. $time = (float)$sec + (float)$usec;
  24. $mem = HTML2PDFgetMem();
  25. if (!$TIMER_ACTION_LAST)
  26. {
  27. if ($debug) $ret = null;
  28. else $ret = 'Debug : init'."<br />\n";
  29. }
  30. else
  31. {
  32. $aff_time = $time-$TIMER_ACTION_LAST[0];
  33. $aff_mem = $mem;
  34. if ($debug) $ret = array($aff_time, $aff_mem);
  35. else $ret = 'Timer : '.number_format($aff_time, 3, '.', '').'s - Memory used '.$aff_mem.' Ko'."<br />\n";
  36. }
  37. $TIMER_ACTION_LAST = array($time, $mem);
  38. return $ret;
  39. }
  40. function HTML2PDFgetMem() { return function_exists('memory_get_usage') ? floor(memory_get_usage()/1024) : 0; }
  41. require_once(dirname(__FILE__).'/_mypdf/mypdf.class.php'); // classe mypdf dérivé de fpdf de Olivier PLATHEY
  42. require_once(dirname(__FILE__).'/parsingHTML.class.php'); // classe de parsing HTML
  43. require_once(dirname(__FILE__).'/styleHTML.class.php'); // classe de gestion des styles
  44. global $HTML2PDF_TABLEAU; $HTML2PDF_TABLEAU = array(); // tableau global necessaire à la gestion des tables imbriquées
  45. class HTML2PDF
  46. {
  47. var $langue = 'fr'; // langue des messages
  48. var $sens = 'P'; // sens d'affichage Portrait ou Landscape
  49. var $format = 'A4'; // format de la page : A4, A3, ...
  50. var $background = array(); // informations sur le background
  51. var $testTDin1page = true; // activer le test de TD ne devant pas depasser une page
  52. var $style = null; // objet de style
  53. var $parsing = null; // objet de parsing
  54. var $parse_pos = 0; // position du parsing
  55. var $temp_pos = 0; // position temporaire pour multi tableau
  56. var $sub_html = null; // sous html
  57. var $sub_part = false; // indicateur de sous html
  58. var $isSubPart = false; // indique que le convertisseur courant est un sous html
  59. var $pdf = null; // objet PDF
  60. var $maxX = 0; // zone maxi X
  61. var $maxY = 0; // zone maxi Y
  62. var $FirstPage = true; // premier page
  63. var $defaultLeft = 0; // marges par default de la page
  64. var $defaultTop = 0;
  65. var $defaultRight = 0;
  66. var $defaultBottom = 0;
  67. var $margeLeft = 0; //marges réelles de la page
  68. var $margeTop = 0;
  69. var $margeRight = 0;
  70. var $margeBottom = 0;
  71. var $marges = array();
  72. var $Maxs = array();
  73. var $maxH = 0; // plus grande hauteur dans la ligne, pour saut de ligne à corriger
  74. var $inLink = ''; // indique si on est à l'interieur d'un lien
  75. var $lstAncre = array(); // liste des ancres détectées ou créées
  76. var $subHEADER = array(); // tableau des sous commandes pour faire l'HEADER
  77. var $subFOOTER = array(); // tableau des sous commandes pour faire le FOOTER
  78. var $subSTATES = array(); // tableau de sauvegarde de certains paramètres
  79. var $defLIST = array(); // tableau de sauvegarde de l'etat des UL et OL
  80. var $lstChamps = array(); // liste des champs
  81. var $lstSelect = array(); // options du select en cours
  82. var $previousCall = null; // dernier appel
  83. var $isInTfoot = false; // indique si on est dans un tfoot
  84. var $pageMarges = array(); // marges spécifiques dues aux floats
  85. var $isAfterFloat = false; // indique si on est apres un float
  86. /**
  87. * Constructeur
  88. *
  89. * @param string sens portrait ou landscape
  90. * @param string format A4, A5, ...
  91. * @param string langue : fr, en, it...
  92. * @param array marges par defaut, dans l'ordre (left, top, right, bottom)
  93. * @param boolean forcer la création de la premiere page, ne pas utiliser, c'est utilisé en interne pour la gestion des tableaux
  94. * @return null
  95. */
  96. function HTML2PDF($sens = 'P', $format = 'A4', $langue='fr', $marges = array(5, 5, 5, 8), $force_page = false)
  97. {
  98. // sauvegarde des paramètres
  99. $this->sens = $sens;
  100. $this->format = $format;
  101. $this->FirstPage = true;
  102. $this->langue = strtolower($langue);
  103. $this->setTestTdInOnePage(true);
  104. // chargement du fichier de langue
  105. $this->textLOAD($this->langue);
  106. // création de l' objet PDF
  107. $this->pdf = new MyPDF($sens, 'mm', $format);
  108. // initialisation des styles
  109. $this->style = new styleHTML($this->pdf);
  110. $this->style->FontSet();
  111. $this->defLIST = array();
  112. // initialisation du parsing
  113. $this->parsing = new parsingHTML();
  114. $this->sub_html = null;
  115. $this->sub_part = false;
  116. // initialisation des marges
  117. $this->setDefaultMargins($marges[0], $marges[1], $marges[2], $marges[3]);
  118. $this->setMargins();
  119. $this->marges = array();
  120. // initialisation des champs de formulaire
  121. $this->lstChamps = array();
  122. // premier page forcée
  123. if ($force_page) $this->setNewPage($this->sens);
  124. }
  125. /**
  126. * activer ou desactiver le test de TD ne devant pas depasser une page
  127. *
  128. * @param boolean nouvel etat
  129. * @return boolean ancien etat
  130. */
  131. function setTestTdInOnePage($mode = true)
  132. {
  133. $old = $this->testTDin1page;
  134. $this->testTDin1page = $mode ? true : false;
  135. return $old;
  136. }
  137. /**
  138. * définir les marges par défault
  139. *
  140. * @param int en mm, marge left
  141. * @param int en mm, marge top
  142. * @param int en mm, marge right. si null, left=right
  143. * @param int en mm, marge bottom. si null, bottom=8
  144. * @return null
  145. */
  146. function setDefaultMargins($left, $top, $right = null, $bottom = null)
  147. {
  148. if ($right===null) $right = $left;
  149. if ($bottom===null) $bottom = 8;
  150. $this->defaultLeft = $this->style->ConvertToMM($left.'mm');
  151. $this->defaultTop = $this->style->ConvertToMM($top.'mm');
  152. $this->defaultRight = $this->style->ConvertToMM($right.'mm');
  153. $this->defaultBottom = $this->style->ConvertToMM($bottom.'mm');
  154. }
  155. /**
  156. * définir les marges réelles, fonctions de la balise page
  157. *
  158. * @return null
  159. */
  160. function setMargins()
  161. {
  162. $this->margeLeft = $this->defaultLeft + (isset($this->background['left']) ? $this->background['left'] : 0);
  163. $this->margeRight = $this->defaultRight + (isset($this->background['right']) ? $this->background['right'] : 0);
  164. $this->margeTop = $this->defaultTop + (isset($this->background['top']) ? $this->background['top'] : 0);
  165. $this->margeBottom = $this->defaultBottom + (isset($this->background['bottom']) ? $this->background['bottom'] : 0);
  166. $this->pdf->SetMargins($this->margeLeft, $this->margeTop, $this->margeRight);
  167. $this->pdf->cMargin = 0;
  168. $this->pdf->SetAutoPageBreak(false, $this->margeBottom);
  169. }
  170. /**
  171. * recuperer les positions x minimales et maximales en fonction d'une hauteur
  172. *
  173. * @param float y
  174. * @return array(float, float)
  175. */
  176. function getMargins($y)
  177. {
  178. $y = floor($y*100);
  179. $x = array($this->pdf->lMargin, $this->pdf->w-$this->pdf->rMargin);
  180. foreach($this->pageMarges as $m_y => $m_x)
  181. if ($m_y<=$y) $x = $m_x;
  182. return $x;
  183. }
  184. /**
  185. * ajouter une marge suite a un float
  186. *
  187. * @param string left ou right
  188. * @param float x1
  189. * @param float y1
  190. * @param float x2
  191. * @param float y2
  192. * @return null
  193. */
  194. function addMargins($float, $x1, $y1, $x2, $y2)
  195. {
  196. $old1 = $this->getMargins($y1);
  197. $old2 = $this->getMargins($y2);
  198. if ($float=='left') $old1[0] = $x2;
  199. if ($float=='right') $old1[1] = $x1;
  200. $y1 = floor($y1*100);
  201. $y2 = floor($y2*100);
  202. foreach($this->pageMarges as $m_y => $m_x)
  203. {
  204. if ($m_y<$y1) continue;
  205. if ($m_y>$y2) break;
  206. if ($float=='left' && $this->pageMarges[$m_y][0]<$x2) unset($this->pageMarges[$m_y]);
  207. if ($float=='right' && $this->pageMarges[$m_y][1]>$x1) unset($this->pageMarges[$m_y]);
  208. }
  209. $this->pageMarges[$y1] = $old1;
  210. $this->pageMarges[$y2] = $old2;
  211. ksort($this->pageMarges);
  212. $this->isAfterFloat = true;
  213. }
  214. /**
  215. * définir des nouvelles marges et sauvegarder les anciennes
  216. *
  217. * @param float marge left
  218. * @param float marge top
  219. * @param float marge right
  220. * @return null
  221. */
  222. function saveMargin($ml, $mt, $mr)
  223. {
  224. $this->marges[] = array('l' => $this->pdf->lMargin, 't' => $this->pdf->tMargin, 'r' => $this->pdf->rMargin, 'page' => $this->pageMarges);
  225. $this->pdf->SetMargins($ml, $mt, $mr);
  226. $this->pageMarges = array();
  227. $this->pageMarges[floor($mt*100)] = array($ml, $this->pdf->w-$mr);
  228. }
  229. /**
  230. * récuperer les dernières marches sauvées
  231. *
  232. * @return null
  233. */
  234. function loadMargin()
  235. {
  236. $old = array_pop($this->marges);
  237. if ($old)
  238. {
  239. $ml = $old['l'];
  240. $mt = $old['t'];
  241. $mr = $old['r'];
  242. $mP = $old['page'];
  243. }
  244. else
  245. {
  246. $ml = $this->margeLeft;
  247. $mt = 0;
  248. $mr = $this->margeRight;
  249. $mP = array($mt => array($ml, $this->pdf->w-$mr));
  250. }
  251. $this->pdf->SetMargins($ml, $mt, $mr);
  252. $this->pageMarges = $mP;
  253. }
  254. /**
  255. * permet d'ajouter une fonte.
  256. *
  257. * @param string nom de la fonte
  258. * @param string style de la fonte
  259. * @param string fichier de la fonte
  260. * @return null
  261. */
  262. function AddFont($family, $style='', $file='')
  263. {
  264. $this->pdf->AddFont($family, $style, $file);
  265. }
  266. /**
  267. * sauvegarder l'état actuelle des maximums
  268. *
  269. * @return null
  270. */
  271. function saveMax()
  272. {
  273. $this->Maxs[] = array($this->maxX, $this->maxY, $this->maxH);
  274. }
  275. /**
  276. * charger le dernier état sauvé des maximums
  277. *
  278. * @return null
  279. */
  280. function loadMax()
  281. {
  282. $old = array_pop($this->Maxs);
  283. if ($old)
  284. {
  285. $this->maxX = $old[0];
  286. $this->maxY = $old[1];
  287. $this->maxH = $old[2];
  288. }
  289. else
  290. {
  291. $this->maxX = 0;
  292. $this->maxY = 0;
  293. $this->maxH = 0;
  294. }
  295. }
  296. /**
  297. * afficher l'header contenu dans page_header
  298. *
  299. * @return null
  300. */
  301. function SetPageHeader()
  302. {
  303. if (!count($this->subHEADER)) return false;
  304. $OLD_parse_pos = $this->parse_pos;
  305. $OLD_parse_code = $this->parsing->code;
  306. $this->parse_pos = 0;
  307. $this->parsing->code = $this->subHEADER;
  308. $this->MakeHTMLcode();
  309. $this->parse_pos = $OLD_parse_pos;
  310. $this->parsing->code = $OLD_parse_code;
  311. }
  312. /**
  313. * afficher le footer contenu dans page_footer
  314. *
  315. * @return null
  316. */
  317. function SetPageFooter()
  318. {
  319. if (!count($this->subFOOTER)) return false;
  320. $OLD_parse_pos = $this->parse_pos;
  321. $OLD_parse_code = $this->parsing->code;
  322. $this->parse_pos = 0;
  323. $this->parsing->code = $this->subFOOTER;
  324. $this->MakeHTMLcode();
  325. $this->parse_pos = $OLD_parse_pos;
  326. $this->parsing->code = $OLD_parse_code;
  327. }
  328. /**
  329. * saut de ligne avec une hauteur spécifique
  330. *
  331. * @param float hauteur de la ligne
  332. * @return null
  333. */
  334. function setNewLine($h)
  335. {
  336. $this->pdf->Ln($h);
  337. list($lx, $rx) = $this->getMargins($this->pdf->y);
  338. $this->pdf->x=$lx;
  339. }
  340. /**
  341. * création d'une nouvelle page avec une orientation particuliere
  342. *
  343. * @param string sens P=portrait ou L=landscape
  344. * @param array tableau des propriétés du fond de la page
  345. * @return null
  346. */
  347. function setNewPage($orientation = '', $background = null)
  348. {
  349. /*
  350. if (!$this->FirstPage)
  351. {
  352. $info = debug_backtrace(); foreach($info as $k => $v) { unset($info[$k]['object']); unset($info[$k]['type']); unset($info[$k]['args']);}
  353. echo '<pre>'.print_r($info, true).'</pre><hr>';
  354. }
  355. */
  356. $this->FirstPage = false;
  357. $this->sens = $orientation ? $orientation : $this->sens;
  358. $this->background = $background!==null ? $background : $this->background;
  359. $this->maxY = 0;
  360. $this->maxX = 0;
  361. $this->pdf->lMargin = $this->defaultLeft;
  362. $this->pdf->rMargin = $this->defaultRight;
  363. $this->pdf->tMargin = $this->defaultTop;
  364. $this->pdf->AddPage($this->sens);
  365. if (!$this->sub_part && !$this->isSubPart)
  366. {
  367. if (is_array($this->background))
  368. {
  369. if (isset($this->background['color']) && $this->background['color'])
  370. {
  371. $this->pdf->SetFillColor($this->background['color'][0], $this->background['color'][1], $this->background['color'][2]);
  372. $this->pdf->Rect(0, 0, $this->pdf->w, $this->pdf->h, 'F');
  373. }
  374. if (isset($this->background['img']) && $this->background['img'])
  375. $this->pdf->Image($this->background['img'], $this->background['posX'], $this->background['posY'], $this->background['width']);
  376. }
  377. $this->SetPageHeader();
  378. $this->SetPageFooter();
  379. }
  380. $this->SetMargins();
  381. $this->pdf->y = $this->margeTop;
  382. list($lx, $rx) = $this->getMargins($this->pdf->y);
  383. $this->pdf->x=$lx;
  384. }
  385. /**
  386. * récupération du PDF
  387. *
  388. * @param string nom du fichier PDF
  389. * @param boolean destination
  390. * @return string contenu éventuel du pdf
  391. *
  392. *
  393. * Destination où envoyer le document. Le paramètre peut prendre les valeurs suivantes :
  394. * true : equivalent à I
  395. * false : equivalent à S
  396. * I : envoyer en inline au navigateur. Le plug-in est utilisé s'il est installé. Le nom indiqué dans name est utilisé lorsque l'on sélectionne "Enregistrer sous" sur le lien générant le PDF.
  397. * D : envoyer au navigateur en forçant le téléchargement, avec le nom indiqué dans name.
  398. * F : sauver dans un fichier local, avec le nom indiqué dans name (peut inclure un répertoire).
  399. * S : renvoyer le document sous forme de chaîne. name est ignoré.
  400. */
  401. function Output($name = '', $dest = false)
  402. {
  403. // nettoyage
  404. global $HTML2PDF_TABLEAU; $HTML2PDF_TABLEAU = array();
  405. // interpretation des paramètres
  406. if ($dest===false) $dest = 'I';
  407. if ($dest===true) $dest = 'S';
  408. if ($dest==='') $dest = 'I';
  409. if ($name=='') $name='document.pdf';
  410. // verification de la destination
  411. $dest = strtoupper($dest);
  412. if (!in_array($dest, array('I', 'D', 'F', 'S'))) $dest = 'I';
  413. // verification du nom
  414. if (strtolower(substr($name, -4))!='.pdf')
  415. {
  416. echo 'ERROR : The output document name "'.$name.'" is not a PDF name';
  417. exit;
  418. }
  419. return $this->pdf->Output($name, $dest);
  420. }
  421. /**
  422. * création d'un sous HTML2PDF pour la gestion des tableaux imbriqués
  423. *
  424. * @param HTML2PDF futur sous HTML2PDF passé en référence pour créatio
  425. * @return null
  426. */
  427. function CreateSubHTML(&$sub_html, $cellmargin=0)
  428. {
  429. // initialisation du sous objet
  430. $sub_html = new HTML2PDF(
  431. $this->sens,
  432. $this->format,
  433. $this->langue,
  434. array($this->defaultLeft,$this->defaultTop,$this->defaultRight,$this->defaultBottom),
  435. true
  436. );
  437. $sub_html->isSubPart = true;
  438. $sub_html->setTestTdInOnePage($this->testTDin1page);
  439. $sub_html->style->css = $this->style->css;
  440. $sub_html->style->css_keys = $this->style->css_keys;
  441. $sub_html->style->table = $this->style->table;
  442. $sub_html->style->value = $this->style->value;
  443. $sub_html->style->value['text-align'] = 'left';
  444. $sub_html->defLIST = $this->defLIST;
  445. // initialisation de la largeur
  446. if ($this->style->value['width'])
  447. {
  448. $marge = $cellmargin*2;
  449. $marge+= $this->style->value['padding']['l'] + $this->style->value['padding']['r'];
  450. $marge+= $this->style->value['border']['l']['width'] + $this->style->value['border']['r']['width'];
  451. $marge = $sub_html->pdf->w - $this->style->value['width'] + $marge;
  452. }
  453. else
  454. $marge = $this->margeLeft+$this->margeRight;
  455. $sub_html->saveMargin(0, 0, $marge);
  456. // initialisation des fontes
  457. $sub_html->pdf->fonts = &$this->pdf->fonts;
  458. $sub_html->pdf->FontFiles = &$this->pdf->FontFiles;
  459. $sub_html->pdf->diffs = &$this->pdf->diffs;
  460. // initialisation des positions et autre
  461. $sub_html->maxX = 0;
  462. $sub_html->maxY = 0;
  463. $sub_html->maxH = 0;
  464. $sub_html->pdf->setX(0);
  465. $sub_html->pdf->setY(0);
  466. $sub_html->style->FontSet();
  467. }
  468. /**
  469. * destruction d'un sous HTML2PDF pour la gestion des tableaux imbriqués
  470. *
  471. * @return null
  472. */
  473. function DestroySubHTML()
  474. {
  475. unset($this->sub_html);
  476. $this->sub_html = null;
  477. }
  478. /**
  479. * Convertir un nombre arabe en nombre romain
  480. *
  481. * @param integer nombre à convertir
  482. * @return string nombre converti
  483. */
  484. function listeArab2Rom($nb_ar)
  485. {
  486. $nb_b10 = array('I','X','C','M');
  487. $nb_b5 = array('V','L','D');
  488. $nb_ro = '';
  489. if ($nb_ar<1) return $nb_ar;
  490. if ($nb_ar>3999) return $nb_ar;
  491. for($i=3; $i>=0 ; $i--)
  492. {
  493. $chiffre=floor($nb_ar/pow(10,$i));
  494. if($chiffre>=1)
  495. {
  496. $nb_ar=$nb_ar-$chiffre*pow(10,$i);
  497. if($chiffre<=3)
  498. {
  499. for($j=$chiffre; $j>=1; $j--)
  500. {
  501. $nb_ro=$nb_ro.$nb_b10[$i];
  502. }
  503. }
  504. else if($chiffre==9)
  505. {
  506. $nb_ro=$nb_ro.$nb_b10[$i].$nb_b10[$i+1];
  507. }
  508. elseif($chiffre==4)
  509. {
  510. $nb_ro=$nb_ro.$nb_b10[$i].$nb_b5[$i];
  511. }
  512. else
  513. {
  514. $nb_ro=$nb_ro.$nb_b5[$i];
  515. for($j=$chiffre-5; $j>=1; $j--)
  516. {
  517. $nb_ro=$nb_ro.$nb_b10[$i];
  518. }
  519. }
  520. }
  521. }
  522. return $nb_ro;
  523. }
  524. /**
  525. * Ajouter un LI au niveau actuel
  526. *
  527. * @return null
  528. */
  529. function listeAddLi()
  530. {
  531. $this->defLIST[count($this->defLIST)-1]['nb']++;
  532. }
  533. function listeGetWidth() { return '7mm'; }
  534. function listeGetPadding() { return '1mm'; }
  535. /**
  536. * Recuperer le LI du niveau actuel
  537. *
  538. * @return string chaine à afficher
  539. */
  540. function listeGetLi()
  541. {
  542. $im = $this->defLIST[count($this->defLIST)-1]['img'];
  543. $st = $this->defLIST[count($this->defLIST)-1]['style'];
  544. $nb = $this->defLIST[count($this->defLIST)-1]['nb'];
  545. $up = (substr($st, 0, 6)=='upper-');
  546. if ($im) return array(false, false, $im);
  547. switch($st)
  548. {
  549. case 'none':
  550. return array('arial', true, ' ');
  551. case 'upper-alpha':
  552. case 'lower-alpha':
  553. $str = '';
  554. while($nb>26)
  555. {
  556. $str = chr(96+$nb%26).$str;
  557. $nb = floor($nb/26);
  558. }
  559. $str = chr(96+$nb).$str;
  560. return array('arial', false, ($up ? strtoupper($str) : $str).'.');
  561. case 'upper-roman':
  562. case 'lower-roman':
  563. $str = $this->listeArab2Rom($nb);
  564. return array('arial', false, ($up ? strtoupper($str) : $str).'.');
  565. case 'decimal':
  566. return array('arial', false, $nb.'.');
  567. case 'square':
  568. return array('zapfdingbats', true, chr(110));
  569. case 'circle':
  570. return array('zapfdingbats', true, chr(109));
  571. case 'disc':
  572. default:
  573. return array('zapfdingbats', true, chr(108));
  574. }
  575. }
  576. /**
  577. * Ajouter un niveau de liste
  578. *
  579. * @param string type de liste : ul, ol
  580. * @param string style de la liste
  581. * @return null
  582. */
  583. function listeAddLevel($type = 'ul', $style = '', $img = null)
  584. {
  585. if ($img)
  586. {
  587. if (preg_match('/^url\(([^)]+)\)$/isU', trim($img), $match))
  588. $img = $match[1];
  589. else
  590. $img = null;
  591. }
  592. else
  593. $img = null;
  594. if (!in_array($type, array('ul', 'ol'))) $type = 'ul';
  595. if (!in_array($style, array('lower-alpha', 'upper-alpha', 'upper-roman', 'lower-roman', 'decimal', 'square', 'circle', 'disc', 'none'))) $style = '';
  596. if (!$style)
  597. {
  598. if ($type=='ul') $style = 'disc';
  599. else $style = 'decimal';
  600. }
  601. $this->defLIST[count($this->defLIST)] = array('style' => $style, 'nb' => 0, 'img' => $img);
  602. }
  603. /**
  604. * Supprimer un niveau de liste
  605. *
  606. * @return null
  607. */
  608. function listeDelLevel()
  609. {
  610. if (count($this->defLIST))
  611. {
  612. unset($this->defLIST[count($this->defLIST)-1]);
  613. $this->defLIST = array_values($this->defLIST);
  614. }
  615. }
  616. /**
  617. * traitement d'un code HTML
  618. *
  619. * @param string code HTML à convertir
  620. * @param boolean afficher en pdf (false) ou en html (true)
  621. * @return null
  622. */
  623. function WriteHTML($html, $vue = false)
  624. {
  625. $html = str_replace('[[page_nb]]', '{nb}', $html);
  626. $html = str_replace('[[date_y]]', date('Y'), $html);
  627. $html = str_replace('[[date_m]]', date('m'), $html);
  628. $html = str_replace('[[date_d]]', date('d'), $html);
  629. $html = str_replace('[[date_h]]', date('H'), $html);
  630. $html = str_replace('[[date_i]]', date('i'), $html);
  631. $html = str_replace('[[date_s]]', date('s'), $html);
  632. // si on veut voir le résultat en HTML => on appelle la fonction
  633. if ($vue) $this->vueHTML($html);
  634. // sinon, traitement pour conversion en PDF :
  635. // parsing
  636. $this->sub_pdf = false;
  637. $this->style->readStyle($html);
  638. $this->parsing->setHTML($html);
  639. $this->parsing->parse();
  640. $this->MakeHTMLcode();
  641. }
  642. function MakeHTMLcode()
  643. {
  644. // pour chaque element identifié par le parsing
  645. for($this->parse_pos=0; $this->parse_pos<count($this->parsing->code); $this->parse_pos++)
  646. {
  647. // récupération de l'élément
  648. $todo = $this->parsing->code[$this->parse_pos];
  649. // si c'est une ouverture de tableau
  650. if (in_array($todo['name'], array('table', 'ul', 'ol')) && !$todo['close'])
  651. {
  652. // on va créer un sous HTML, et on va travailler sur une position temporaire
  653. $tag_open = $todo['name'];
  654. $this->sub_part = true;
  655. $this->temp_pos = $this->parse_pos;
  656. // pour tous les éléments jusqu'à la fermeture de la table afin de préparer les dimensions
  657. while(isset($this->parsing->code[$this->temp_pos]) && !($this->parsing->code[$this->temp_pos]['name']==$tag_open && $this->parsing->code[$this->temp_pos]['close']))
  658. {
  659. $this->loadAction($this->parsing->code[$this->temp_pos]);
  660. $this->temp_pos++;
  661. }
  662. if (isset($this->parsing->code[$this->temp_pos])) $this->loadAction($this->parsing->code[$this->temp_pos]);
  663. $this->sub_part = false;
  664. }
  665. // chargement de l'action correspondant à l'élément
  666. $this->loadAction($todo);
  667. }
  668. }
  669. /**
  670. * affichage en mode HTML du contenu
  671. *
  672. * @param string contenu
  673. * @return null
  674. */
  675. function vueHTML($content)
  676. {
  677. $content = preg_replace('/<page_header([^>]*)>/isU', '<hr>'.HTML2PDF::textGET('vue01').' : $1<hr><div$1>', $content);
  678. $content = preg_replace('/<page_footer([^>]*)>/isU', '<hr>'.HTML2PDF::textGET('vue02').' : $1<hr><div$1>', $content);
  679. $content = preg_replace('/<page([^>]*)>/isU', '<hr>'.HTML2PDF::textGET('vue03').' : $1<hr><div$1>', $content);
  680. $content = preg_replace('/<\/page([^>]*)>/isU', '</div><hr>', $content);
  681. $content = preg_replace('/<bookmark([^>]*)>/isU', '<hr>bookmark : $1<hr>', $content);
  682. $content = preg_replace('/<\/bookmark([^>]*)>/isU', '', $content);
  683. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  684. <html>
  685. <head>
  686. <title>'.HTML2PDF::textGET('vue04').' HTML</title>
  687. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
  688. </head>
  689. <body style="padding: 10px; font-size: 10pt;font-family: Arial;">
  690. '.$content.'
  691. </body>
  692. </html>';
  693. exit;
  694. }
  695. /**
  696. * chargement de l'action correspondante à un element de parsing
  697. *
  698. * @param array élément de parsing
  699. * @return null
  700. */
  701. function loadAction($row)
  702. {
  703. // nom de l'action
  704. $fnc = ($row['close'] ? 'c_' : 'o_').strtoupper($row['name']);
  705. // parametres de l'action
  706. $param = $row['param'];
  707. // si aucune page n'est créé, on la créé
  708. if ($fnc!='o_PAGE' && $this->FirstPage)
  709. {
  710. $this->setNewPage();
  711. }
  712. // lancement de l'action
  713. if (is_callable(array(&$this, $fnc)))
  714. {
  715. $this->{$fnc}($param);
  716. $this->previousCall = $fnc;
  717. }
  718. else
  719. {
  720. HTML2PDF::makeError(1, __FILE__, __LINE__, strtoupper($row['name']));
  721. }
  722. }
  723. /**
  724. * balise : PAGE
  725. * mode : OUVERTURE
  726. *
  727. * @param array paramètres de l'élément de parsing
  728. * @return null
  729. */
  730. function o_PAGE($param)
  731. {
  732. $newPageSet= (!isset($param['pageset']) || $param['pageset']!='old');
  733. $this->maxH = 0;
  734. if ($newPageSet)
  735. {
  736. $this->subHEADER = array();
  737. $this->subFOOTER = array();
  738. // identification de l'orientation demandée
  739. $orientation = '';
  740. if (isset($param['orientation']))
  741. {
  742. $param['orientation'] = strtolower($param['orientation']);
  743. if ($param['orientation']=='p') $orientation = 'P';
  744. if ($param['orientation']=='portrait') $orientation = 'P';
  745. if ($param['orientation']=='l') $orientation = 'L';
  746. if ($param['orientation']=='paysage') $orientation = 'L';
  747. if ($param['orientation']=='landscape') $orientation = 'L';
  748. }
  749. // identification des propriétés du background
  750. $background = array();
  751. if (isset($param['backimg']))
  752. {
  753. $background['img'] = isset($param['backimg']) ? $param['backimg'] : ''; // nom de l'image
  754. $background['posX'] = isset($param['backimgx']) ? $param['backimgx'] : 'center'; // position horizontale de l'image
  755. $background['posY'] = isset($param['backimgy']) ? $param['backimgy'] : 'middle'; // position verticale de l'image
  756. $background['width'] = isset($param['backimgw']) ? $param['backimgw'] : '100%'; // taille de l'image (100% = largueur de la feuille)
  757. // conversion du nom de l'image, en cas de paramètres en _GET
  758. $background['img'] = str_replace('&amp;', '&', $background['img']);
  759. // conversion des positions
  760. if ($background['posX']=='left') $background['posX'] = '0%';
  761. if ($background['posX']=='center') $background['posX'] = '50%';
  762. if ($background['posX']=='right') $background['posX'] = '100%';
  763. if ($background['posY']=='top') $background['posY'] = '0%';
  764. if ($background['posY']=='middle') $background['posY'] = '50%';
  765. if ($background['posY']=='bottom') $background['posY'] = '100%';
  766. // si il y a une image de précisé
  767. if ($background['img'])
  768. {
  769. // est-ce que c'est une image ?
  770. $infos=@GetImageSize($background['img']);
  771. if (count($infos)>1)
  772. {
  773. // taille de l'image, en fonction de la taille spécifiée.
  774. $Wi = $this->style->ConvertToMM($background['width'], $this->pdf->w);
  775. $Hi = $Wi*$infos[1]/$infos[0];
  776. // récupération des dimensions et positions de l'image
  777. $background['width'] = $Wi;
  778. $background['posX'] = $this->style->ConvertToMM($background['posX'], $this->pdf->w - $Wi);
  779. $background['posY'] = $this->style->ConvertToMM($background['posY'], $this->pdf->h - $Hi);
  780. }
  781. else
  782. $background = array();
  783. }
  784. else
  785. $background = array();
  786. }
  787. // marges TOP et BOTTOM pour le texte.
  788. $background['top'] = isset($param['backtop']) ? $param['backtop'] : '0';
  789. $background['bottom'] = isset($param['backbottom']) ? $param['backbottom'] : '0';
  790. $background['left'] = isset($param['backleft']) ? $param['backleft'] : '0';
  791. $background['right'] = isset($param['backright']) ? $param['backright'] : '0';
  792. if (preg_match('/^([0-9]*)$/isU', $background['top'])) $background['top'] .= 'mm';
  793. if (preg_match('/^([0-9]*)$/isU', $background['bottom'])) $background['bottom'] .= 'mm';
  794. if (preg_match('/^([0-9]*)$/isU', $background['left'])) $background['left'] .= 'mm';
  795. if (preg_match('/^([0-9]*)$/isU', $background['right'])) $background['right'] .= 'mm';
  796. $background['top'] = $this->style->ConvertToMM($background['top'], $this->pdf->h);
  797. $background['bottom'] = $this->style->ConvertToMM($background['bottom'], $this->pdf->h);
  798. $background['left'] = $this->style->ConvertToMM($background['left'], $this->pdf->w);
  799. $background['right'] = $this->style->ConvertToMM($background['right'], $this->pdf->w);
  800. $res = false;
  801. $background['color'] = isset($param['backcolor']) ? $this->style->ConvertToRVB($param['backcolor'], $res) : null;
  802. if (!$res) $background['color'] = null;
  803. $this->style->save();
  804. $this->style->analyse('PAGE', $param);
  805. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  806. $this->style->FontSet();
  807. // nouvelle page
  808. $this->setNewPage($orientation, $background);
  809. // footer automatique
  810. if (isset($param['footer']))
  811. {
  812. $lst = explode(';', $param['footer']);
  813. foreach($lst as $key => $val) $lst[$key] = trim(strtolower($val));
  814. $page = in_array('page', $lst);
  815. $date = in_array('date', $lst);
  816. $heure = in_array('heure', $lst);
  817. $form = in_array('form', $lst);
  818. }
  819. else
  820. {
  821. $page = null;
  822. $date = null;
  823. $heure = null;
  824. $form = null;
  825. }
  826. $this->pdf->SetMyFooter($page, $date, $heure, $form);
  827. }
  828. else
  829. {
  830. $this->style->save();
  831. $this->style->analyse('PAGE', $param);
  832. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  833. $this->style->FontSet();
  834. $this->setNewPage();
  835. }
  836. }
  837. /**
  838. * balise : PAGE
  839. * mode : FERMETURE
  840. *
  841. * @param array paramètres de l'élément de parsing
  842. * @return null
  843. */
  844. function c_PAGE($param)
  845. {
  846. $this->maxH = 0;
  847. $this->style->load();
  848. $this->style->FontSet();
  849. }
  850. function o_PAGE_HEADER($param)
  851. {
  852. $this->subHEADER = array();
  853. for($this->parse_pos; $this->parse_pos<count($this->parsing->code); $this->parse_pos++)
  854. {
  855. $todo = $this->parsing->code[$this->parse_pos];
  856. if ($todo['name']=='page_header') $todo['name']='page_header_sub';
  857. $this->subHEADER[] = $todo;
  858. if (strtolower($todo['name'])=='page_header_sub' && $todo['close']) break;
  859. }
  860. $this->SetPageHeader();
  861. }
  862. function o_PAGE_FOOTER($param)
  863. {
  864. $this->subFOOTER = array();
  865. for($this->parse_pos; $this->parse_pos<count($this->parsing->code); $this->parse_pos++)
  866. {
  867. $todo = $this->parsing->code[$this->parse_pos];
  868. if ($todo['name']=='page_footer') $todo['name']='page_footer_sub';
  869. $this->subFOOTER[] = $todo;
  870. if (strtolower($todo['name'])=='page_footer_sub' && $todo['close']) break;
  871. }
  872. $this->SetPageFooter();
  873. }
  874. function o_PAGE_HEADER_SUB($param)
  875. {
  876. $this->subSTATES = array();
  877. $this->subSTATES['x'] = $this->pdf->x;
  878. $this->subSTATES['y'] = $this->pdf->y;
  879. $this->subSTATES['s'] = $this->style->value;
  880. $this->subSTATES['t'] = $this->style->table;
  881. $this->subSTATES['ml'] = $this->pdf->lMargin;
  882. $this->subSTATES['mr'] = $this->pdf->rMargin;
  883. $this->subSTATES['mt'] = $this->pdf->tMargin;
  884. $this->subSTATES['mb'] = $this->pdf->bMargin;
  885. $this->pdf->x = $this->defaultLeft;
  886. $this->pdf->y = $this->defaultTop;
  887. $this->style->initStyle();
  888. $this->style->resetStyle();
  889. $this->style->value['width'] = $this->pdf->w - $this->defaultLeft - $this->defaultRight;
  890. $this->style->table = array();
  891. $this->pdf->lMargin = $this->defaultLeft;
  892. $this->pdf->rMargin = $this->defaultRight;
  893. $this->pdf->tMargin = $this->defaultTop;
  894. $this->pdf->bMargin = $this->defaultBottom;
  895. $this->pdf->PageBreakTrigger = $this->pdf->h - $this->pdf->bMargin;
  896. $this->style->save();
  897. $this->style->analyse('page_header_sub', $param);
  898. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  899. $this->style->FontSet();
  900. }
  901. function c_PAGE_HEADER_SUB($param)
  902. {
  903. $this->style->load();
  904. $this->pdf->x = $this->subSTATES['x'];
  905. $this->pdf->y = $this->subSTATES['y'];
  906. $this->style->value = $this->subSTATES['s'];
  907. $this->style->table = $this->subSTATES['t'];
  908. $this->pdf->lMargin = $this->subSTATES['ml'];
  909. $this->pdf->rMargin = $this->subSTATES['mr'];
  910. $this->pdf->tMargin = $this->subSTATES['mt'];
  911. $this->pdf->bMargin = $this->subSTATES['mb'];
  912. $this->pdf->PageBreakTrigger = $this->pdf->h - $this->pdf->bMargin;
  913. $this->style->FontSet();
  914. }
  915. function o_PAGE_FOOTER_SUB($param)
  916. {
  917. $this->subSTATES = array();
  918. $this->subSTATES['x'] = $this->pdf->x;
  919. $this->subSTATES['y'] = $this->pdf->y;
  920. $this->subSTATES['s'] = $this->style->value;
  921. $this->subSTATES['t'] = $this->style->table;
  922. $this->subSTATES['ml'] = $this->pdf->lMargin;
  923. $this->subSTATES['mr'] = $this->pdf->rMargin;
  924. $this->subSTATES['mt'] = $this->pdf->tMargin;
  925. $this->subSTATES['mb'] = $this->pdf->bMargin;
  926. $this->pdf->x = $this->defaultLeft;
  927. $this->pdf->y = $this->defaultTop;
  928. $this->style->initStyle();
  929. $this->style->resetStyle();
  930. $this->style->value['width'] = $this->pdf->w - $this->defaultLeft - $this->defaultRight;
  931. $this->style->table = array();
  932. $this->pdf->lMargin = $this->defaultLeft;
  933. $this->pdf->rMargin = $this->defaultRight;
  934. $this->pdf->tMargin = $this->defaultTop;
  935. $this->pdf->bMargin = $this->defaultBottom;
  936. $this->pdf->PageBreakTrigger = $this->pdf->h - $this->pdf->bMargin;
  937. // on en créé un sous HTML que l'on transforme en PDF
  938. // pour récupérer la hauteur
  939. // on extrait tout ce qui est contenu dans le FOOTER
  940. $sub = null;
  941. $res = $this->parsing->getLevel($this->parse_pos);
  942. $this->CreateSubHTML($sub);
  943. $sub->writeHTML($res[1]);
  944. $this->pdf->y = $this->pdf->h - $sub->maxY - $this->defaultBottom - 0.01;
  945. unset($sub);
  946. $this->style->save();
  947. $this->style->analyse('page_footer_sub', $param);
  948. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  949. $this->style->FontSet();
  950. }
  951. function c_PAGE_FOOTER_SUB($param)
  952. {
  953. $this->style->load();
  954. $this->pdf->x = $this->subSTATES['x'];
  955. $this->pdf->y = $this->subSTATES['y'];
  956. $this->style->value = $this->subSTATES['s'];
  957. $this->style->table = $this->subSTATES['t'];
  958. $this->pdf->lMargin = $this->subSTATES['ml'];
  959. $this->pdf->rMargin = $this->subSTATES['mr'];
  960. $this->pdf->tMargin = $this->subSTATES['mt'];
  961. $this->pdf->bMargin = $this->subSTATES['mb'];
  962. $this->pdf->PageBreakTrigger = $this->pdf->h - $this->pdf->bMargin;
  963. $this->style->FontSet();
  964. }
  965. /**
  966. * balise : NOBREAK
  967. * mode : OUVERTURE
  968. *
  969. * @param array paramètres de l'élément de parsing
  970. * @return null
  971. */
  972. function o_NOBREAK($param)
  973. {
  974. $this->maxH = 0;
  975. // on extrait tout ce qui est contenu dans le NOBREAK
  976. $res = $this->parsing->getLevel($this->parse_pos);
  977. // on en créé un sous HTML que l'on transforme en PDF
  978. // pour analyse les dimensions
  979. // et voir si ca rentre
  980. $sub = null;
  981. $this->CreateSubHTML($sub);
  982. $sub->writeHTML($res[1]);
  983. $y = $this->pdf->getY();
  984. if (
  985. $sub->maxY < ($this->pdf->h - $this->pdf->tMargin-$this->pdf->bMargin) &&
  986. $y + $sub->maxY>=($this->pdf->h - $this->pdf->bMargin)
  987. )
  988. $this->setNewPage();
  989. unset($sub);
  990. }
  991. /**
  992. * balise : NOBREAK
  993. * mode : FERMETURE
  994. *
  995. * @param array paramètres de l'élément de parsing
  996. * @return null
  997. */
  998. function c_NOBREAK($param)
  999. {
  1000. $this->maxH = 0;
  1001. }
  1002. /**
  1003. * balise : DIV
  1004. * mode : OUVERTURE
  1005. *
  1006. * @param array paramètres de l'élément de parsing
  1007. * @return null
  1008. */
  1009. function o_DIV($param, $other = 'div')
  1010. {
  1011. $this->style->save();
  1012. $this->style->analyse($other, $param);
  1013. $this->style->FontSet();
  1014. $align_object = null;
  1015. if ($this->style->value['margin-auto']) $align_object = 'center';
  1016. $marge = array();
  1017. $marge['l'] = $this->style->value['border']['l']['width'] + $this->style->value['padding']['l']+0.03;
  1018. $marge['r'] = $this->style->value['border']['r']['width'] + $this->style->value['padding']['r']+0.03;
  1019. $marge['t'] = $this->style->value['border']['t']['width'] + $this->style->value['padding']['t']+0.03;
  1020. $marge['b'] = $this->style->value['border']['b']['width'] + $this->style->value['padding']['b']+0.03;
  1021. // on extrait tout ce qui est contenu dans la DIV
  1022. $res = $this->parsing->getLevel($this->parse_pos);
  1023. // on en créé un sous HTML que l'on transforme en PDF
  1024. // pour analyse les dimensions
  1025. $w = 0; $h = 0;
  1026. if (trim($res[1]))
  1027. {
  1028. $sub = null;
  1029. $this->CreateSubHTML($sub);
  1030. $sub->writeHTML($res[1]);
  1031. $w = $sub->maxX;
  1032. $h = $sub->maxY;
  1033. unset($sub);
  1034. }
  1035. $w+= $marge['l']+$marge['r'];
  1036. $h+= $marge['t']+$marge['b'];
  1037. $this->style->value['width'] = max($w, $this->style->value['width']);
  1038. $this->style->value['height'] = max($h, $this->style->value['height']);
  1039. if (!$this->style->value['position'])
  1040. {
  1041. if (
  1042. $this->style->value['width'] < ($this->pdf->w - $this->pdf->lMargin-$this->pdf->rMargin) &&
  1043. $this->pdf->x + $this->style->value['width']>=($this->pdf->w - $this->pdf->rMargin)
  1044. )
  1045. $this->o_BR(array());
  1046. if (
  1047. $this->style->value['height'] < ($this->pdf->h - $this->pdf->tMargin-$this->pdf->bMargin) &&
  1048. $this->pdf->y + $this->style->value['height']>=($this->pdf->h - $this->pdf->bMargin)
  1049. )
  1050. $this->setNewPage();
  1051. // en cas d'alignement => correction
  1052. $w = $this->style->value['width'];
  1053. $old = isset($this->style->table[count($this->style->table)-1]) ? $this->style->table[count($this->style->table)-1] : $this->style->value;
  1054. $parent_w = $old['width'] ? $old['width'] : $this->pdf->w - $this->pdf->lMargin - $this->pdf->rMargin;
  1055. if ($parent_w>$w)
  1056. {
  1057. if ($align_object=='center') $this->pdf->x = $this->pdf->x + ($parent_w-$w)*0.5;
  1058. else if ($align_object=='right') $this->pdf->x = $this->pdf->x + $parent_w-$w;
  1059. }
  1060. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  1061. }
  1062. else
  1063. {
  1064. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  1065. $this->saveMax();
  1066. $this->saveX = 0;
  1067. $this->saveY = 0;
  1068. $this->saveH = 0;
  1069. }
  1070. // initialisation du style des bordures de la premiere partie de tableau
  1071. $this->Rectangle(
  1072. $this->style->value['x'],
  1073. $this->style->value['y'],
  1074. $this->style->value['width'],
  1075. $this->style->value['height'],
  1076. $this->style->value['border'],
  1077. $this->style->value['padding'],
  1078. 0,
  1079. $this->style->value['background']
  1080. );
  1081. $marge = array();
  1082. $marge['l'] = $this->style->value['border']['l']['width'] + $this->style->value['padding']['l']+0.03;
  1083. $marge['r'] = $this->style->value['border']['r']['width'] + $this->style->value['padding']['r']+0.03;
  1084. $marge['t'] = $this->style->value['border']['t']['width'] + $this->style->value['padding']['t']+0.03;
  1085. $marge['b'] = $this->style->value['border']['b']['width'] + $this->style->value['padding']['b']+0.03;
  1086. $this->style->value['width'] = $this->style->value['width']-$marge['l']-$marge['r'];
  1087. $this->style->value['height'] = $this->style->value['height']-$marge['r']-$marge['b'];
  1088. // limitation des marges aux dimensions de la div
  1089. $mL = $this->style->value['x']+$marge['l'];
  1090. $mR = $this->pdf->w - $mL - $this->style->value['width'];
  1091. $this->saveMargin($mL, 0, $mR);
  1092. // positionnement en fonction
  1093. $h_corr = $this->style->value['height'];
  1094. $h_reel = $h-$marge['b']-$marge['t'];
  1095. switch($this->style->value['vertical-align'])
  1096. {
  1097. case 'bottom':
  1098. $y_corr = $h_corr-$h_reel;
  1099. break;
  1100. case 'middle':
  1101. $y_corr = ($h_corr-$h_reel)*0.5;
  1102. break;
  1103. case 'top':
  1104. default:
  1105. $y_corr = 0;
  1106. break;
  1107. }
  1108. $this->pdf->setX($this->style->value['x']+$marge['l']);
  1109. $this->pdf->setY($this->style->value['y']+$marge['t']+$y_corr);
  1110. }
  1111. function o_BLOCKQUOTE($param) { $this->o_DIV($param, 'blockquote'); }
  1112. /**
  1113. * balise : DIV
  1114. * mode : FERMETURE
  1115. *
  1116. * @param array paramètres de l'élément de parsing
  1117. * @return null
  1118. */
  1119. function c_DIV($param)
  1120. {
  1121. $marge = array();
  1122. $marge['l'] = $this->style->value['border']['l']['width'] + $this->style->value['padding']['l']+0.03;
  1123. $marge['r'] = $this->style->value['border']['r']['width'] + $this->style->value['padding']['r']+0.03;
  1124. $marge['t'] = $this->style->value['border']['t']['width'] + $this->style->value['padding']['t']+0.03;
  1125. $marge['b'] = $this->style->value['border']['b']['width'] + $this->style->value['padding']['b']+0.03;
  1126. $x = $this->style->value['x'];
  1127. $y = $this->style->value['y'];
  1128. $w = $this->style->value['width']+$marge['l']+$marge['r'];
  1129. $h = $this->style->value['height']+$marge['t']+$marge['b'];
  1130. // correction pour les margins
  1131. $w+= $this->style->value['margin']['r'];
  1132. $h+= $this->style->value['margin']['b'];
  1133. if ($this->style->value['position']!='absolute')
  1134. {
  1135. // position
  1136. $this->pdf->x = $x+$w;
  1137. $this->pdf->y = $y;
  1138. // position MAX
  1139. $this->maxX = max($this->maxX, $x+$w);
  1140. $this->maxY = max($this->maxY, $y+$h);
  1141. $this->maxH = max($this->maxH, $h);
  1142. }
  1143. else
  1144. {
  1145. // position
  1146. $this->pdf->x = $this->style->value['xc'];
  1147. $this->pdf->y = $this->style->value['yc'];
  1148. $this->loadMax();
  1149. }
  1150. $block = ($this->style->value['display']!='inline' && $this->style->value['position']!='absolute');
  1151. $this->style->load();
  1152. $this->style->FontSet();
  1153. $this->loadMargin();
  1154. if ($block) $this->o_BR(array());
  1155. }
  1156. function c_BLOCKQUOTE($param) { $this->c_DIV($param); }
  1157. /**
  1158. * balise : BARCODE
  1159. * mode : OUVERTURE
  1160. *
  1161. * @param array paramètres de l'élément de parsing
  1162. * @return null
  1163. */
  1164. function o_BARCODE($param)
  1165. {
  1166. $lst_barcode = array(
  1167. 'EAN13' => '0.35mm',
  1168. 'UPC_A' => '0.35mm',
  1169. 'CODE39' => '1.00mm',
  1170. );
  1171. if (isset($param['type'])) $param['type'] = strtoupper($param['type']);
  1172. if (!isset($param['type']) || !isset($lst_barcode[$param['type']])) $param['type']=='CODE39';
  1173. if (!isset($param['value'])) $param['value'] = 0;
  1174. if (!isset($param['bar_w'])) $param['bar_w'] = $lst_barcode[$param['type']];
  1175. if (!isset($param['bar_h'])) $param['bar_h'] = '10mm';
  1176. if (!isset($param['style']['color'])) $param['style']['color'] = '#000000';
  1177. $param['style']['background-color'] = $param['style']['color'];
  1178. $this->style->save();
  1179. $this->style->analyse('barcode', $param);
  1180. $this->style->setPosition($this->pdf->x, $this->pdf->y);
  1181. $this->style->FontSet();
  1182. $x = $this->pdf->getX();
  1183. $y = $this->pdf->getY();
  1184. $w = $this->style->ConvertToMM($param['bar_w']);
  1185. $h = $this->style->ConvertToMM($param['bar_h']);
  1186. $infos = $this->pdf->{'BARCODE_'.$param['type']}($x, $y, $param['value'], $h, $w);
  1187. // position maximale globale
  1188. $this->maxX = max($this->maxX, $x+$infos[0]);
  1189. $this->maxY = max($this->maxY, $y+$infos[1]);
  1190. $this->maxH = max($this->maxH, $infos[1]);
  1191. $this->pdf->setX($x+$infos[0]);
  1192. $this->style->load();
  1193. $this->style->FontSet();
  1194. }
  1195. /**
  1196. * balise : BARCODE
  1197. * mode : FERMETURE
  1198. *
  1199. * @param array paramètres de l'élément de parsing
  1200. * @return null
  1201. */
  1202. function c_BARCODE($param)
  1203. {
  1204. // completement inutile
  1205. }
  1206. /**
  1207. * balise : BOOKMARK
  1208. * mode : OUVERTURE
  1209. *
  1210. * @param array paramètres de l'élément de parsing
  1211. * @return null
  1212. */
  1213. function o_BOOKMARK($param)
  1214. {
  1215. $titre = isset($param['title']) ? trim($param['title']) : '';
  1216. $level = isset($param['level']) ? floor($param['level']) : 0;
  1217. if ($level<0) $level = 0;
  1218. if ($titre) $this->pdf->Bookmark($titre, $level, -1);
  1219. }
  1220. /**
  1221. * balise : BOOKMARK
  1222. * mode : FERMETURE
  1223. *
  1224. * @param array paramètres de l'élément de parsing
  1225. * @return null
  1226. */
  1227. function c_BOOKMARK($param)
  1228. {
  1229. // completement inutile
  1230. }
  1231. /**
  1232. * balise : WRITE
  1233. * mode : OUVERTURE
  1234. *
  1235. * @param array paramètres de l'élément de parsing
  1236. * @return null
  1237. */
  1238. function o_WRITE($param)
  1239. {
  1240. $fill = false; //($this->style->value['background']['color']!=null);
  1241. // récupération du texte à écrire, et conversion
  1242. $txt = $param['txt'];
  1243. $txt = str_replace('&euro;', '€', $txt);
  1244. if ($this->isAfterFloat)
  1245. {
  1246. $txt = preg_replace('/^([\s]*)([^\s])/isU', '$2', $txt);
  1247. $this->isAfterFloat = false;
  1248. }
  1249. $txt = html_entity_decode($txt, ENT_QUOTES, 'ISO-8859-15');
  1250. // $txt = utf8_decode(html_entity_decode($txt, ENT_QUOTES, 'UTF-8'));
  1251. $txt = str_replace('[[page_cu]]', $this->pdf->PageNo(), $txt);
  1252. // tailles du texte
  1253. $h = 1.08*$this->style->value['font-size'];
  1254. $dh = $h*$this->style->value['mini-decal'];
  1255. $w = $this->pdf->GetStringWidth($txt);
  1256. // identification de l'alignement
  1257. $align = 'L';
  1258. if ($this->style->value['text-align']!='left')
  1259. {
  1260. $w = $this->style->value['width'];
  1261. if ($this->style->value['text-align']=='center') $align = 'C';
  1262. if ($this->style->value['text-align']=='right') $align = 'R';
  1263. }
  1264. $maxX = 0; // plus grande largeur du texte apres retour à la ligne
  1265. $x = $this->pdf->getX(); // position du texte
  1266. $y = $this->pdf->getY();
  1267. $w = $this->pdf->GetStringWidth($txt); // largeur du texte
  1268. list($left, $right) = $this->getMargins($y); // marges autorisees
  1269. $nb = 0; // nbr de lignes découpées
  1270. // tant que ca ne rentre pas sur la ligne et qu'on a du texte => on découpe
  1271. while($x+$w>$right && $x<$right && strlen($txt))
  1272. {
  1273. // liste des mots
  1274. $lst = explode(' ', $txt);
  1275. // trouver une phrase qui rentre dans la largeur, en ajoutant les mots 1 à 1
  1276. $i=0;
  1277. $old = '';
  1278. $str = $lst[0];
  1279. while(($x+$this->pdf->GetStringWidth($str))<=$right)
  1280. {
  1281. unset($lst[$i]);
  1282. $old = $str;
  1283. $i++;
  1284. $str.= ' '.$lst[$i];
  1285. }
  1286. $str = $old;
  1287. // si rien de rentre, et que le premier mot ne rentre de toute facon pas dans une ligne, on le force...
  1288. if ($i==0 && (($left+$this->pdf->GetStringWidth($lst[0]))>=$right))
  1289. {
  1290. $str = $lst[0];
  1291. unset($lst[0]);
  1292. }
  1293. // récupération des mots restant, et calcul de la largeur
  1294. $txt = implode(' ', $lst);
  1295. $w = $this->pdf->GetStringWidth($str);
  1296. // ecriture du bout de phrase extrait et qui rentre
  1297. $wc = ($align=='L' ? $w : $this->style->value['width']);
  1298. if ($right - $left<$wc) $wc = $right - $left;
  1299. $this->pdf->Cell($wc, $h+$dh, $str, 0, 0, $align, $fill, $this->inLink);
  1300. $this->maxH = max($this->maxH, $this->style->getLineHeight());
  1301. // détermination de la largeur max
  1302. $maxX = max($maxX, $this->pdf->getX());
  1303. // nouvelle position et nouvelle largeur pour la boucle
  1304. $w = $this->pdf->GetStringWidth($txt);
  1305. $y = $this->pdf->getY();
  1306. $x = $this->pdf->getX();
  1307. // si il reste du text à afficher
  1308. if (strlen($txt))
  1309. {
  1310. // retour à la ligne
  1311. $this->o_BR(array('style' => ''));
  1312. $y = $this->pdf->getY();
  1313. $x = $this->pdf->getX();
  1314. // si la prochaine ligne ne rentre pas dans la page => nouvelle page
  1315. if ($y + $h>$this->pdf->h - $this->pdf->bMargin) $this->setNewPage();
  1316. // ligne suplémentaire. au bout de 1000 : trop long => erreur
  1317. $nb++;
  1318. if ($nb>1000) HTML2PDF::makeError(2, __FILE__, __LINE__, array($txt, $right-$left, $this->pdf->GetStringWidth($txt)));
  1319. list($left, $right) = $this->getMargins($y); // marges autorisees
  1320. }
  1321. }
  1322. // si il reste du text apres découpe, c'est qu'il rentre direct => on l'affiche
  1323. if (strlen($txt))
  1324. {
  1325. $this->pdf->Cell(($align=='L' ? $w : $this->style->value['width']), $h+$dh, $txt, 0, 0, $align, $fill, $this->inLink);
  1326. $this->maxH = max($this->maxH, $this->style->getLineHeight());
  1327. }
  1328. // détermination des positions MAX
  1329. $maxX = max($maxX, $this->pdf->getX());
  1330. $maxY = $this->pdf->getY()+$h;
  1331. // position maximale globale
  1332. $this->maxX = max($this->maxX, $maxX);
  1333. $this->maxY = max($this->maxY, $maxY);
  1334. }
  1335. /**
  1336. * tracer une image
  1337. *
  1338. * @param string nom du fichier source
  1339. * @return null
  1340. */
  1341. function Image($src, $sub_li=false)
  1342. {
  1343. // est-ce que c'est une image ?
  1344. $infos=@GetImageSize($src);
  1345. if (count($infos)<2)
  1346. {
  1347. HTML2PDF::makeError(6, __FILE__, __LINE__, $src);
  1348. return false;
  1349. }
  1350. // récupération des dimensions dans l'unité du PDF
  1351. $wi = $infos[0]/$this->pdf->k;
  1352. $hi = $infos[1]/$this->pdf->k;
  1353. // détermination des dimensions d'affichage en fonction du style
  1354. if ($this->style->value['width'] && $this->style->value['height'])
  1355. {
  1356. $w = $this->style->value['width'];
  1357. $h = $this->style->value['height'];
  1358. }
  1359. else if ($this->style->value['width'])
  1360. {
  1361. $w = $this->style->value['width'];
  1362. $h = $hi*$w/$wi;
  1363. }
  1364. else if ($this->style->value['height'])
  1365. {
  1366. $h = $this->style->value['height'];
  1367. $w = $wi*$h/$hi;
  1368. }
  1369. else
  1370. {
  1371. $w = 72./96.*$wi;
  1372. $h = 72./96.*$hi;
  1373. }
  1374. // detection du float
  1375. $float = $this->style->getFloat();
  1376. if ($float && $this->maxH) $this->o_BR(array());
  1377. // position d'affichage
  1378. $x = $this->pdf->getX();
  1379. $y = $this->pdf->getY();
  1380. // si l'image ne rentre pas dans la page => nouvelle page
  1381. if ($y + $h>$this->pdf->h - $this-…

Large files files are truncated, but you can click here to view the full file