dolibarr  13.0.2
tcpdfbarcode.modules.php
Go to the documentation of this file.
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 
26 require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
28 
33 {
38  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
39 
43  public $error = '';
44 
45  public $is2d = false;
46 
52  public function info()
53  {
54  global $langs;
55 
56  return 'TCPDF-barcode';
57  }
58 
64  public function isEnabled()
65  {
66  return true;
67  }
68 
75  public function canBeActivated()
76  {
77  global $langs;
78 
79  return true;
80  }
81 
88  public function encodingIsSupported($encoding)
89  {
90  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
91  if (empty($tcpdfEncoding)) {
92  return 0;
93  } else {
94  return 1;
95  }
96  }
97 
108  public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
109  {
110  global $_GET;
111 
112  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
113  if (empty($tcpdfEncoding)) return -1;
114 
115  $color = array(0, 0, 0);
116 
117  $_GET["code"] = $code;
118  $_GET["type"] = $encoding;
119  $_GET["readable"] = $readable;
120 
121  if ($code) {
122  // Load the tcpdf barcode class
123  if ($this->is2d) {
124  $height = 3;
125  $width = 3;
126  require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
127  $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
128  } else {
129  $height = 50;
130  $width = 1;
131  require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
132  $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
133  }
134 
135  dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
136  $barcodeobj->getBarcodePNG($width, $height, $color);
137 
138  return 1;
139  } else {
140  return -2;
141  }
142  }
143 
154  public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
155  {
156  global $conf, $_GET;
157 
158  dol_mkdir($conf->barcode->dir_temp);
159  $file = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
160 
161  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
162  if (empty($tcpdfEncoding)) return -1;
163 
164  $color = array(0, 0, 0);
165 
166  $_GET["code"] = $code;
167  $_GET["type"] = $encoding;
168  $_GET["readable"] = $readable;
169 
170  if ($code) {
171  // Load the tcpdf barcode class
172  if ($this->is2d) {
173  $height = 1;
174  $width = 1;
175  require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
176  $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
177  } else {
178  $height = 50;
179  $width = 1;
180  require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
181  $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
182  }
183 
184  dol_syslog("writeBarCode::TCPDF.getBarcodePngData");
185  if ($imageData = $barcodeobj->getBarcodePngData($width, $height, $color)) {
186  if (function_exists('imagecreate')) {
187  $imageData = imagecreatefromstring($imageData);
188  }
189  if (imagepng($imageData, $file)) {
190  return 1;
191  } else {
192  return -3;
193  }
194  } else {
195  return -4;
196  }
197  } else {
198  return -2;
199  }
200  }
201 
208  public function getTcpdfEncodingType($dolEncodingType)
209  {
210  $tcpdf1dEncodingTypes = array(
211  'C39' => 'C39',
212  'C39+' => 'C39+',
213  'C39E' => 'C39E',
214  'C39E+' => 'C39E+',
215  'S25' => 'S25',
216  'S25+' => 'S25+',
217  'I25' => 'I25',
218  'I25+' => 'I25+',
219  'C128' => 'C128',
220  'C128A' => 'C128A',
221  'C128B' => 'C128B',
222  'C128C' => 'C128C',
223  'EAN2' => 'EAN2',
224  'EAN5' => 'EAN5',
225  'EAN8' => 'EAN8',
226  'EAN13' => 'EAN13',
227  'ISBN' => 'EAN13',
228  'UPC' => 'UPCA',
229  'UPCE' => 'UPCE',
230  'MSI' => 'MSI',
231  'MSI+' => 'MSI+',
232  'POSTNET' => 'POSTNET',
233  'PLANET' => 'PLANET',
234  'RMS4CC' => 'RMS4CC',
235  'KIX' => 'KIX',
236  'IMB' => 'IMB',
237  'CODABAR' => 'CODABAR',
238  'CODE11' => 'CODE11',
239  'PHARMA' => 'PHARMA',
240  'PHARMA2T' => 'PHARMA2T'
241  );
242 
243  $tcpdf2dEncodingTypes = array(
244  'DATAMATRIX' => 'DATAMATRIX',
245  'PDF417' => 'PDF417',
246  'QRCODE' => 'QRCODE,L',
247  'QRCODE,L' => 'QRCODE,L',
248  'QRCODE,M' => 'QRCODE,M',
249  'QRCODE,Q' => 'QRCODE,Q',
250  'QRCODE,H' => 'QRCODE,H'
251  );
252 
253  if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
254  $this->is2d = false;
255  return $tcpdf1dEncodingTypes[$dolEncodingType];
256  } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
257  $this->is2d = true;
258  return $tcpdf2dEncodingTypes[$dolEncodingType];
259  } else {
260  return '';
261  }
262  }
263 }
writeBarCode($code, $encoding, $readable= 'Y', $scale=1, $nooutputiferror=0)
Save an image file on disk (with no output)
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
isEnabled()
Return if a module can be used or not.
getTcpdfEncodingType($dolEncodingType)
get available output_modes for tcpdf class wth its translated description
encodingIsSupported($encoding)
Return true if encoding is supported.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
info()
Return description of numbering model.
Class to generate barcode images using tcpdf barcode generator.
Parent class for barcode document models.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)
buildBarCode($code, $encoding, $readable= 'Y', $scale=1, $nooutputiferror=0)
Return an image file on the fly (no need to write on disk)