dolibarr 18.0.6
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
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)) {
114 return -1;
115 }
116
117 $color = array(0, 0, 0);
118
119 $_GET["code"] = $code;
120 $_GET["type"] = $encoding;
121 $_GET["readable"] = $readable;
122
123 if ($code) {
124 // Load the tcpdf barcode class
125 if ($this->is2d) {
126 $height = 3;
127 $width = 3;
128 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
129 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
130 } else {
131 $height = 50;
132 $width = 1;
133 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
134 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
135 }
136
137 dol_syslog("buildBarCode::TCPDF.getBarcodePNG");
138 $barcodeobj->getBarcodePNG($width, $height, $color);
139
140 return 1;
141 } else {
142 return -2;
143 }
144 }
145
156 public function writeBarCode($code, $encoding, $readable = 'Y', $scale = 1, $nooutputiferror = 0)
157 {
158 global $conf, $langs, $_GET;
159
160 dol_mkdir($conf->barcode->dir_temp);
161 if (!is_writable($conf->barcode->dir_temp)) {
162 $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->barcode->dir_temp);
163 dol_syslog('Error in write_file: ' . $this->error, LOG_ERR);
164 return -1;
165 }
166
167 $file = $conf->barcode->dir_temp . '/barcode_' . $code . '_' . $encoding . '.png';
168
169 $tcpdfEncoding = $this->getTcpdfEncodingType($encoding);
170 if (empty($tcpdfEncoding)) {
171 return -1;
172 }
173
174 $color = array(0, 0, 0);
175
176 $_GET["code"] = $code;
177 $_GET["type"] = $encoding;
178 $_GET["readable"] = $readable;
179
180 if ($code) {
181 // Load the tcpdf barcode class
182 if ($this->is2d) {
183 $height = 1;
184 $width = 1;
185 require_once TCPDF_PATH.'tcpdf_barcodes_2d.php';
186 $barcodeobj = new TCPDF2DBarcode($code, $tcpdfEncoding);
187 } else {
188 $height = 50;
189 $width = 1;
190 require_once TCPDF_PATH.'tcpdf_barcodes_1d.php';
191 $barcodeobj = new TCPDFBarcode($code, $tcpdfEncoding);
192 }
193
194 dol_syslog("writeBarCode::TCPDF.getBarcodePngData");
195 if ($imageData = $barcodeobj->getBarcodePngData($width, $height, $color)) {
196 if (function_exists('imagecreate')) {
197 $imageData = imagecreatefromstring($imageData);
198 }
199 if (imagepng($imageData, $file)) {
200 return 1;
201 } else {
202 return -3;
203 }
204 } else {
205 return -4;
206 }
207 } else {
208 return -2;
209 }
210 }
211
218 public function getTcpdfEncodingType($dolEncodingType)
219 {
220 $tcpdf1dEncodingTypes = array(
221 'C39' => 'C39',
222 'C39+' => 'C39+',
223 'C39E' => 'C39E',
224 'C39E+' => 'C39E+',
225 'S25' => 'S25',
226 'S25+' => 'S25+',
227 'I25' => 'I25',
228 'I25+' => 'I25+',
229 'C128' => 'C128',
230 'C128A' => 'C128A',
231 'C128B' => 'C128B',
232 'C128C' => 'C128C',
233 'EAN2' => 'EAN2',
234 'EAN5' => 'EAN5',
235 'EAN8' => 'EAN8',
236 'EAN13' => 'EAN13',
237 'ISBN' => 'EAN13',
238 'UPC' => 'UPCA',
239 'UPCE' => 'UPCE',
240 'MSI' => 'MSI',
241 'MSI+' => 'MSI+',
242 'POSTNET' => 'POSTNET',
243 'PLANET' => 'PLANET',
244 'RMS4CC' => 'RMS4CC',
245 'KIX' => 'KIX',
246 'IMB' => 'IMB',
247 'CODABAR' => 'CODABAR',
248 'CODE11' => 'CODE11',
249 'PHARMA' => 'PHARMA',
250 'PHARMA2T' => 'PHARMA2T'
251 );
252
253 $tcpdf2dEncodingTypes = array(
254 'DATAMATRIX' => 'DATAMATRIX',
255 'PDF417' => 'PDF417',
256 'QRCODE' => 'QRCODE,L',
257 'QRCODE,L' => 'QRCODE,L',
258 'QRCODE,M' => 'QRCODE,M',
259 'QRCODE,Q' => 'QRCODE,Q',
260 'QRCODE,H' => 'QRCODE,H'
261 );
262
263 if (array_key_exists($dolEncodingType, $tcpdf1dEncodingTypes)) {
264 $this->is2d = false;
265 return $tcpdf1dEncodingTypes[$dolEncodingType];
266 } elseif (array_key_exists($dolEncodingType, $tcpdf2dEncodingTypes)) {
267 $this->is2d = true;
268 return $tcpdf2dEncodingTypes[$dolEncodingType];
269 } else {
270 return '';
271 }
272 }
273}
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.
info()
Return description of numbering model.
isEnabled()
Return if a module can be used or not.
canBeActivated()
Checks if the numbers already in the database do not cause conflicts that would prevent this numberin...
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)