dolibarr 24.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 * 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;
122 $_GET["type"] = $encoding;
123 $_GET["readable"] = $readable;
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 dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
140 $barcodeobj->getBarcodePNG($width, $height, $color);
141
142 return 1;
143 } else {
144 return -2;
145 }
146 }
147
158 public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
159 {
160 global $conf, $langs;
161
162 // Force value of temp directory because we may call this even if module barcode is disabled
163 if (empty($conf->barcode)) {
164 $conf->barcode = new stdClass();
165 }
166 if (empty($conf->barcode->dir_temp)) {
167 $conf->barcode->dir_temp = DOL_DATA_ROOT.'/barcode/temp';
168 }
169
170 dol_mkdir($conf->barcode->dir_temp);
171 if (!is_writable($conf->barcode->dir_temp)) {
172 if ($langs instanceof Translate) {
173 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
174 } else {
175 $this->error = "ErrorFailedToWriteInTempDirectory ".$conf->barcode->dir_temp;
176 }
177 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
178 return -1;
179 }
180
181 $newcode = $code;
182 if (!preg_match('/^\w+$/', $code) || dol_strlen($code) > 32) {
183 $newcode = dol_hash($newcode, 'md5'); // No need for security here, we can use md5
184 }
185
186 $filebarcode = $conf->barcode->dir_temp . '/barcode_' . $newcode . '_' . $encoding . '.png';
187
188 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
189 if (empty($tcpdfEncoding)) {
190 return -1;
191 }
192
193 $color = array(0, 0, 0);
194
195 $_GET["code"] = $code;
196 $_GET["type"] = $encoding;
197 $_GET["readable"] = $readable;
198
199 if ($code) {
200 // Load the tcpdf barcode class
201 if ($this->is2d) {
202 $height = 1;
203 $width = 1;
204 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
205 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
206 } else {
207 $height = 50;
208 $width = 1;
209 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
210 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
211 }
212
213 dol_syslog("writeBarCode::TCPDF.getBarcodePngData file=".$filebarcode);
214
215 $imageData = (string) $barcodeobj->getBarcodePngData($width, $height, $color);
216 // Note: We can avoid to generate an image and add it into PDF by using vectorial instructions directly into PDF with TCPDF->write2DBarcode()
217
218 if ($imageData) {
219 if (function_exists('imagecreate')) {
220 $imageData = imagecreatefromstring($imageData);
221 }
222 if (imagepng($imageData, $filebarcode)) {
223 return 1;
224 } else {
225 return -3;
226 }
227 } else {
228 return -4;
229 }
230 } else {
231 return -2;
232 }
233 }
234
241 public function getTcpdfEncodingType($dolEncodingType)
242 {
243 $tcpdf1dEncodingTypes = array(
244 'C39' => 'C39',
245 'C39+' => 'C39+',
246 'C39E' => 'C39E',
247 'C39E+' => 'C39E+',
248 'S25' => 'S25',
249 'S25+' => 'S25+',
250 'I25' => 'I25',
251 'I25+' => 'I25+',
252 'C128' => 'C128',
253 'C128A' => 'C128A',
254 'C128B' => 'C128B',
255 'C128C' => 'C128C',
256 'EAN2' => 'EAN2',
257 'EAN5' => 'EAN5',
258 'EAN8' => 'EAN8',
259 'EAN13' => 'EAN13',
260 'ISBN' => 'EAN13',
261 'UPC' => 'UPCA',
262 'UPCE' => 'UPCE',
263 'MSI' => 'MSI',
264 'MSI+' => 'MSI+',
265 'POSTNET' => 'POSTNET',
266 'PLANET' => 'PLANET',
267 'RMS4CC' => 'RMS4CC',
268 'KIX' => 'KIX',
269 'IMB' => 'IMB',
270 'CODABAR' => 'CODABAR',
271 'CODE11' => 'CODE11',
272 'PHARMA' => 'PHARMA',
273 'PHARMA2T' => 'PHARMA2T'
274 );
275
276 $tcpdf2dEncodingTypes = array(
277 'DATAMATRIX' => 'DATAMATRIX',
278 'PDF417' => 'PDF417',
279 'QRCODE' => 'QRCODE,L',
280 'QRCODE,L' => 'QRCODE,L',
281 'QRCODE,M' => 'QRCODE,M',
282 'QRCODE,Q' => 'QRCODE,Q',
283 'QRCODE,H' => 'QRCODE,H'
284 );
285
286 if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
287 $this->is2d = false;
288 return $tcpdf1dEncodingTypes[$dolEncodingType];
289 } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
290 $this->is2d = true;
291 return $tcpdf2dEncodingTypes[$dolEncodingType];
292 } else {
293 return '';
294 }
295 }
296}
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)
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.