dolibarr 19.0.4
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
26require_once DOL_DOCUMENT_ROOT.'/core/modules/barcode/modules_barcode.class.php';
27require_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
53 public function info($langs)
54 {
55 return 'TCPDF-barcode';
56 }
57
63 public function isEnabled()
64 {
65 return true;
66 }
67
75 public function canBeActivated($object)
76 {
77 return true;
78 }
79
86 public function encodingIsSupported($encoding)
87 {
88 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
89 if (empty($tcpdfEncoding)) {
90 return 0;
91 } else {
92 return 1;
93 }
94 }
95
106 public function buildBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
107 {
108 global $_GET;
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, $_GET;
157
158 dol_mkdir($conf->barcode->dir_temp);
159 if (!is_writable($conf->barcode->dir_temp)) {
160 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
161 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
162 return -1;
163 }
164
165 $file = $conf->barcode->dir_temp . '/barcode_' . $code . '_' . $encoding . '.png';
166
167 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
168 if (empty($tcpdfEncoding)) {
169 return -1;
170 }
171
172 $color = array(0, 0, 0);
173
174 $_GET["code"] = $code;
175 $_GET["type"] = $encoding;
176 $_GET["readable"] = $readable;
177
178 if ($code) {
179 // Load the tcpdf barcode class
180 if ($this->is2d) {
181 $height = 1;
182 $width = 1;
183 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
184 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
185 } else {
186 $height = 50;
187 $width = 1;
188 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
189 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
190 }
191
192 dol_syslog("writeBarCode::TCPDF.getBarcodePngData");
193 if ($imageData = $barcodeobj->getBarcodePngData($width, $height, $color)) {
194 if (function_exists('imagecreate')) {
195 $imageData = imagecreatefromstring($imageData);
196 }
197 if (imagepng($imageData, $file)) {
198 return 1;
199 } else {
200 return -3;
201 }
202 } else {
203 return -4;
204 }
205 } else {
206 return -2;
207 }
208 }
209
216 public function getTcpdfEncodingType($dolEncodingType)
217 {
218 $tcpdf1dEncodingTypes = array(
219 'C39' => 'C39',
220 'C39+' => 'C39+',
221 'C39E' => 'C39E',
222 'C39E+' => 'C39E+',
223 'S25' => 'S25',
224 'S25+' => 'S25+',
225 'I25' => 'I25',
226 'I25+' => 'I25+',
227 'C128' => 'C128',
228 'C128A' => 'C128A',
229 'C128B' => 'C128B',
230 'C128C' => 'C128C',
231 'EAN2' => 'EAN2',
232 'EAN5' => 'EAN5',
233 'EAN8' => 'EAN8',
234 'EAN13' => 'EAN13',
235 'ISBN' => 'EAN13',
236 'UPC' => 'UPCA',
237 'UPCE' => 'UPCE',
238 'MSI' => 'MSI',
239 'MSI+' => 'MSI+',
240 'POSTNET' => 'POSTNET',
241 'PLANET' => 'PLANET',
242 'RMS4CC' => 'RMS4CC',
243 'KIX' => 'KIX',
244 'IMB' => 'IMB',
245 'CODABAR' => 'CODABAR',
246 'CODE11' => 'CODE11',
247 'PHARMA' => 'PHARMA',
248 'PHARMA2T' => 'PHARMA2T'
249 );
250
251 $tcpdf2dEncodingTypes = array(
252 'DATAMATRIX' => 'DATAMATRIX',
253 'PDF417' => 'PDF417',
254 'QRCODE' => 'QRCODE,L',
255 'QRCODE,L' => 'QRCODE,L',
256 'QRCODE,M' => 'QRCODE,M',
257 'QRCODE,Q' => 'QRCODE,Q',
258 'QRCODE,H' => 'QRCODE,H'
259 );
260
261 if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
262 $this->is2d = false;
263 return $tcpdf1dEncodingTypes[$dolEncodingType];
264 } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
265 $this->is2d = true;
266 return $tcpdf2dEncodingTypes[$dolEncodingType];
267 } else {
268 return '';
269 }
270 }
271}
Parent class for barcode document models.
Class to generate barcode images using tcpdf barcode generator.
getTcpdfEncodingType($dolEncodingType)
get available output_modes for tcpdf class wth its translated description
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.
buildBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0)
Return an image file on the fly (no need to write on disk)
writeBarCode($code, $encoding, $readable='Y', $scale=1, $nooutputiferror=0)
Save an image file on disk (with no output)
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)