PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/modules/member/labels/pdf_standardlabel.class.php

https://github.com/asterix14/dolibarr
PHP | 453 lines | 283 code | 61 blank | 109 comment | 89 complexity | a14175d6ac2d95a8162607acfca6678a 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/labels/pdf_standardlabel.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_standardlabel
  65. * \brief Classe afin d'editer au format PDF des pages d'etiquette adresse au format Avery ou personnalise
  66. */
  67. class pdf_standardlabel {
  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='')
  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. // Print lines
  131. if ($this->code == "CARD")
  132. {
  133. $this->Tformat=$this->_Avery_Labels["CARD"];
  134. //$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
  135. $this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
  136. }
  137. // Top
  138. if ($header!='')
  139. {
  140. if ($this->code == "CARD")
  141. {
  142. $pdf->SetDrawColor(128,128,128);
  143. $pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1);
  144. $pdf->SetDrawColor(0,0,0);
  145. }
  146. $pdf->SetXY($_PosX, $_PosY+1);
  147. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
  148. }
  149. // Center
  150. if ($textright=='') // Only a left part
  151. {
  152. if ($textleft == '%LOGO%' && $logo) $this->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  153. else if ($textleft == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  154. else
  155. {
  156. $pdf->SetXY($_PosX+3, $_PosY+3+$this->_Line_Height);
  157. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
  158. }
  159. }
  160. else if ($textleft!='' && $textright!='') //
  161. {
  162. if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
  163. {
  164. if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  165. else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+2,$_PosY+3+$this->_Line_Height,20);
  166. $pdf->SetXY($_PosX+21, $_PosY+3+$this->_Line_Height);
  167. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  168. }
  169. else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
  170. {
  171. if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  172. else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-21,$_PosY+3+$this->_Line_Height,20);
  173. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  174. $pdf->MultiCell($this->_Width-22, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
  175. }
  176. else
  177. {
  178. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  179. $pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft));
  180. $pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+3+$this->_Line_Height);
  181. $pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  182. }
  183. }
  184. else // Only a right part
  185. {
  186. if ($textright == '%LOGO%' && $logo) $this->Image($logo,$_PosX+$this->_Width-21,$_PosY+1,20);
  187. else if ($textright == '%PHOTO%' && $photo) $this->Image($photo,$_PosX+$this->_Width-21,$_PosY+1,20);
  188. else
  189. {
  190. $pdf->SetXY($_PosX+2, $_PosY+3+$this->_Line_Height);
  191. $pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
  192. }
  193. }
  194. // Bottom
  195. if ($footer!='')
  196. {
  197. if ($this->code == "CARD")
  198. {
  199. $pdf->SetDrawColor(128,128,128);
  200. $pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
  201. $pdf->SetDrawColor(0,0,0);
  202. }
  203. $pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
  204. $pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
  205. }
  206. //print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
  207. $this->_COUNTY++;
  208. if ($this->_COUNTY == $this->_Y_Number) {
  209. // Si on est en bas de page, on remonte le 'curseur' de position
  210. $this->_COUNTX++;
  211. $this->_COUNTY=0;
  212. }
  213. if ($this->_COUNTX == $this->_X_Number) {
  214. // Si on est en bout de page, alors on repart sur une nouvelle page
  215. $this->_COUNTX=0;
  216. $this->_COUNTY=0;
  217. }
  218. }
  219. function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
  220. {
  221. $pdf->SetLineWidth($epaisseur);
  222. $length=abs($x1-$x2);
  223. $hauteur=abs($y1-$y2);
  224. if($length>$hauteur) {
  225. $Pointilles=($length/$nbPointilles)/2; // taille des pointilles
  226. }
  227. else {
  228. $Pointilles=($hauteur/$nbPointilles)/2;
  229. }
  230. for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
  231. for($j=$i;$j<=($i+$Pointilles);$j++) {
  232. if($j<=($x2-1)) {
  233. $pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
  234. $pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
  235. }
  236. }
  237. }
  238. for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
  239. for($j=$i;$j<=($i+$Pointilles);$j++) {
  240. if($j<=($y2-1)) {
  241. $pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
  242. $pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
  243. }
  244. }
  245. }
  246. }
  247. /*
  248. * Fonction realisant une croix aux 4 coins des cartes
  249. */
  250. function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
  251. {
  252. $pdf->SetDrawColor(192,192,192);
  253. $pdf->SetLineWidth($epaisseur);
  254. $lg=$taille/2;
  255. // croix haut gauche
  256. $pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
  257. $pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
  258. // croix bas gauche
  259. $pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
  260. $pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
  261. // croix haut droit
  262. $pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
  263. $pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
  264. // croix bas droit
  265. $pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
  266. $pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
  267. $pdf->SetDrawColor(0,0,0);
  268. }
  269. // convert units (in to mm, mm to in)
  270. // $src and $dest must be 'in' or 'mm'
  271. function _Convert_Metric ($value, $src, $dest) {
  272. if ($src != $dest) {
  273. $tab['in'] = 39.37008;
  274. $tab['mm'] = 1000;
  275. return $value * $tab[$dest] / $tab[$src];
  276. } else {
  277. return $value;
  278. }
  279. }
  280. // Give the height for a char size given.
  281. function _Get_Height_Chars($pt) {
  282. // Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
  283. $_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);
  284. if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
  285. return $_Table_Hauteur_Chars[$pt];
  286. } else {
  287. return 100; // There is a prob..
  288. }
  289. }
  290. function _Set_Format(&$pdf, $format) {
  291. $this->_Metric = $format['metric'];
  292. $this->_Avery_Name = $format['name'];
  293. $this->_Avery_Code = $format['code'];
  294. $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
  295. $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
  296. $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
  297. $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
  298. $this->_X_Number = $format['NX'];
  299. $this->_Y_Number = $format['NY'];
  300. $this->_Width = $this->_Convert_Metric($format['width'], $this->_Metric, $this->_Metric_Doc);
  301. $this->_Height = $this->_Convert_Metric($format['height'], $this->_Metric, $this->_Metric_Doc);
  302. $this->Set_Char_Size($pdf, $format['font-size']);
  303. }
  304. /**
  305. * \brief Function to build PDF on disk, then output on HTTP strem.
  306. * \param arrayofmembers Array of members informations
  307. * \param outputlangs Lang object for output language
  308. * \return int 1=ok, 0=ko
  309. */
  310. function write_file($arrayofmembers,$outputlangs)
  311. {
  312. global $user,$conf,$langs,$mysoc,$_Avery_Labels;
  313. // Choose type (L7163 by default)
  314. $this->code=empty($conf->global->ADHERENT_ETIQUETTE_TYPE)?'L7163':$conf->global->ADHERENT_ETIQUETTE_TYPE;
  315. $this->Tformat = $_Avery_Labels[$this->code];
  316. if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
  317. $this->type = 'pdf';
  318. $this->format = $this->Tformat['paper-size'];
  319. if (! is_object($outputlangs)) $outputlangs=$langs;
  320. // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
  321. if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
  322. $outputlangs->load("main");
  323. $outputlangs->load("dict");
  324. $outputlangs->load("companies");
  325. $outputlangs->load("members");
  326. $outputlangs->load("admin");
  327. $dir = $conf->adherent->dir_temp;
  328. $file = $dir . "/tmplabels.pdf";
  329. if (! file_exists($dir))
  330. {
  331. if (create_exdir($dir) < 0)
  332. {
  333. $this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
  334. return 0;
  335. }
  336. }
  337. $pdf=pdf_getInstance($this->format,$this->Tformat['metric']);
  338. if (class_exists('TCPDF'))
  339. {
  340. $pdf->setPrintHeader(false);
  341. $pdf->setPrintFooter(false);
  342. }
  343. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  344. $pdf->SetTitle($outputlangs->transnoentities('MembersLabels'));
  345. $pdf->SetSubject($outputlangs->transnoentities("MembersLabels"));
  346. $pdf->SetCreator("Dolibarr ".DOL_VERSION);
  347. $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
  348. $pdf->SetKeyWords($outputlangs->transnoentities('MembersLabels')." ".$outputlangs->transnoentities("Foundation")." ".$outputlangs->convToOutputCharset($mysoc->name));
  349. if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) $pdf->SetCompression(false);
  350. $pdf->SetMargins(0,0);
  351. $pdf->SetAutoPageBreak(false);
  352. $this->_Metric_Doc = $this->Tformat['metric'];
  353. // Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
  354. $posX=1;
  355. $posY=1;
  356. if ($posX > 0) $posX--; else $posX=0;
  357. if ($posY > 0) $posY--; else $posY=0;
  358. $this->_COUNTX = $posX;
  359. $this->_COUNTY = $posY;
  360. $this->_Set_Format($pdf, $this->Tformat);
  361. $pdf->Open();
  362. $pdf->AddPage();
  363. // Add each record
  364. foreach($arrayofmembers as $val)
  365. {
  366. // imprime le texte specifique sur la carte
  367. $this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['id'],$val['photo']);
  368. }
  369. //$pdf->SetXY(10, 295);
  370. //$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
  371. // Output to file
  372. $pdf->Output($file,'F');
  373. if (! empty($conf->global->MAIN_UMASK))
  374. @chmod($file, octdec($conf->global->MAIN_UMASK));
  375. // Output to http stream
  376. clearstatcache();
  377. $attachment=true;
  378. if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
  379. $filename='tmplabels.pdf';
  380. $type=dol_mimetype($filename);
  381. if ($encoding) header('Content-Encoding: '.$encoding);
  382. if ($type) header('Content-Type: '.$type);
  383. if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
  384. else header('Content-Disposition: inline; filename="'.$filename.'"');
  385. // Ajout directives pour resoudre bug IE
  386. header('Cache-Control: Public, must-revalidate');
  387. header('Pragma: public');
  388. readfile($file);
  389. return 1;
  390. }
  391. }
  392. ?>