tcpdfbarcode.modules.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /* Copyright (C) 2005-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2005 Regis Houssin <regis.houssin@inodbox.com>
  4. * Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
  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 3 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 <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/modules/barcode/doc/tcpdfbarcode.modules.php
  21. * \ingroup barcode
  22. * \brief File of class to manage barcode numbering with tcpdf library
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
  26. /**
  27. * Class to generate barcode images using tcpdf barcode generator
  28. */
  29. class modTcpdfbarcode extends ModeleBarCode
  30. {
  31. /**
  32. * Dolibarr version of the loaded document
  33. * @var string
  34. */
  35. public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
  36. /**
  37. * @var string Error code (or message)
  38. */
  39. public $error = '';
  40. public $is2d = false;
  41. /**
  42. * Return description of numbering model
  43. *
  44. * @return string Text with description
  45. */
  46. public function info()
  47. {
  48. global $langs;
  49. return 'TCPDF-barcode';
  50. }
  51. /**
  52. * Return if a module can be used or not
  53. *
  54. * @return boolean true if module can be used
  55. */
  56. public function isEnabled()
  57. {
  58. return true;
  59. }
  60. /**
  61. * Checks if the numbers already in the database do not
  62. * cause conflicts that would prevent this numbering working.
  63. *
  64. * @return boolean false if conflict, true if ok
  65. */
  66. public function canBeActivated()
  67. {
  68. global $langs;
  69. return true;
  70. }
  71. /**
  72. * Return true if encoding is supported
  73. *
  74. * @param string $encoding Encoding norm
  75. * @return int >0 if supported, 0 if not
  76. */
  77. public function encodingIsSupported($encoding)
  78. {
  79. $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
  80. if (empty($tcpdfEncoding)) {
  81. return 0;
  82. } else {
  83. return 1;
  84. }
  85. }
  86. /**
  87. * Return an image file on the fly (no need to write on disk)
  88. *
  89. * @param string $code Value to encode
  90. * @param string $encoding Mode of encoding
  91. * @param string $readable Code can be read (What is this ? is this used ?)
  92. * @param integer $scale Scale (not used with this engine)
  93. * @param integer $nooutputiferror No output if error (not used with this engine)
  94. * @return int <0 if KO, >0 if OK
  95. */
  96. public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
  97. {
  98. global $_GET;
  99. $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
  100. if (empty($tcpdfEncoding)) {
  101. return -1;
  102. }
  103. $color = array(0, 0, 0);
  104. $_GET["code"] = $code;
  105. $_GET["type"] = $encoding;
  106. $_GET["readable"] = $readable;
  107. if ($code) {
  108. // Load the tcpdf barcode class
  109. if ($this->is2d) {
  110. $height = 3;
  111. $width = 3;
  112. require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
  113. $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
  114. } else {
  115. $height = 50;
  116. $width = 1;
  117. require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
  118. $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
  119. }
  120. dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
  121. $barcodeobj->getBarcodePNG($width, $height, $color);
  122. return 1;
  123. } else {
  124. return -2;
  125. }
  126. }
  127. /**
  128. * Save an image file on disk (with no output)
  129. *
  130. * @param string $code Value to encode
  131. * @param string $encoding Mode of encoding
  132. * @param string $readable Code can be read
  133. * @param integer $scale Scale (not used with this engine)
  134. * @param integer $nooutputiferror No output if error (not used with this engine)
  135. * @return int <0 if KO, >0 if OK
  136. */
  137. public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
  138. {
  139. global $conf, $_GET;
  140. dol_mkdir($conf->barcode->dir_temp);
  141. if (!is_writable($conf->barcode->dir_temp)) {
  142. $this->error = "Failed to write in temp directory ".$conf->barcode->dir_temp;
  143. dol_syslog('Error in write_file: '.$this->error, LOG_ERR);
  144. return -1;
  145. }
  146. $file = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
  147. $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
  148. if (empty($tcpdfEncoding)) {
  149. return -1;
  150. }
  151. $color = array(0, 0, 0);
  152. $_GET["code"] = $code;
  153. $_GET["type"] = $encoding;
  154. $_GET["readable"] = $readable;
  155. if ($code) {
  156. // Load the tcpdf barcode class
  157. if ($this->is2d) {
  158. $height = 1;
  159. $width = 1;
  160. require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
  161. $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
  162. } else {
  163. $height = 50;
  164. $width = 1;
  165. require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
  166. $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
  167. }
  168. dol_syslog("writeBarCode::TCPDF.getBarcodePngData");
  169. if ($imageData = $barcodeobj->getBarcodePngData($width, $height, $color)) {
  170. if (function_exists('imagecreate')) {
  171. $imageData = imagecreatefromstring($imageData);
  172. }
  173. if (imagepng($imageData, $file)) {
  174. return 1;
  175. } else {
  176. return -3;
  177. }
  178. } else {
  179. return -4;
  180. }
  181. } else {
  182. return -2;
  183. }
  184. }
  185. /**
  186. * get available output_modes for tcpdf class wth its translated description
  187. *
  188. * @param string $dolEncodingType dolibarr barcode encoding type
  189. * @return string tcpdf encoding type
  190. */
  191. public function getTcpdfEncodingType($dolEncodingType)
  192. {
  193. $tcpdf1dEncodingTypes = array(
  194. 'C39' => 'C39',
  195. 'C39+' => 'C39+',
  196. 'C39E' => 'C39E',
  197. 'C39E+' => 'C39E+',
  198. 'S25' => 'S25',
  199. 'S25+' => 'S25+',
  200. 'I25' => 'I25',
  201. 'I25+' => 'I25+',
  202. 'C128' => 'C128',
  203. 'C128A' => 'C128A',
  204. 'C128B' => 'C128B',
  205. 'C128C' => 'C128C',
  206. 'EAN2' => 'EAN2',
  207. 'EAN5' => 'EAN5',
  208. 'EAN8' => 'EAN8',
  209. 'EAN13' => 'EAN13',
  210. 'ISBN' => 'EAN13',
  211. 'UPC' => 'UPCA',
  212. 'UPCE' => 'UPCE',
  213. 'MSI' => 'MSI',
  214. 'MSI+' => 'MSI+',
  215. 'POSTNET' => 'POSTNET',
  216. 'PLANET' => 'PLANET',
  217. 'RMS4CC' => 'RMS4CC',
  218. 'KIX' => 'KIX',
  219. 'IMB' => 'IMB',
  220. 'CODABAR' => 'CODABAR',
  221. 'CODE11' => 'CODE11',
  222. 'PHARMA' => 'PHARMA',
  223. 'PHARMA2T' => 'PHARMA2T'
  224. );
  225. $tcpdf2dEncodingTypes = array(
  226. 'DATAMATRIX' => 'DATAMATRIX',
  227. 'PDF417' => 'PDF417',
  228. 'QRCODE' => 'QRCODE,L',
  229. 'QRCODE,L' => 'QRCODE,L',
  230. 'QRCODE,M' => 'QRCODE,M',
  231. 'QRCODE,Q' => 'QRCODE,Q',
  232. 'QRCODE,H' => 'QRCODE,H'
  233. );
  234. if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
  235. $this->is2d = false;
  236. return $tcpdf1dEncodingTypes[$dolEncodingType];
  237. } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
  238. $this->is2d = true;
  239. return $tcpdf2dEncodingTypes[$dolEncodingType];
  240. } else {
  241. return '';
  242. }
  243. }
  244. }