commonstickergenerator.class.php 11 KB

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