dolibarr  20.0.0-beta
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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
27 require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
29 
34 {
39  public $version = 'dolibarr'; // 'development', 'experimental', 'dolibarr'
40 
44  public $error = '';
45 
46  public $is2d = false;
47 
54  public function info($langs)
55  {
56  return 'TCPDF-barcode';
57  }
58 
64  public function isEnabled()
65  {
66  return true;
67  }
68 
76  public function canBeActivated($object)
77  {
78  return true;
79  }
80 
87  public function encodingIsSupported($encoding)
88  {
89  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
90  if (empty($tcpdfEncoding)) {
91  return 0;
92  } else {
93  return 1;
94  }
95  }
96 
108  public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0, $filebarcode = '')
109  {
110  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
111  if (empty($tcpdfEncoding)) {
112  return -1;
113  }
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, $langs;
157 
158  // Force value of temp directory because we may call this even if module barcode is disabled
159  if (empty($conf->barcode)) {
160  $conf->barcode = new stdClass();
161  }
162  if (empty($conf->barcode->dir_temp)) {
163  $conf->barcode->dir_temp = DOL_DATA_ROOT.'/barcode/temp';
164  }
165 
166  dol_mkdir($conf->barcode->dir_temp);
167  if (!is_writable($conf->barcode->dir_temp)) {
168  if ($langs instanceof Translate) {
169  $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
170  } else {
171  $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
172  }
173  dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
174  return -1;
175  }
176 
177  $newcode = $code;
178  if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
179  $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
180  }
181 
182  $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
183 
184  $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
185  if (empty($tcpdfEncoding)) {
186  return -1;
187  }
188 
189  $color = array(0, 0, 0);
190 
191  $_GET["code"] = $code;
192  $_GET["type"] = $encoding;
193  $_GET["readable"] = $readable;
194 
195  if ($code) {
196  // Load the tcpdf barcode class
197  if ($this->is2d) {
198  $height = 1;
199  $width = 1;
200  require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
201  $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
202  } else {
203  $height = 50;
204  $width = 1;
205  require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
206  $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
207  }
208 
209  dol_syslog("writeBarCode::TCPDF.getBarcodePngData file=".$filebarcode);
210 
211  $imageData = (string) $barcodeobj->getBarcodePngData($width, $height, $color);
212  // Note: We can avoid to generate an image and add it into PDF by using vectorial instructions directly into PDF with TCPDF->write2DBarcode()
213 
214  if ($imageData) {
215  if (function_exists('imagecreate')) {
216  $imageData = imagecreatefromstring($imageData);
217  }
218  if (imagepng($imageData, $filebarcode)) {
219  return 1;
220  } else {
221  return -3;
222  }
223  } else {
224  return -4;
225  }
226  } else {
227  return -2;
228  }
229  }
230 
237  public function getTcpdfEncodingType($dolEncodingType)
238  {
239  $tcpdf1dEncodingTypes = array(
240  'C39' => 'C39',
241  'C39+' => 'C39+',
242  'C39E' => 'C39E',
243  'C39E+' => 'C39E+',
244  'S25' => 'S25',
245  'S25+' => 'S25+',
246  'I25' => 'I25',
247  'I25+' => 'I25+',
248  'C128' => 'C128',
249  'C128A' => 'C128A',
250  'C128B' => 'C128B',
251  'C128C' => 'C128C',
252  'EAN2' => 'EAN2',
253  'EAN5' => 'EAN5',
254  'EAN8' => 'EAN8',
255  'EAN13' => 'EAN13',
256  'ISBN' => 'EAN13',
257  'UPC' => 'UPCA',
258  'UPCE' => 'UPCE',
259  'MSI' => 'MSI',
260  'MSI+' => 'MSI+',
261  'POSTNET' => 'POSTNET',
262  'PLANET' => 'PLANET',
263  'RMS4CC' => 'RMS4CC',
264  'KIX' => 'KIX',
265  'IMB' => 'IMB',
266  'CODABAR' => 'CODABAR',
267  'CODE11' => 'CODE11',
268  'PHARMA' => 'PHARMA',
269  'PHARMA2T' => 'PHARMA2T'
270  );
271 
272  $tcpdf2dEncodingTypes = array(
273  'DATAMATRIX' => 'DATAMATRIX',
274  'PDF417' => 'PDF417',
275  'QRCODE' => 'QRCODE,L',
276  'QRCODE,L' => 'QRCODE,L',
277  'QRCODE,M' => 'QRCODE,M',
278  'QRCODE,Q' => 'QRCODE,Q',
279  'QRCODE,H' => 'QRCODE,H'
280  );
281 
282  if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
283  $this->is2d = false;
284  return $tcpdf1dEncodingTypes[$dolEncodingType];
285  } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
286  $this->is2d = true;
287  return $tcpdf2dEncodingTypes[$dolEncodingType];
288  } else {
289  return '';
290  }
291  }
292 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Parent class for barcode document generators (image)
Class to manage translations.
Class to generate barcode images using tcpdf barcode generator.
getTcpdfEncodingType($dolEncodingType)
get available output_modes for tcpdf class with its translated description
buildBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0, $filebarcode='')
Return an image file on the fly (no need to write on disk) with the HTTP content-type (generated by T...
encodingIsSupported($encoding)
Return true if encoding is supported.
canBeActivated($object)
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
info($langs)
Return description of numbering model.
isEnabled()
Return if a module can be used or not.
writeBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0)
Save an image file on disk (with no output)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dol_hash($chain, $type='0', $nosalt=0)
Returns a hash (non reversible encryption) of a string.