commonstickergenerator.class.php 11 KB

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