commonstickergenerator.class.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /**
  60. * @var array format Array with informations
  61. */
  62. public $format;
  63. // protected
  64. var $_Avery_Name = ''; // Nom du format de l'etiquette
  65. var $_Margin_Left = 0; // Marge de gauche de l'etiquette
  66. var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette
  67. var $_X_Space = 0; // Espace horizontal entre 2 bandes d'etiquettes
  68. var $_Y_Space = 0; // Espace vertical entre 2 bandes d'etiquettes
  69. var $_X_Number = 0; // NX Nombre d'etiquettes sur la largeur de la page
  70. var $_Y_Number = 0; // NY Nombre d'etiquettes sur la hauteur de la page
  71. var $_Width = 0; // Largeur de chaque etiquette
  72. var $_Height = 0; // Hauteur de chaque etiquette
  73. var $_Char_Size = 10; // Hauteur des caracteres
  74. var $_Line_Height = 10; // Hauteur par defaut d'une ligne
  75. var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values
  76. var $_Metric_Doc = 'mm'; // Type of metric for the doc..
  77. var $_COUNTX = 1;
  78. var $_COUNTY = 1;
  79. var $_First = 1;
  80. var $Tformat;
  81. /**
  82. * Constructor
  83. *
  84. * @param DoliDB $db Database handler
  85. */
  86. function __construct($db)
  87. {
  88. $this->db = $db;
  89. }
  90. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  91. /**
  92. * Function to build PDF on disk, then output on HTTP strem.
  93. *
  94. * @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
  95. * @param Translate $outputlangs Lang object for output language
  96. * @param string $srctemplatepath Full path of source filename for generator using a template file
  97. * @param string $outputdir Output directory for pdf file
  98. * @return int 1=OK, 0=KO
  99. */
  100. abstract function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='');
  101. // phpcs:enable
  102. /**
  103. * Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
  104. *
  105. * @param PDF $pdf PDF reference
  106. * @param Translate $outputlangs Output langs
  107. * @param array $param Associative array containing label content and optional parameters
  108. * @return void
  109. */
  110. abstract function addSticker(&$pdf,$outputlangs,$param);
  111. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  112. /**
  113. * Methode qui permet de modifier la taille des caracteres
  114. * Cela modiera aussi l'espace entre chaque ligne
  115. *
  116. * @param PDF $pdf PDF reference
  117. * @param int $pt point
  118. * @return void
  119. */
  120. function Set_Char_Size(&$pdf,$pt)
  121. {
  122. // phpcs:enable
  123. if ($pt > 3) {
  124. $this->_Char_Size = $pt;
  125. $this->_Line_Height = $this->_Get_Height_Chars($pt);
  126. $pdf->SetFont('','',$pt);
  127. }
  128. }
  129. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  130. /**
  131. * protected Print dot line
  132. *
  133. * @param PDF $pdf PDF reference
  134. * @param int $x1 X1
  135. * @param int $y1 Y1
  136. * @param int $x2 X2
  137. * @param int $y2 Y2
  138. * @param int $epaisseur Epaisseur
  139. * @param int $nbPointilles Nb pointilles
  140. * @return void
  141. */
  142. function _Pointille(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$nbPointilles=15)
  143. {
  144. // phpcs:enable
  145. $pdf->SetLineWidth($epaisseur);
  146. $length=abs($x1-$x2);
  147. $hauteur=abs($y1-$y2);
  148. if($length>$hauteur) {
  149. $Pointilles=($length/$nbPointilles)/2; // taille des pointilles
  150. }
  151. else {
  152. $Pointilles=($hauteur/$nbPointilles)/2;
  153. }
  154. for($i=$x1;$i<=$x2;$i+=$Pointilles+$Pointilles) {
  155. for($j=$i;$j<=($i+$Pointilles);$j++) {
  156. if($j<=($x2-1)) {
  157. $pdf->Line($j,$y1,$j+1,$y1); // on trace le pointill? du haut, point par point
  158. $pdf->Line($j,$y2,$j+1,$y2); // on trace le pointill? du bas, point par point
  159. }
  160. }
  161. }
  162. for($i=$y1;$i<=$y2;$i+=$Pointilles+$Pointilles) {
  163. for($j=$i;$j<=($i+$Pointilles);$j++) {
  164. if($j<=($y2-1)) {
  165. $pdf->Line($x1,$j,$x1,$j+1); // on trace le pointill? du haut, point par point
  166. $pdf->Line($x2,$j,$x2,$j+1); // on trace le pointill? du bas, point par point
  167. }
  168. }
  169. }
  170. }
  171. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  172. /**
  173. * protected Function realisant une croix aux 4 coins des cartes
  174. *
  175. * @param PDF $pdf PDF reference
  176. * @param int $x1 X1
  177. * @param int $y1 Y1
  178. * @param int $x2 X2
  179. * @param int $y2 Y2
  180. * @param int $epaisseur Epaisseur
  181. * @param int $taille Size
  182. * @return void
  183. */
  184. function _Croix(&$pdf,$x1=0,$y1=0,$x2=210,$y2=297,$epaisseur=1,$taille=4)
  185. {
  186. // phpcs:enable
  187. $pdf->SetDrawColor(192,192,192);
  188. $pdf->SetLineWidth($epaisseur);
  189. $lg=$taille/2;
  190. // croix haut gauche
  191. $pdf->Line($x1,$y1-$lg,$x1,$y1+$lg);
  192. $pdf->Line($x1-$lg,$y1,$x1+$lg,$y1);
  193. // croix bas gauche
  194. $pdf->Line($x1,$y2-$lg,$x1,$y2+$lg);
  195. $pdf->Line($x1-$lg,$y2,$x1+$lg,$y2);
  196. // croix haut droit
  197. $pdf->Line($x2,$y1-$lg,$x2,$y1+$lg);
  198. $pdf->Line($x2-$lg,$y1,$x2+$lg,$y1);
  199. // croix bas droit
  200. $pdf->Line($x2,$y2-$lg,$x2,$y2+$lg);
  201. $pdf->Line($x2-$lg,$y2,$x2+$lg,$y2);
  202. $pdf->SetDrawColor(0,0,0);
  203. }
  204. /**
  205. * Convert units (in to mm, mm to in)
  206. * $src and $dest must be 'in' or 'mm'
  207. *
  208. * @param int $value value
  209. * @param string $src from ('in' or 'mm')
  210. * @param string $dest to ('in' or 'mm')
  211. * @return float value value after conversion
  212. */
  213. private function convertMetric($value, $src, $dest)
  214. {
  215. if ($src != $dest) {
  216. $tab = array(
  217. 'in'=>39.37008,
  218. 'mm'=>1000
  219. );
  220. return $value * $tab[$dest] / $tab[$src];
  221. }
  222. return $value;
  223. }
  224. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  225. /**
  226. * protected Give the height for a char size given.
  227. *
  228. * @param int $pt Point
  229. * @return int Height chars
  230. */
  231. function _Get_Height_Chars($pt)
  232. {
  233. // phpcs:enable
  234. // Tableau de concordance entre la hauteur des caracteres et de l'espacement entre les lignes
  235. $_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);
  236. if (in_array($pt, array_keys($_Table_Hauteur_Chars))) {
  237. return $_Table_Hauteur_Chars[$pt];
  238. } else {
  239. return 100; // There is a prob..
  240. }
  241. }
  242. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  243. /**
  244. * protected Set format
  245. *
  246. * @param PDF $pdf PDF reference
  247. * @param string $format Format
  248. * @return void
  249. */
  250. function _Set_Format(&$pdf, $format)
  251. {
  252. // phpcs:enable
  253. $this->_Metric = $format['metric'];
  254. $this->_Avery_Name = $format['name'];
  255. $this->_Avery_Code = $format['code'];
  256. $this->_Margin_Left = $this->convertMetric($format['marginLeft'], $this->_Metric, $this->_Metric_Doc);
  257. $this->_Margin_Top = $this->convertMetric($format['marginTop'], $this->_Metric, $this->_Metric_Doc);
  258. $this->_X_Space = $this->convertMetric($format['SpaceX'], $this->_Metric, $this->_Metric_Doc);
  259. $this->_Y_Space = $this->convertMetric($format['SpaceY'], $this->_Metric, $this->_Metric_Doc);
  260. $this->_X_Number = $format['NX'];
  261. $this->_Y_Number = $format['NY'];
  262. $this->_Width = $this->convertMetric($format['width'], $this->_Metric, $this->_Metric_Doc);
  263. $this->_Height = $this->convertMetric($format['height'], $this->_Metric, $this->_Metric_Doc);
  264. $this->Set_Char_Size($pdf, $format['font-size']);
  265. }
  266. }