PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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