PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/modules/member/doc/pdf_standard.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 525 lines | 300 code | 66 blank | 159 comment | 93 complexity | e90de285b816b09242a5e0f36c333803 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2003 Steve Dillon
  3. * Copyright (C) 2003 Laurent Passebecq
  4. * Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
  6. * Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /* Inspire de PDF_Label
  22. * PDF_Label - PDF label editing
  23. * @package PDF_Label
  24. * @author Laurent PASSEBECQ <lpasseb@numericable.fr>
  25. * @copyright 2003 Laurent PASSEBECQ
  26. * disponible ici : http://www.fpdf.org/fr/script/script29.php
  27. */
  28. //-------------------------------------------------------------------
  29. // VERSIONS :
  30. // 1.0 : Initial release
  31. // 1.1 : + : Added unit in the constructor
  32. // + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
  33. // + : Added in the description of a label :
  34. // font-size : defaut char size (can be changed by calling Set_Char_Size(xx);
  35. // paper-size : Size of the paper for this sheet (thanx to Al Canton)
  36. // metric : type of unit used in this description
  37. // You can define your label properties in inches by setting metric to 'in'
  38. // and printing in millimiter by setting unit to 'mm' in constructor.
  39. // Added some labels :
  40. // 5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
  41. // 8600 : thanx to Kunal Walia : kunal@u.washington.edu
  42. // + : Added 3mm to the position of labels to avoid errors
  43. ////////////////////////////////////////////////////
  44. /**
  45. * \file htdocs/core/modules/member/doc/pdf_standard.class.php
  46. * \ingroup member
  47. * \brief Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
  48. */
  49. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  51. /**
  52. * Classe afin d'editer au format PDF des cartes de visite au format Avery ou personnalise
  53. */
  54. class pdf_standard
  55. {
  56. var $code; // Code of format
  57. var $format; // Array with informations
  58. // Proprietes privees
  59. var $_Avery_Name = ''; // Nom du format de l'etiquette
  60. var $_Margin_Left = 0; // Marge de gauche de l'etiquette
  61. var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette
  62. var $_X_Space = 0; // Espace horizontal entre 2 bandes d'etiquettes
  63. var $_Y_Space = 0; // Espace vertical entre 2 bandes d'etiquettes
  64. var $_X_Number = 0; // NX Nombre d'etiquettes sur la largeur de la page
  65. var $_Y_Number = 0; // NY Nombre d'etiquettes sur la hauteur de la page
  66. var $_Width = 0; // Largeur de chaque etiquette
  67. var $_Height = 0; // Hauteur de chaque etiquette
  68. var $_Char_Size = 10; // Hauteur des caracteres
  69. var $_Line_Height = 10; // Hauteur par defaut d'une ligne
  70. var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values
  71. var $_Metric_Doc = 'mm'; // Type of metric for the doc..
  72. var $_COUNTX = 1;
  73. var $_COUNTY = 1;
  74. var $_First = 1;
  75. /**
  76. * Constructor
  77. *
  78. * @param DoliDB $db Database handler
  79. */
  80. function __construct($db = '')
  81. {
  82. $this->db = $db;
  83. }
  84. /**
  85. * Methode qui permet de modifier la taille des caracteres
  86. * Cela modiera aussi l'espace entre chaque ligne
  87. *
  88. * @param PDF &$pdf PDF
  89. * @param int $pt point
  90. * @return void
  91. */
  92. function Set_Char_Size(&$pdf,$pt)
  93. {
  94. if ($pt > 3) {
  95. $this->_Char_Size = $pt;
  96. $this->_Line_Height = $this->_Get_Height_Chars($pt);
  97. $pdf->SetFont('','',$pt);
  98. }
  99. }
  100. /**
  101. * On imprime une etiquette
  102. *
  103. * @param PDF &$pdf PDF
  104. * @param string $textleft Textleft
  105. * @param string $header Header
  106. * @param string $footer Footer
  107. * @param Translate $outputlangs Output langs
  108. * @param string $textright Text right
  109. * @param int $idmember Id member
  110. * @param string $photomember Photo member
  111. * @return void
  112. */
  113. function Add_PDF_card(&$pdf,$textleft,$header,$footer,$outputlangs,$textright='',$idmember=0,$photomember='')
  114. {
  115. global $mysoc,$conf,$langs;
  116. // We are in a new page, then we must add a page
  117. if (($this->_COUNTX ==0) and ($this->_COUNTY==0) and (!$this->_First==1)) {
  118. $pdf->AddPage();
  119. }
  120. $this->_First=0;
  121. $_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
  122. $_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
  123. // Define logo
  124. $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  125. if (! is_readable($logo))
  126. {
  127. $logo='';
  128. if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
  129. {
  130. $logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
  131. }
  132. elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
  133. {
  134. $logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
  135. }
  136. }
  137. // Define photo
  138. $dir=$conf->adherent->dir_output;
  139. $file=get_exdir($idmember,2).'photos/'.$photomember;
  140. $photo=$dir.'/'.$file;
  141. if (empty($photomember) || ! is_readable($photo)) $photo='';
  142. // Define background image
  143. $backgroundimage='';
  144. if(! empty($conf->global->ADHERENT_CARD_BACKGROUND) && file_exists($conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND))
  145. {
  146. $backgroundimage=$conf->adherent->dir_output.'/'.$conf->global->ADHERENT_CARD_BACKGROUND;
  147. }
  148. // Print lines
  149. if ($this->code == "CARD")
  150. {
  151. $this->Tformat=$this->_Avery_Labels["CARD"];
  152. //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
  153. $this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
  154. }
  155. // Background
  156. if ($backgroundimage)
  157. {
  158. $pdf->image($backgroundimage,$_PosX,$_PosY,$this->_Width,$this->_Height);
  159. }
  160. // Top
  161. if ($header!='')
  162. {
  163. if ($this->code == "CARD")
  164. {
  165. $pdf->SetDrawColor(128,128,128);
  166. $pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1);
  167. $pdf->SetDrawColor(0,0,0);
  168. }
  169. $pdf->SetXY($_PosX, $_PosY+1);
  170. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
  171. }
  172. // Center
  173. if ($textright=='') // Only a left part
  174. {
  175. if ($textleft == '%LOGO%' && $logo) $this->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  176. else if ($textleft == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  177. else
  178. {
  179. $pdf->SetXY($_PosX+3, $_PosY+3+$this->_Line_Height);
  180. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  181. }
  182. }
  183. else if ($textleft!='' && $textright!='') //
  184. {
  185. if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
  186. {
  187. if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  188. else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  189. $pdf->SetXY($_PosX+21, $_PosY+3+$this->_Line_Height);
  190. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  191. }
  192. else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
  193. {
  194. if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  195. else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  196. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  197. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  198. }
  199. else
  200. {
  201. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  202. $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft), 0, 'L');
  203. $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+3+$this->_Line_Height);
  204. $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  205. }
  206. }
  207. else // Only a right part
  208. {
  209. if ($textright == '%LOGO%' && $logo) $this->Image($logo,$_PosX+$this->_Width-21,$_PosY+1,20);
  210. else if ($textright == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+$this->_Width-21,$_PosY+1,20);
  211. else
  212. {
  213. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  214. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  215. }
  216. }
  217. // Bottom
  218. if ($footer!='')
  219. {
  220. if ($this->code == "CARD")
  221. {
  222. $pdf->SetDrawColor(128,128,128);
  223. $pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
  224. $pdf->SetDrawColor(0,0,0);
  225. }
  226. $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
  227. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
  228. }
  229. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  230. $this->_COUNTY++;
  231. if ($this->_COUNTY == $this->_Y_Number) {
  232. // Si on est en bas de page, on remonte le 'curseur' de position
  233. $this->_COUNTX++;
  234. $this->_COUNTY=0;
  235. }
  236. if ($this->_COUNTX == $this->_X_Number) {
  237. // Si on est en bout de page, alors on repart sur une nouvelle page
  238. $this->_COUNTX=0;
  239. $this->_COUNTY=0;
  240. }
  241. }
  242. /**
  243. * Print dot line
  244. *
  245. * @param PDF &$pdf PDF
  246. * @param int $x1 X1
  247. * @param int $y1 Y1
  248. * @param int $x2 X2
  249. * @param int $y2 Y2
  250. * @param int $epaisseur Epaisseur
  251. * @param int $nbPointilles Nb pointilles
  252. * @return void
  253. */
  254. function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
  255. {
  256. $pdf->SetLineWidth($epaisseur);
  257. $length=abs($x1-$x2);
  258. $hauteur=abs($y1-$y2);
  259. if($length>$hauteur) {
  260. $Pointilles=($length/$nbPointilles)/2; // taille des pointilles
  261. }
  262. else {
  263. $Pointilles=($hauteur/$nbPointilles)/2;
  264. }
  265. for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
  266. for($j=$i;$j<=($i+$Pointilles);$j++) {
  267. if($j<=($x2-1)) {
  268. $pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
  269. $pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
  270. }
  271. }
  272. }
  273. for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
  274. for($j=$i;$j<=($i+$Pointilles);$j++) {
  275. if($j<=($y2-1)) {
  276. $pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
  277. $pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
  278. }
  279. }
  280. }
  281. }
  282. /**
  283. * Fonction realisant une croix aux 4 coins des cartes
  284. *
  285. * @param PDF &$pdf PDF
  286. * @param int $x1 X1
  287. * @param int $y1 Y1
  288. * @param int $x2 X2
  289. * @param int $y2 Y2
  290. * @param int $epaisseur Epaisseur
  291. * @param int $taille Size
  292. * @return void
  293. */
  294. function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
  295. {
  296. $pdf->SetDrawColor(192,192,192);
  297. $pdf->SetLineWidth($epaisseur);
  298. $lg=$taille/2;
  299. // croix haut gauche
  300. $pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
  301. $pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
  302. // croix bas gauche
  303. $pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
  304. $pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
  305. // croix haut droit
  306. $pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
  307. $pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
  308. // croix bas droit
  309. $pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
  310. $pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
  311. $pdf->SetDrawColor(0,0,0);
  312. }
  313. /**
  314. * Convert units (in to mm, mm to in)
  315. * $src and $dest must be 'in' or 'mm'
  316. *
  317. * @param int $value value
  318. * @param string $src from
  319. * @param string $dest to
  320. * @return float value value after conversion
  321. */
  322. function _Convert_Metric ($value, $src, $dest)
  323. {
  324. if ($src != $dest) {
  325. $tab['in'] = 39.37008;
  326. $tab['mm'] = 1000;
  327. return $value * $tab[$dest] / $tab[$src];
  328. } else {
  329. return $value;
  330. }
  331. }
  332. /**
  333. * Give the height for a char size given.
  334. *
  335. * @param int $pt Point
  336. * @return int Height chars
  337. */
  338. function _Get_Height_Chars($pt)
  339. {
  340. // Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
  341. $_Table_Hauteur_Chars = array(6=>2, 7=>2.5, 8=>3, 9=>3.5, 10=>4, 11=>6, 12=>7, 13=>8, 14=>9, 15=>10);
  342. if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
  343. return $_Table_Hauteur_Chars[$pt];
  344. } else {
  345. return 100; // There is a prob..
  346. }
  347. }
  348. /**
  349. * Set format
  350. *
  351. * @param PDF &$pdf PDF
  352. * @param string $format Format
  353. * @return void
  354. */
  355. function _Set_Format(&$pdf, $format)
  356. {
  357. $this->_Metric = $format['metric'];
  358. $this->_Avery_Name = $format['name'];
  359. $this->_Avery_Code = $format['code'];
  360. $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
  361. $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
  362. $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
  363. $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
  364. $this->_X_Number = $format['NX'];
  365. $this->_Y_Number = $format['NY'];
  366. $this->_Width = $this->_Convert_Metric($format['width'], $this->_Metric, $this->_Metric_Doc);
  367. $this->_Height = $this->_Convert_Metric($format['height'], $this->_Metric, $this->_Metric_Doc);
  368. $this->Set_Char_Size($pdf, $format['font-size']);
  369. }
  370. /**
  371. * Function to build PDF on disk, then output on HTTP strem.
  372. *
  373. * @param array $arrayofmembers Array of members informations
  374. * @param Translate $outputlangs Lang object for output language
  375. * @param string $srctemplatepath Full path of source filename for generator using a template file
  376. * @return int 1=OK, 0=KO
  377. */
  378. function write_file($arrayofmembers,$outputlangs,$srctemplatepath)
  379. {
  380. global $user,$conf,$langs,$mysoc,$_Avery_Labels;
  381. $this->code=$srctemplatepath;
  382. $this->Tformat = $_Avery_Labels[$this->code];
  383. if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
  384. $this->type = 'pdf';
  385. $this->format = $this->Tformat['paper-size'];
  386. if (! is_object($outputlangs)) $outputlangs=$langs;
  387. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  388. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  389. $outputlangs->load("main");
  390. $outputlangs->load("dict");
  391. $outputlangs->load("companies");
  392. $outputlangs->load("members");
  393. $outputlangs->load("admin");
  394. $dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
  395. $filename='tmp_cards.pdf';
  396. $file = $dir."/".$filename;
  397. if (! file_exists($dir))
  398. {
  399. if (dol_mkdir($dir) < 0)
  400. {
  401. $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
  402. return 0;
  403. }
  404. }
  405. $pdf=pdf_getInstance($this->format,$this->Tformat['metric']);
  406. if (class_exists('TCPDF'))
  407. {
  408. $pdf->setPrintHeader(false);
  409. $pdf->setPrintFooter(false);
  410. }
  411. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  412. $pdf->SetTitle($outputlangs->transnoentities('MembersCards'));
  413. $pdf->SetSubject($outputlangs->transnoentities("MembersCards"));
  414. $pdf->SetCreator("Speedealing ".DOL_VERSION);
  415. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  416. $pdf->SetKeyWords($outputlangs->transnoentities('MembersCards')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name));
  417. if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
  418. $pdf->SetMargins(0,0);
  419. $pdf->SetAutoPageBreak(false);
  420. $this->_Metric_Doc = $this->Tformat['metric'];
  421. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  422. $posX=1;
  423. $posY=1;
  424. if ($posX > 0) $posX--; else $posX=0;
  425. if ($posY > 0) $posY--; else $posY=0;
  426. $this->_COUNTX = $posX;
  427. $this->_COUNTY = $posY;
  428. $this->_Set_Format($pdf, $this->Tformat);
  429. $pdf->Open();
  430. $pdf->AddPage();
  431. // Add each record
  432. foreach($arrayofmembers as $val)
  433. {
  434. // imprime le texte specifique sur la carte
  435. $this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['id'],$val['photo']);
  436. }
  437. //$pdf->SetXY(10, 295);
  438. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  439. // Output to file
  440. $pdf->Output($file,'F');
  441. if (! empty($conf->global->MAIN_UMASK))
  442. @chmod($file, octdec($conf->global->MAIN_UMASK));
  443. // Output to http stream
  444. clearstatcache();
  445. $attachment=true;
  446. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  447. $type=dol_mimetype($filename);
  448. //if ($encoding) header('Content-Encoding: '.$encoding);
  449. if ($type) header('Content-Type: '.$type);
  450. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  451. else header('Content-Disposition: inline; filename="'.$filename.'"');
  452. // Ajout directives pour resoudre bug IE
  453. header('Cache-Control: Public, must-revalidate');
  454. header('Pragma: public');
  455. readfile($file);
  456. return 1;
  457. }
  458. }
  459. ?>