commonstickergenerator.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  7. * Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /* Inspire de PDF_Label
  23. * PDF_Label - PDF label editing
  24. * @package PDF_Label
  25. * @author Laurent PASSEBECQ <lpasseb@numericable.fr>
  26. * @copyright 2003 Laurent PASSEBECQ
  27. * disponible ici : http://www.fpdf.org/fr/script/script29.php
  28. */
  29. //-------------------------------------------------------------------
  30. // VERSIONS :
  31. // 1.0 : Initial release
  32. // 1.1 : + : Added unit in the constructor
  33. // + : Now Positions start @ (1,1).. then the first image @top-left of a page is (1,1)
  34. // + : Added in the description of a label :
  35. // font-size : defaut char size (can be changed by calling Set_Char_Size(xx);
  36. // paper-size : Size of the paper for this sheet (thanx to Al Canton)
  37. // metric : type of unit used in this description
  38. // You can define your label properties in inches by setting metric to 'in'
  39. // and printing in millimiter by setting unit to 'mm' in constructor.
  40. // Added some labels :
  41. // 5160, 5161, 5162, 5163,5164 : thanx to Al Canton : acanton@adams-blake.com
  42. // 8600 : thanx to Kunal Walia : kunal@u.washington.edu
  43. // + : Added 3mm to the position of labels to avoid errors
  44. ////////////////////////////////////////////////////
  45. /**
  46. * \file htdocs/core/class/commonstickergenerator.class.php
  47. * \ingroup core
  48. * \brief generate pdf document with labels or cards in Avery or custom format
  49. */
  50. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  51. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  52. require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
  53. /**
  54. * Class to generate stick sheet with format Avery or other personalised
  55. */
  56. abstract class CommonStickerGenerator
  57. {
  58. public $code; // Code of format
  59. public $format; // Array with informations
  60. // protected
  61. var $_Avery_Name = ''; // Nom du format de l'etiquette
  62. var $_Margin_Left = 0; // Marge de gauche de l'etiquette
  63. var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette
  64. var $_X_Space = 0; // Espace horizontal entre 2 bandes d'etiquettes
  65. var $_Y_Space = 0; // Espace vertical entre 2 bandes d'etiquettes
  66. var $_X_Number = 0; // NX Nombre d'etiquettes sur la largeur de la page
  67. var $_Y_Number = 0; // NY Nombre d'etiquettes sur la hauteur de la page
  68. var $_Width = 0; // Largeur de chaque etiquette
  69. var $_Height = 0; // Hauteur de chaque etiquette
  70. var $_Char_Size = 10; // Hauteur des caracteres
  71. var $_Line_Height = 10; // Hauteur par defaut d'une ligne
  72. var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values
  73. var $_Metric_Doc = 'mm'; // Type of metric for the doc..
  74. var $_COUNTX = 1;
  75. var $_COUNTY = 1;
  76. var $_First = 1;
  77. var $Tformat;
  78. /**
  79. * Constructor
  80. *
  81. * @param DoliDB $db Database handler
  82. */
  83. function __construct($db)
  84. {
  85. $this->db = $db;
  86. }
  87. /**
  88. * Function to build PDF on disk, then output on HTTP strem.
  89. *
  90. * @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
  91. * @param Translate $outputlangs Lang object for output language
  92. * @param string $srctemplatepath Full path of source filename for generator using a template file
  93. * @param string $outputdir Output directory for pdf file
  94. * @return int 1=OK, 0=KO
  95. */
  96. abstract function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='');
  97. /**
  98. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  99. *
  100. * @param PDF $pdf PDF reference
  101. * @param Translate $outputlangs Output langs
  102. * @param array $param Associative array containing label content and optional parameters
  103. * @return void
  104. */
  105. abstract function addSticker(&$pdf,$outputlangs,$param);
  106. /**
  107. * Methode qui permet de modifier la taille des caracteres
  108. * Cela modiera aussi l'espace entre chaque ligne
  109. *
  110. * @param PDF $pdf PDF reference
  111. * @param int $pt point
  112. * @return void
  113. */
  114. function Set_Char_Size(&$pdf,$pt)
  115. {
  116. if ($pt > 3) {
  117. $this->_Char_Size = $pt;
  118. $this->_Line_Height = $this->_Get_Height_Chars($pt);
  119. $pdf->SetFont('','',$pt);
  120. }
  121. }
  122. /**
  123. * protected Print dot line
  124. *
  125. * @param PDF $pdf PDF reference
  126. * @param int $x1 X1
  127. * @param int $y1 Y1
  128. * @param int $x2 X2
  129. * @param int $y2 Y2
  130. * @param int $epaisseur Epaisseur
  131. * @param int $nbPointilles Nb pointilles
  132. * @return void
  133. */
  134. function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
  135. {
  136. $pdf->SetLineWidth($epaisseur);
  137. $length=abs($x1-$x2);
  138. $hauteur=abs($y1-$y2);
  139. if($length>$hauteur) {
  140. $Pointilles=($length/$nbPointilles)/2; // taille des pointilles
  141. }
  142. else {
  143. $Pointilles=($hauteur/$nbPointilles)/2;
  144. }
  145. for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
  146. for($j=$i;$j<=($i+$Pointilles);$j++) {
  147. if($j<=($x2-1)) {
  148. $pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
  149. $pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
  150. }
  151. }
  152. }
  153. for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
  154. for($j=$i;$j<=($i+$Pointilles);$j++) {
  155. if($j<=($y2-1)) {
  156. $pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
  157. $pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
  158. }
  159. }
  160. }
  161. }
  162. /**
  163. * protected Function realisant une croix aux 4 coins des cartes
  164. *
  165. * @param PDF $pdf PDF reference
  166. * @param int $x1 X1
  167. * @param int $y1 Y1
  168. * @param int $x2 X2
  169. * @param int $y2 Y2
  170. * @param int $epaisseur Epaisseur
  171. * @param int $taille Size
  172. * @return void
  173. */
  174. function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
  175. {
  176. $pdf->SetDrawColor(192,192,192);
  177. $pdf->SetLineWidth($epaisseur);
  178. $lg=$taille/2;
  179. // croix haut gauche
  180. $pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
  181. $pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
  182. // croix bas gauche
  183. $pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
  184. $pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
  185. // croix haut droit
  186. $pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
  187. $pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
  188. // croix bas droit
  189. $pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
  190. $pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
  191. $pdf->SetDrawColor(0,0,0);
  192. }
  193. /**
  194. * protected Convert units (in to mm, mm to in)
  195. * $src and $dest must be 'in' or 'mm'
  196. *
  197. * @param int $value value
  198. * @param string $src from
  199. * @param string $dest to
  200. * @return float value value after conversion
  201. */
  202. function _Convert_Metric ($value, $src, $dest)
  203. {
  204. if ($src != $dest) {
  205. $tab['in'] = 39.37008;
  206. $tab['mm'] = 1000;
  207. return $value * $tab[$dest] / $tab[$src];
  208. } else {
  209. return $value;
  210. }
  211. }
  212. /**
  213. * protected Give the height for a char size given.
  214. *
  215. * @param int $pt Point
  216. * @return int Height chars
  217. */
  218. function _Get_Height_Chars($pt)
  219. {
  220. // Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
  221. $_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);
  222. if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
  223. return $_Table_Hauteur_Chars[$pt];
  224. } else {
  225. return 100; // There is a prob..
  226. }
  227. }
  228. /**
  229. * protected Set format
  230. *
  231. * @param PDF $pdf PDF reference
  232. * @param string $format Format
  233. * @return void
  234. */
  235. function _Set_Format(&$pdf, $format)
  236. {
  237. $this->_Metric = $format['metric'];
  238. $this->_Avery_Name = $format['name'];
  239. $this->_Avery_Code = $format['code'];
  240. $this->_Margin_Left = $this->_Convert_Metric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
  241. $this->_Margin_Top = $this->_Convert_Metric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
  242. $this->_X_Space = $this->_Convert_Metric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
  243. $this->_Y_Space = $this->_Convert_Metric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
  244. $this->_X_Number = $format['NX'];
  245. $this->_Y_Number = $format['NY'];
  246. $this->_Width = $this->_Convert_Metric($format['width'], $this->_Metric, $this->_Metric_Doc);
  247. $this->_Height = $this->_Convert_Metric($format['height'], $this->_Metric, $this->_Metric_Doc);
  248. $this->Set_Char_Size($pdf, $format['font-size']);
  249. }
  250. }