dolibarr 24.0.1
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 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/barcode.lib.php'; // This is to include def like $genbarcode_loc and $font_loc
30
35{
40 public $version = 'dolibarr';
41
45 public $error = '';
46
50 public $is2d = false;
51
58 public function info($langs)
59 {
60 return 'TCPDF-barcode';
61 }
62
68 public function isEnabled()
69 {
70 return true;
71 }
72
80 public function canBeActivated($object)
81 {
82 return true;
83 }
84
91 public function encodingIsSupported($encoding)
92 {
93 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
94 if (empty($tcpdfEncoding)) {
95 return 0;
96 } else {
97 return 1;
98 }
99 }
100
112 public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0, $filebarcode = '')
113 {
114 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
115 if (empty($tcpdfEncoding)) {
116 return -1;
117 }
118
119 $color = array(0, 0, 0);
120
121 $_GET["code"] = $code; // obsolete? nothing seems to read this back
122 $_GET["type"] = $encoding; // obsolete? nothing seems to read this back
123 $_GET["readable"] = $readable; // obsolete? nothing seems to read this back
124
125 if ($code) {
126 // Load the tcpdf barcode class
127 if ($this->is2d) {
128 $height = 3;
129 $width = 3;
130 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
131 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
132 } else {
133 $height = 50;
134 $width = 1;
135 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
136 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
137 }
138
139 if (empty($barcodeobj->getBarcodeArray())) {
140 // Value is not valid for this encoding (for example a wrong checksum digit on a EAN13/UPC/... code).
141 // The tcpdf library would fatal error on imagecreate() if we called getBarcodePNG() with no barcode array.
142 $this->error = 'Value "'.dol_escape_htmltag($code).'" is not valid for encoding "'.dol_escape_htmltag($encoding).'"';
143 dol_syslog("buildBarCode::".$this->error, LOG_WARNING);
144 return -3;
145 }
146
147 dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
148 $barcodeobj->getBarcodePNG($width, $height, $color);
149
150 return 1;
151 } else {
152 return -2;
153 }
154 }
155
166 public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
167 {
168 global $conf, $langs;
169
170 // Force value of temp directory because we may call this even if module barcode is disabled
171 if (empty($conf->barcode)) {
172 $conf->barcode = new stdClass();
173 }
174 if (empty($conf->barcode->dir_temp)) {
175 $conf->barcode->dir_temp = DOL_DATA_ROOT.'/barcode/temp';
176 }
177
178 dol_mkdir($conf->barcode->dir_temp);
179 if (!is_writable($conf->barcode->dir_temp)) {
180 if ($langs instanceof Translate) {
181 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
182 } else {
183 $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
184 }
185 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
186 return -1;
187 }
188
189 $newcode = $code;
190 if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
191 $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
192 }
193
194 $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
195
196 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
197 if (empty($tcpdfEncoding)) {
198 return -1;
199 }
200
201 $color = array(0, 0, 0);
202
203 $_GET["code"] = $code; // obsolete? nothing seems to read this back
204 $_GET["type"] = $encoding; // obsolete? nothing seems to read this back
205 $_GET["readable"] = $readable; // obsolete? nothing seems to read this back
206
207 if ($code) {
208 // Load the tcpdf barcode class
209 if ($this->is2d) {
210 $height = 1;
211 $width = 1;
212 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
213 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
214 } else {
215 $height = 50;
216 $width = 1;
217 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
218 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
219 }
220
221 if (empty($barcodeobj->getBarcodeArray())) {
222 // Value is not valid for this encoding (for example a wrong checksum digit on a EAN13/UPC/... code).
223 // The tcpdf library would fatal error on imagecreate() if we called getBarcodePngData() with no barcode array.
224 $this->error = 'Value "'.dol_escape_htmltag($code).'" is not valid for encoding "'.dol_escape_htmltag($encoding).'"';
225 dol_syslog("writeBarCode::".$this->error, LOG_WARNING);
226 return -5;
227 }
228
229 dol_syslog("writeBarCode::TCPDF.getBarcodePngData file=".$filebarcode);
230
231 $imageData = (string) $barcodeobj->getBarcodePngData($width, $height, $color);
232 // Note: We can avoid to generate an image and add it into PDF by using vectorial instructions directly into PDF with TCPDF->write2DBarcode()
233
234 if ($imageData) {
235 if (function_exists('imagecreate')) {
236 $imageData = imagecreatefromstring($imageData);
237 }
238 if (imagepng($imageData, $filebarcode)) {
239 return 1;
240 } else {
241 return -3;
242 }
243 } else {
244 return -4;
245 }
246 } else {
247 return -2;
248 }
249 }
250
257 public function getTcpdfEncodingType($dolEncodingType)
258 {
259 $tcpdf1dEncodingTypes = array(
260 'C39' => 'C39',
261 'C39+' => 'C39+',
262 'C39E' => 'C39E',
263 'C39E+' => 'C39E+',
264 'S25' => 'S25',
265 'S25+' => 'S25+',
266 'I25' => 'I25',
267 'I25+' => 'I25+',
268 'C128' => 'C128',
269 'C128A' => 'C128A',
270 'C128B' => 'C128B',
271 'C128C' => 'C128C',
272 'EAN2' => 'EAN2',
273 'EAN5' => 'EAN5',
274 'EAN8' => 'EAN8',
275 'EAN13' => 'EAN13',
276 'ISBN' => 'EAN13',
277 'UPC' => 'UPCA',
278 'UPCE' => 'UPCE',
279 'MSI' => 'MSI',
280 'MSI+' => 'MSI+',
281 'POSTNET' => 'POSTNET',
282 'PLANET' => 'PLANET',
283 'RMS4CC' => 'RMS4CC',
284 'KIX' => 'KIX',
285 'IMB' => 'IMB',
286 'CODABAR' => 'CODABAR',
287 'CODE11' => 'CODE11',
288 'PHARMA' => 'PHARMA',
289 'PHARMA2T' => 'PHARMA2T'
290 );
291
292 $tcpdf2dEncodingTypes = array(
293 'DATAMATRIX' => 'DATAMATRIX',
294 'PDF417' => 'PDF417',
295 'QRCODE' => 'QRCODE,L',
296 'QRCODE,L' => 'QRCODE,L',
297 'QRCODE,M' => 'QRCODE,M',
298 'QRCODE,Q' => 'QRCODE,Q',
299 'QRCODE,H' => 'QRCODE,H'
300 );
301
302 if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
303 $this->is2d = false;
304 return $tcpdf1dEncodingTypes[$dolEncodingType];
305 } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
306 $this->is2d = true;
307 return $tcpdf2dEncodingTypes[$dolEncodingType];
308 } else {
309 return '';
310 }
311 }
312}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
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)
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.