PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/vendors/html2pdf/_mypdf/mypdf.class.php

https://github.com/ata/steak
PHP | 485 lines | 382 code | 65 blank | 38 comment | 85 complexity | ee99f9ddefabf77dacc1913b8b46eebb MD5 | raw file
  1. <?php
  2. /**
  3. * Logiciel : HTML2PDF - classe MyPDF
  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_MYPDF__'))
  12. {
  13. define('__CLASS_MYPDF__', true);
  14. require_once(dirname(__FILE__).'/99_fpdf_protection.class.php'); // classe fpdf_protection
  15. class MyPDF extends FPDF_Protection
  16. {
  17. var $footer_param = array();
  18. var $underline = false;
  19. var $overline = false;
  20. var $linethrough = false;
  21. function MyPDF($sens = 'P', $unit = 'mm', $format = 'A4')
  22. {
  23. $this->underline = false;
  24. $this->overline = false;
  25. $this->linethrough = false;
  26. $this->FPDF_Protection($sens, $unit, $format);
  27. $this->AliasNbPages();
  28. $this->SetMyFooter();
  29. }
  30. function SetMyFooter($page = null, $date = null, $heure = null, $form = null)
  31. {
  32. if ($page===null) $page = null;
  33. if ($date===null) $date = null;
  34. if ($heure===null) $heure = null;
  35. if ($form===null) $form = null;
  36. $this->footer_param = array('page' => $page, 'date' => $date, 'heure' => $heure, 'form' => $form);
  37. }
  38. function Footer()
  39. {
  40. $txt = '';
  41. if ($this->footer_param['form']) $txt = (HTML2PDF::textGET('pdf05'));
  42. if ($this->footer_param['date'] && $this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf03'));
  43. if ($this->footer_param['date'] && !$this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf01'));
  44. if (!$this->footer_param['date'] && $this->footer_param['heure']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf02'));
  45. if ($this->footer_param['page']) $txt.= ($txt ? ' - ' : '').(HTML2PDF::textGET('pdf04'));
  46. $txt = str_replace('[[date_d]]', date('d'), $txt);
  47. $txt = str_replace('[[date_m]]', date('m'), $txt);
  48. $txt = str_replace('[[date_y]]', date('Y'), $txt);
  49. $txt = str_replace('[[date_h]]', date('H'), $txt);
  50. $txt = str_replace('[[date_i]]', date('i'), $txt);
  51. $txt = str_replace('[[date_s]]', date('s'), $txt);
  52. $txt = str_replace('[[current]]', $this->PageNo(), $txt);
  53. $txt = str_replace('[[nb]]', '{nb}', $txt);
  54. if (strlen($txt)>0)
  55. {
  56. $this->SetY(-11);
  57. $this->setOverline(false);
  58. $this->setLinethrough(false);
  59. $this->SetFont('Arial','I',8);
  60. $this->Cell(0, 10, $txt, 0, 0, 'R');
  61. }
  62. }
  63. // redéfinition de la fonction Image de FPDF afin de rajouter la gestion des fichiers PHP
  64. function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
  65. {
  66. //Put an image on the page
  67. if(!isset($this->images[$file]))
  68. {
  69. //First use of this image, get info
  70. if($type=='')
  71. {
  72. /* MODIFICATION HTML2PDF pour le support des images PHP */
  73. $type = explode('?', $file);
  74. $type = pathinfo($type[0]);
  75. if (!isset($type['extension']) || !$type['extension'])
  76. $this->Error('Image file has no extension and no type was specified: '.$file);
  77. $type = $type['extension'];
  78. /* FIN MODIFICATION */
  79. }
  80. $type=strtolower($type);
  81. /* MODIFICATION HTML2PDF pour le support des images PHP */
  82. if ($type=='php')
  83. {
  84. // identification des infos
  85. $infos=@GetImageSize($file);
  86. if (!$infos) $this->Error('Unsupported image : '.$file);
  87. // identification du type
  88. $type = explode('/', $infos['mime']);
  89. if ($type[0]!='image') $this->Error('Unsupported image : '.$file);
  90. $type = $type[1];
  91. }
  92. /* FIN MODIFICATION */
  93. if($type=='jpeg')
  94. $type='jpg';
  95. $mtd='_parse'.$type;
  96. if(!method_exists($this,$mtd))
  97. $this->Error('Unsupported image type: '.$type);
  98. $info=$this->$mtd($file);
  99. $info['i']=count($this->images)+1;
  100. $this->images[$file]=$info;
  101. }
  102. else
  103. $info=$this->images[$file];
  104. //Automatic width and height calculation if needed
  105. if($w==0 && $h==0)
  106. {
  107. //Put image at 72 dpi
  108. $w=$info['w']/$this->k;
  109. $h=$info['h']/$this->k;
  110. }
  111. elseif($w==0)
  112. $w=$h*$info['w']/$info['h'];
  113. elseif($h==0)
  114. $h=$w*$info['h']/$info['w'];
  115. //Flowing mode
  116. if($y===null)
  117. {
  118. if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
  119. {
  120. //Automatic page break
  121. $x2=$this->x;
  122. $this->AddPage($this->CurOrientation,$this->CurPageFormat);
  123. $this->x=$x2;
  124. }
  125. $y=$this->y;
  126. $this->y+=$h;
  127. }
  128. if($x===null)
  129. $x=$this->x;
  130. $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
  131. if($link)
  132. $this->Link($x,$y,$w,$h,$link);
  133. }
  134. // Draw a polygon
  135. // Auteur : Andrew Meier
  136. // Licence : Freeware
  137. function Polygon($points, $style='D')
  138. {
  139. if($style=='F') $op='f';
  140. elseif($style=='FD' or $style=='DF') $op='b';
  141. else $op='s';
  142. $h = $this->h;
  143. $k = $this->k;
  144. $points_string = '';
  145. for($i=0; $i<count($points); $i+=2)
  146. {
  147. $points_string .= sprintf('%.2f %.2f', $points[$i]*$k, ($h-$points[$i+1])*$k);
  148. if($i==0) $points_string .= ' m ';
  149. else $points_string .= ' l ';
  150. }
  151. $this->_out($points_string . $op);
  152. }
  153. function setOverline($value = true)
  154. {
  155. $this->overline = $value;
  156. }
  157. function setLinethrough($value = true)
  158. {
  159. $this->linethrough = $value;
  160. }
  161. // redéfinition de la methode Text de FPDF afin de rajouter la gestion des overline et linethrough
  162. function Text($x, $y, $txt)
  163. {
  164. //Output a string
  165. $s=sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
  166. /* MODIFICATION HTML2PDF pour le support de underline, overline, linethrough */
  167. if ($txt!='')
  168. {
  169. if($this->underline) $s.=' '.$this->_dounderline($x,$y,$txt);
  170. if($this->overline) $s.=' '.$this->_dooverline($x,$y,$txt);
  171. if($this->linethrough) $s.=' '.$this->_dolinethrough($x,$y,$txt);
  172. }
  173. /* FIN MODIFICATION */
  174. if($this->ColorFlag)
  175. $s='q '.$this->TextColor.' '.$s.' Q';
  176. $this->_out($s);
  177. }
  178. // redéfinition de la methode Cell de FPDF afin de rajouter la gestion des overline et linethrough
  179. function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
  180. {
  181. //Output a cell
  182. $k=$this->k;
  183. if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
  184. {
  185. //Automatic page break
  186. $x=$this->x;
  187. $ws=$this->ws;
  188. if($ws>0)
  189. {
  190. $this->ws=0;
  191. $this->_out('0 Tw');
  192. }
  193. $this->AddPage($this->CurOrientation,$this->CurPageFormat);
  194. $this->x=$x;
  195. if($ws>0)
  196. {
  197. $this->ws=$ws;
  198. $this->_out(sprintf('%.3F Tw',$ws*$k));
  199. }
  200. }
  201. if($w==0)
  202. $w=$this->w-$this->rMargin-$this->x;
  203. $s='';
  204. if($fill || $border==1)
  205. {
  206. if($fill)
  207. $op=($border==1) ? 'B' : 'f';
  208. else
  209. $op='S';
  210. $s=sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
  211. }
  212. if(is_string($border))
  213. {
  214. $x=$this->x;
  215. $y=$this->y;
  216. if(strpos($border,'L')!==false)
  217. $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
  218. if(strpos($border,'T')!==false)
  219. $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
  220. if(strpos($border,'R')!==false)
  221. $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  222. if(strpos($border,'B')!==false)
  223. $s.=sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
  224. }
  225. if($txt!=='')
  226. {
  227. if($align=='R')
  228. $dx=$w-$this->cMargin-$this->GetStringWidth($txt);
  229. elseif($align=='C')
  230. $dx=($w-$this->GetStringWidth($txt))/2;
  231. else
  232. $dx=$this->cMargin;
  233. if($this->ColorFlag)
  234. $s.='q '.$this->TextColor.' ';
  235. $txt2=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
  236. $s.=sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
  237. /* MODIFICATION HTML2PDF pour le support de underline, overline, linethrough */
  238. if($this->underline) $s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  239. if($this->overline) $s.=' '.$this->_dooverline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  240. if($this->linethrough) $s.=' '.$this->_dolinethrough($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
  241. /* FIN MODIFICATION */
  242. if($this->ColorFlag)
  243. $s.=' Q';
  244. if($link)
  245. $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
  246. }
  247. if($s)
  248. $this->_out($s);
  249. $this->lasth=$h;
  250. if($ln>0)
  251. {
  252. //Go to next line
  253. $this->y+=$h;
  254. if($ln==1)
  255. $this->x=$this->lMargin;
  256. }
  257. else
  258. $this->x+=$w;
  259. }
  260. function _dounderline($x, $y, $txt)
  261. {
  262. //Underline text
  263. $up=$this->CurrentFont['up'];
  264. $ut=$this->CurrentFont['ut'];
  265. $p_x = $x*$this->k;
  266. $p_y = ($this->h-($y-$up/1000*$this->FontSize))*$this->k;
  267. $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
  268. $p_h = -$ut/1000*$this->FontSizePt;
  269. return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
  270. }
  271. function _dooverline($x, $y, $txt)
  272. {
  273. //Overline text
  274. $up=$this->CurrentFont['up'];
  275. $ut=$this->CurrentFont['ut'];
  276. $p_x = $x*$this->k;
  277. $p_y = ($this->h-($y-(1000+1.5*$up)/1000*$this->FontSize))*$this->k;
  278. $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
  279. $p_h = -$ut/1000*$this->FontSizePt;
  280. return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
  281. }
  282. function _dolinethrough($x, $y, $txt)
  283. {
  284. //Linethrough text
  285. $up=$this->CurrentFont['up'];
  286. $ut=$this->CurrentFont['ut'];
  287. $p_x = $x*$this->k;
  288. $p_y = ($this->h-($y-(1000+2.5*$up)/2000*$this->FontSize))*$this->k;
  289. $p_w = ($this->GetStringWidth($txt)+$this->ws*substr_count($txt,' '))*$this->k;
  290. $p_h = -$ut/1000*$this->FontSizePt;
  291. return sprintf('%.2F %.2F %.2F %.2F re f',$p_x,$p_y,$p_w,$p_h);
  292. }
  293. function clippingPathOpen($x = null, $y = null, $w = null, $h = null, $coin_TL=null, $coin_TR=null, $coin_BL=null, $coin_BR=null)
  294. {
  295. $path = '';
  296. if ($x!==null && $y!==null && $w!==null && $h!==null)
  297. {
  298. $x1 = $x*$this->k;
  299. $y1 = ($this->h-$y)*$this->k;
  300. $x2 = ($x+$w)*$this->k;
  301. $y2 = ($this->h-$y)*$this->k;
  302. $x3 = ($x+$w)*$this->k;
  303. $y3 = ($this->h-$y-$h)*$this->k;
  304. $x4 = $x*$this->k;
  305. $y4 = ($this->h-$y-$h)*$this->k;
  306. if ($coin_TL || $coin_TR || $coin_BL || $coin_BR)
  307. {
  308. if ($coin_TL) { $coin_TL[0] = $coin_TL[0]*$this->k; $coin_TL[1] =-$coin_TL[1]*$this->k; }
  309. if ($coin_TR) { $coin_TR[0] = $coin_TR[0]*$this->k; $coin_TR[1] =-$coin_TR[1]*$this->k; }
  310. if ($coin_BL) { $coin_BL[0] = $coin_BL[0]*$this->k; $coin_BL[1] =-$coin_BL[1]*$this->k; }
  311. if ($coin_BR) { $coin_BR[0] = $coin_BR[0]*$this->k; $coin_BR[1] =-$coin_BR[1]*$this->k; }
  312. $MyArc = 4/3 * (sqrt(2) - 1);
  313. if ($coin_TL)
  314. $path.= sprintf('%.2f %.2f m ', $x1+$coin_TL[0], $y1);
  315. else
  316. $path.= sprintf('%.2f %.2f m ', $x1, $y1);
  317. if ($coin_TR)
  318. {
  319. $xt1 = ($x2-$coin_TR[0])+$coin_TR[0]*$MyArc;
  320. $yt1 = ($y2+$coin_TR[1])-$coin_TR[1];
  321. $xt2 = ($x2-$coin_TR[0])+$coin_TR[0];
  322. $yt2 = ($y2+$coin_TR[1])-$coin_TR[1]*$MyArc;
  323. $path.= sprintf('%.2f %.2f l ', $x2-$coin_TR[0], $y2);
  324. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $x2, $y2+$coin_TR[1]);
  325. }
  326. else
  327. $path.= sprintf('%.2f %.2f l ', $x2, $y2);
  328. if ($coin_BR)
  329. {
  330. $xt1 = ($x3-$coin_BR[0])+$coin_BR[0];
  331. $yt1 = ($y3-$coin_BR[1])+$coin_BR[1]*$MyArc;
  332. $xt2 = ($x3-$coin_BR[0])+$coin_BR[0]*$MyArc;
  333. $yt2 = ($y3-$coin_BR[1])+$coin_BR[1];
  334. $path.= sprintf('%.2f %.2f l ', $x3, $y3-$coin_BR[1]);
  335. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $x3-$coin_BR[0], $y3);
  336. }
  337. else
  338. $path.= sprintf('%.2f %.2f l ', $x3, $y3);
  339. if ($coin_BL)
  340. {
  341. $xt1 = ($x4+$coin_BL[0])-$coin_BL[0]*$MyArc;
  342. $yt1 = ($y4-$coin_BL[1])+$coin_BL[1];
  343. $xt2 = ($x4+$coin_BL[0])-$coin_BL[0];
  344. $yt2 = ($y4-$coin_BL[1])+$coin_BL[1]*$MyArc;
  345. $path.= sprintf('%.2f %.2f l ', $x4+$coin_BL[0], $y4);
  346. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $x4, $y4-$coin_BL[1]);
  347. }
  348. else
  349. $path.= sprintf('%.2f %.2f l ', $x4, $y4);
  350. if ($coin_TL)
  351. {
  352. $xt1 = ($x1+$coin_TL[0])-$coin_TL[0];
  353. $yt1 = ($y1+$coin_TL[1])-$coin_TL[1]*$MyArc;
  354. $xt2 = ($x1+$coin_TL[0])-$coin_TL[0]*$MyArc;
  355. $yt2 = ($y1+$coin_TL[1])-$coin_TL[1];
  356. $path.= sprintf('%.2f %.2f l ', $x1, $y1+$coin_TL[1]);
  357. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $x1+$coin_TL[0], $y1);
  358. }
  359. }
  360. else
  361. {
  362. $path.= sprintf('%.2f %.2f m ', $x1, $y1);
  363. $path.= sprintf('%.2f %.2f l ', $x2, $y2);
  364. $path.= sprintf('%.2f %.2f l ', $x3, $y3);
  365. $path.= sprintf('%.2f %.2f l ', $x4, $y4);
  366. }
  367. $path.= ' h W n';
  368. }
  369. $this->_out('q '.$path.' ');
  370. }
  371. function clippingPathClose()
  372. {
  373. $this->_out(' Q');
  374. }
  375. function drawCourbe($ext1_x, $ext1_y, $ext2_x, $ext2_y, $int1_x, $int1_y, $int2_x, $int2_y, $cen_x, $cen_y)
  376. {
  377. $MyArc = 4/3 * (sqrt(2) - 1);
  378. $ext1_x = $ext1_x*$this->k; $ext1_y = ($this->h-$ext1_y)*$this->k;
  379. $ext2_x = $ext2_x*$this->k; $ext2_y = ($this->h-$ext2_y)*$this->k;
  380. $int1_x = $int1_x*$this->k; $int1_y = ($this->h-$int1_y)*$this->k;
  381. $int2_x = $int2_x*$this->k; $int2_y = ($this->h-$int2_y)*$this->k;
  382. $cen_x = $cen_x*$this->k; $cen_y = ($this->h-$cen_y) *$this->k;
  383. $path = '';
  384. if ($ext1_x-$cen_x!=0)
  385. {
  386. $xt1 = $cen_x+($ext1_x-$cen_x);
  387. $yt1 = $cen_y+($ext2_y-$cen_y)*$MyArc;
  388. $xt2 = $cen_x+($ext1_x-$cen_x)*$MyArc;
  389. $yt2 = $cen_y+($ext2_y-$cen_y);
  390. }
  391. else
  392. {
  393. $xt1 = $cen_x+($ext2_x-$cen_x)*$MyArc;
  394. $yt1 = $cen_y+($ext1_y-$cen_y);
  395. $xt2 = $cen_x+($ext2_x-$cen_x);
  396. $yt2 = $cen_y+($ext1_y-$cen_y)*$MyArc;
  397. }
  398. $path.= sprintf('%.2f %.2f m ', $ext1_x, $ext1_y);
  399. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $ext2_x, $ext2_y);
  400. if ($int1_x-$cen_x!=0)
  401. {
  402. $xt1 = $cen_x+($int1_x-$cen_x)*$MyArc;
  403. $yt1 = $cen_y+($int2_y-$cen_y);
  404. $xt2 = $cen_x+($int1_x-$cen_x);
  405. $yt2 = $cen_y+($int2_y-$cen_y)*$MyArc;
  406. }
  407. else
  408. {
  409. $xt1 = $cen_x+($int2_x-$cen_x);
  410. $yt1 = $cen_y+($int1_y-$cen_y)*$MyArc;
  411. $xt2 = $cen_x+($int2_x-$cen_x)*$MyArc;
  412. $yt2 = $cen_y+($int1_y-$cen_y);
  413. }
  414. $path.= sprintf('%.2f %.2f l ', $int2_x, $int2_y);
  415. $path.= sprintf('%.2f %.2f %.2f %.2f %.2f %.2f c ', $xt1, $yt1, $xt2, $yt2, $int1_x, $int1_y);
  416. $this->_out($path . 'f');
  417. }
  418. }
  419. }
  420. ?>