event-tickets-with-ticket-scanner
Last commit date
3rd
1 year ago
css
1 year ago
img
1 year ago
languages
1 year ago
ticket
1 year ago
vendors
1 year ago
SASO_EVENTTICKETS.php
1 year ago
backend.js
1 year ago
changelog.txt
1 year ago
db.php
1 year ago
index.php
1 year ago
init_file.php
1 year ago
order_details.js
1 year ago
readme.txt
1 year ago
saso-eventtickets-validator.js
1 year ago
sasoEventtickets_AdminSettings.php
1 year ago
sasoEventtickets_Authtoken.php
1 year ago
sasoEventtickets_Base.php
1 year ago
sasoEventtickets_Core.php
1 year ago
sasoEventtickets_Frontend.php
1 year ago
sasoEventtickets_Options.php
1 year ago
sasoEventtickets_PDF.php
1 year ago
sasoEventtickets_Ticket.php
1 year ago
sasoEventtickets_TicketBadge.php
1 year ago
sasoEventtickets_TicketDesigner.php
1 year ago
sasoEventtickets_TicketQR.php
1 year ago
ticket_events.js
1 year ago
ticket_scanner.js
1 year ago
validator.js
1 year ago
wc_backend.js
1 year ago
wc_frontend.js
1 year ago
woocommerce-hooks.php
1 year ago
sasoEventtickets_PDF.php
403 lines
| 1 | <?php |
| 2 | use setasign\Fpdi\Tcpdf\Fpdi; |
| 3 | include_once(plugin_dir_path(__FILE__)."init_file.php"); |
| 4 | class sasoEventtickets_PDF { |
| 5 | private $parts = []; |
| 6 | private $filemode; |
| 7 | private $filepath; |
| 8 | private $filename; |
| 9 | private $orientation = "P"; |
| 10 | private $page_format = 'A4'; |
| 11 | private $isRTL = false; |
| 12 | private $languageArray = null; |
| 13 | private $background_image = null; |
| 14 | private $fontSize = 10; |
| 15 | private $fontFamily = "dejavusans"; |
| 16 | |
| 17 | private $is_own_page_format = false; |
| 18 | private $size_width = 210; |
| 19 | private $size_height = 297; |
| 20 | public $marginsZero = false; |
| 21 | |
| 22 | private $attach_pdfs = []; |
| 23 | |
| 24 | private $qr; |
| 25 | private $qr_values; |
| 26 | |
| 27 | public function __construct($parts=[], $filemode="I", $filename="PDF.pdf") { |
| 28 | $this->qr_values = $this->getDefaultQRValues(); |
| 29 | if (is_array($parts)) $this->setParts($parts); |
| 30 | $this->setFilemode($filemode); |
| 31 | $this->setFilename($filename); |
| 32 | $this->_loadLibs(); |
| 33 | } |
| 34 | |
| 35 | public function getPossibleFontFamiles() { |
| 36 | $ret = ["default"=>'dejavusans', "fonts"=>[]]; |
| 37 | if ($handle = opendir(__DIR__.'/vendors/TCPDF/fonts')) { |
| 38 | while (false !== ($entry = readdir($handle))) { |
| 39 | if (pathinfo($entry, PATHINFO_EXTENSION) == "php") { |
| 40 | $ret["fonts"][] = substr($entry, 0, -4); |
| 41 | } |
| 42 | } |
| 43 | closedir($handle); |
| 44 | } |
| 45 | return $ret; |
| 46 | } |
| 47 | |
| 48 | public function setAdditionalPDFsToAttachThem($pdfs) { |
| 49 | if (!is_array($pdfs)) { |
| 50 | $pdfs = [$pdfs]; |
| 51 | } |
| 52 | $this->attach_pdfs = $pdfs; |
| 53 | } |
| 54 | |
| 55 | public function setBackgroundImage($background_image=null) { |
| 56 | $this->background_image = $background_image; |
| 57 | } |
| 58 | |
| 59 | public function setFontSize($number=10) { |
| 60 | $this->fontSize = intval($number); |
| 61 | } |
| 62 | |
| 63 | public function convertPixelIntoMm($pixels, $dpi=96) { |
| 64 | if ($dpi < 1) $dpi = 96; |
| 65 | return $pixels * 25.4 / $dpi; |
| 66 | } |
| 67 | |
| 68 | private function getDefaultQRValues() { |
| 69 | return [ |
| 70 | 'pos'=>['x'=>150, 'y'=>10], |
| 71 | 'size'=>['width'=>50, 'height'=>50], |
| 72 | "type"=>"QRCODE,Q", |
| 73 | 'style'=>[ |
| 74 | 'position'=>'R', |
| 75 | //'align'=>'C', |
| 76 | 'border' => 0, |
| 77 | 'vpadding' => 0,//'auto', |
| 78 | 'hpadding' => 0,//'auto', |
| 79 | 'fgcolor' => array(0,0,0), |
| 80 | //'bgcolor' => false, //array(255,255,255) |
| 81 | 'bgcolor' => array(255,255,255), |
| 82 | 'module_width' => 1, // width of a single module in points |
| 83 | 'module_height' => 1 // height of a single module in points |
| 84 | ], |
| 85 | 'align'=>'C' |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | public function setQRParams($data) { |
| 90 | foreach ($data as $key => $value) { |
| 91 | if (is_array($value)) { |
| 92 | $this->qr_values[$key] = array_merge($this->qr_values[$key], $value); |
| 93 | } else { |
| 94 | $this->qr_values[$key] = $value; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function setFontFamily($fontFamily="dejavusans") { |
| 100 | $this->fontFamily = trim($fontFamily); |
| 101 | } |
| 102 | |
| 103 | public function initQR() { |
| 104 | $this->qr = array_merge(["text"=>""], $this->qr_values); |
| 105 | } |
| 106 | |
| 107 | public function setSize($w, $h) { |
| 108 | $this->is_own_page_format = true; |
| 109 | $this->size_width = intval($w); |
| 110 | $this->size_height = intval($h); |
| 111 | } |
| 112 | public function setRTL($rtl=false) { |
| 113 | $this->isRTL = $rtl; |
| 114 | } |
| 115 | public function isRTL() { |
| 116 | return $this->isRTL; |
| 117 | } |
| 118 | public function setLanguageArray($a) { |
| 119 | $this->languageArray = $a; |
| 120 | } |
| 121 | public function setQRCodeContent($qr) { |
| 122 | if ($this->qr == null) { |
| 123 | $this->initQR(); |
| 124 | } |
| 125 | foreach ($qr as $key => $value) { |
| 126 | if (is_array($this->qr[$key]) && is_array($value)) { |
| 127 | $this->qr[$key] = array_merge($this->qr[$key], $value); |
| 128 | } else { |
| 129 | $this->qr[$key] = $value; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | public function setPageFormat($format) { |
| 134 | $this->page_format = trim($format); |
| 135 | } |
| 136 | public function setOrientation($value){ |
| 137 | // L oder P |
| 138 | $this->orientation = addslashes(trim($value)); |
| 139 | } |
| 140 | public function setFilemode($m) { |
| 141 | $this->filemode = strtoupper($m); |
| 142 | } |
| 143 | |
| 144 | public function getFilemode() { |
| 145 | return $this->filemode; |
| 146 | } |
| 147 | public function setFilepath($path) { |
| 148 | $this->filepath = trim($path); |
| 149 | } |
| 150 | public function setFilename($p) { |
| 151 | $this->filename = trim($p); |
| 152 | } |
| 153 | |
| 154 | public function getFullFilePath() { |
| 155 | return $this->filepath.$this->filename; |
| 156 | } |
| 157 | public function setParts($parts=[]) { |
| 158 | $this->parts = []; |
| 159 | foreach($parts as $part) { |
| 160 | $this->addPart($part); |
| 161 | } |
| 162 | } |
| 163 | public function addPart($part) { |
| 164 | $teile = explode('{PAGEBREAK}', $part); |
| 165 | foreach($teile as $teil) { |
| 166 | $this->parts[] = $teil; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private function getParts() { |
| 171 | return $this->parts; |
| 172 | } |
| 173 | |
| 174 | private function _loadLibs() { |
| 175 | // always load alternative config file for examples |
| 176 | require_once('vendors/TCPDF/config/tcpdf_config.php'); |
| 177 | |
| 178 | // Include the main TCPDF library (search the library on the following directories). |
| 179 | $tcpdf_include_dirs = array( |
| 180 | plugin_dir_path(__FILE__).'vendors/TCPDF/tcpdf.php', |
| 181 | realpath(dirname(__FILE__) . '/vendors/TCPDF/tcpdf.php'),// True source file |
| 182 | realpath('vendors/TCPDF/tcpdf.php'),// Relative from $PWD |
| 183 | '/usr/share/php/tcpdf/tcpdf.php', |
| 184 | '/usr/share/tcpdf/tcpdf.php', |
| 185 | '/usr/share/php-tcpdf/tcpdf.php', |
| 186 | '/var/www/tcpdf/tcpdf.php', |
| 187 | '/var/www/html/tcpdf/tcpdf.php', |
| 188 | '/usr/local/apache2/htdocs/tcpdf/tcpdf.php' |
| 189 | ); |
| 190 | foreach ($tcpdf_include_dirs as $tcpdf_include_path) { |
| 191 | if (@file_exists($tcpdf_include_path)) { |
| 192 | require_once($tcpdf_include_path); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | require_once('vendors/FPDI-2.3.7/src/autoload.php'); |
| 198 | require_once("vendors/fpdf185/fpdf.php"); |
| 199 | } |
| 200 | |
| 201 | private function prepareOutputBuffer() { |
| 202 | if ($this->filemode != "F") ob_clean(); |
| 203 | if ($this->filemode != "F") ob_start(); |
| 204 | } |
| 205 | private function cleanOutputBuffer() { |
| 206 | if ($this->filemode != "F") { |
| 207 | $output_level = ob_get_level(); |
| 208 | for ($a=0;$a<$output_level;$a++) { |
| 209 | ob_end_clean(); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | private function outputPDF($pdf) { |
| 214 | if ($this->filemode == "F") { |
| 215 | $pdf->Output($this->filepath.$this->filename, $this->filemode); |
| 216 | } else { |
| 217 | header_remove(); |
| 218 | $pdf->Output($this->filename, $this->filemode); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private function getFormat() { |
| 223 | $format = $this->page_format; |
| 224 | if ($this->is_own_page_format) { |
| 225 | $format = [$this->size_width, $this->size_height]; |
| 226 | } |
| 227 | return $format; |
| 228 | } |
| 229 | |
| 230 | private function checkFilePath() { |
| 231 | if (empty($this->filepath)) $this->filepath = get_temp_dir(); |
| 232 | } |
| 233 | |
| 234 | private function attachPDFs($pdf, $pdf_filelocations=[]) { |
| 235 | if (count($pdf_filelocations) > 0) { |
| 236 | foreach($pdf_filelocations as $pdf_filelocation) { |
| 237 | // mergen und entsprechend dem filemode senden |
| 238 | $pagenumbers = $pdf->setSourceFile($pdf_filelocation); |
| 239 | for ($a=1;$a<=$pagenumbers;$a++) { |
| 240 | $tplIdx = $pdf->importPage($a); |
| 241 | $pdf->AddPage(); |
| 242 | $pdf->useTemplate($tplIdx,0,0,null,null,true); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | return $pdf; |
| 247 | } |
| 248 | |
| 249 | public function mergeFiles($pdf_filelocations=[]) { |
| 250 | if (count($pdf_filelocations) == 0) throw new Exception("no files to merge"); |
| 251 | $this->prepareOutputBuffer(); |
| 252 | $this->checkFilePath(); |
| 253 | $format = $this->getFormat(); |
| 254 | $pdf = new FPDI($this->orientation, PDF_UNIT, $format, true, 'UTF-8', false, false); |
| 255 | $pdf = $this->attachPDFs($pdf, $pdf_filelocations); |
| 256 | |
| 257 | $this->cleanOutputBuffer(); |
| 258 | $this->outputPDF($pdf); |
| 259 | } |
| 260 | |
| 261 | public function render() { |
| 262 | $this->prepareOutputBuffer(); |
| 263 | $this->checkFilePath(); |
| 264 | $format = $this->getFormat(); |
| 265 | |
| 266 | if ($this->size_width > $this->size_height) { |
| 267 | $this->orientation = "L"; |
| 268 | } |
| 269 | |
| 270 | $pdf = new FPDI($this->orientation, PDF_UNIT, $format, true, 'UTF-8', false, false); |
| 271 | //$pdf->error = function ($msg) {throw new Exception("PDF-Parser: ".$msg);}; |
| 272 | |
| 273 | $preferences = [ |
| 274 | //'HideToolbar' => true, |
| 275 | //'HideMenubar' => true, |
| 276 | //'HideWindowUI' => true, |
| 277 | //'FitWindow' => true, |
| 278 | 'CenterWindow' => true, |
| 279 | //'DisplayDocTitle' => true, |
| 280 | //'NonFullScreenPageMode' => 'UseNone', // UseNone, UseOutlines, UseThumbs, UseOC |
| 281 | //'ViewArea' => 'CropBox', // CropBox, BleedBox, TrimBox, ArtBox |
| 282 | //'ViewClip' => 'CropBox', // CropBox, BleedBox, TrimBox, ArtBox |
| 283 | 'PrintArea' => 'CropBox', // CropBox, BleedBox, TrimBox, ArtBox |
| 284 | //'PrintClip' => 'CropBox', // CropBox, BleedBox, TrimBox, ArtBox |
| 285 | 'PrintScaling' => 'None', // None, AppDefault |
| 286 | 'Duplex' => 'DuplexFlipLongEdge', // Simplex, DuplexFlipShortEdge, DuplexFlipLongEdge |
| 287 | 'PickTrayByPDFSize' => true, |
| 288 | //'PrintPageRange' => array(1,1,2,3), |
| 289 | //'NumCopies' => 2 |
| 290 | ]; |
| 291 | if ($this->orientation == "L") $preferences['Duplex'] = "DuplexFlipShortEdge"; |
| 292 | $pdf->setViewerPreferences($preferences); |
| 293 | $pdf->SetAutoPageBreak(TRUE, 5); |
| 294 | |
| 295 | // set image scale factor |
| 296 | $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); |
| 297 | $pdf->setJPEGQuality(90); |
| 298 | |
| 299 | //$pdf->addFormat("custom", $this->size_width, $this->size_height); |
| 300 | |
| 301 | // set margins |
| 302 | if ($this->marginsZero) { |
| 303 | $pdf->SetMargins(0, 0, 0); |
| 304 | } |
| 305 | //$pdf->SetMargins(PDF_MARGIN_LEFT, 17, 10); |
| 306 | //$pdf->SetHeaderMargin(10); |
| 307 | //$pdf->SetFooterMargin(10); |
| 308 | |
| 309 | $pdf->SetPrintHeader(false); |
| 310 | $pdf->SetPrintFooter(false); |
| 311 | |
| 312 | if ($this->isRTL) { |
| 313 | $pdf->setRTL(true); |
| 314 | } |
| 315 | |
| 316 | if ($this->languageArray != null) { |
| 317 | $pdf->setLanguageArray($this->languageArray); |
| 318 | } |
| 319 | |
| 320 | //$pdf->SetFont('helvetica', '', "10pt"); |
| 321 | //$pdf->SetFont('dejavusans', '', $this->fontSize."pt"); |
| 322 | //$pdf->SetFont('cid0jp', '', $this->fontSize."pt"); // support for japanese |
| 323 | $pdf->SetFont($this->fontFamily, '', $this->fontSize."pt"); // support for japanese |
| 324 | |
| 325 | $page_parts = $this->getParts(); |
| 326 | // Print text using writeHTMLCell() |
| 327 | $pdf->AddPage(); |
| 328 | |
| 329 | // background image |
| 330 | if ($this->background_image != null) { |
| 331 | //$w_image = $this->orientation == "L" ? $this->size_height : $this->size_width; |
| 332 | //$h_image = $this->orientation == "L" ? $this->size_width : $this->size_height; |
| 333 | $w_image = $this->size_width; |
| 334 | $h_image = $this->size_height; |
| 335 | $pdf->SetAutoPageBreak(false, 0); |
| 336 | $bg_pos_x = 0; |
| 337 | $bg_pos_y = 0; |
| 338 | $bg_size_w = $w_image; |
| 339 | $bg_size_h = $h_image; |
| 340 | if (function_exists("getimagesize")){ |
| 341 | $finfo = getimagesize($this->background_image); |
| 342 | //print_r($finfo);exit; |
| 343 | $bg_size_w = $pdf->pixelsToUnits($finfo[0]); |
| 344 | $bg_size_h = $pdf->pixelsToUnits($finfo[1]); |
| 345 | $faktor = 1; |
| 346 | if ($bg_size_w > $w_image) { |
| 347 | $faktor = $bg_size_w / $w_image; |
| 348 | $bg_size_w = $w_image; |
| 349 | $bg_size_h /= $faktor; |
| 350 | } |
| 351 | if ($bg_size_h > $h_image) { |
| 352 | $faktor = $bg_size_h / $h_image; |
| 353 | $bg_size_h = $h_image; |
| 354 | $bg_size_w /= $faktor; |
| 355 | } |
| 356 | $bg_pos_x = ($w_image - $bg_size_w) / 2; |
| 357 | $bg_pos_y = ($h_image - $bg_size_h) / 2; |
| 358 | } |
| 359 | //$pdf->Image($this->background_image, $bg_pos_x, $bg_pos_y, $bg_size_w, $bg_size_h, '', '', '', false, 300, '', false, false, 1, 'CM'); |
| 360 | $pdf->Image($this->background_image, $bg_pos_x, $bg_pos_y, $bg_size_w, $bg_size_h, '', '', '', false, 300, '', false, false, 0); |
| 361 | $pdf->SetAutoPageBreak(TRUE, 5); |
| 362 | $pdf->setPageMark(); |
| 363 | } |
| 364 | |
| 365 | $qr_params = $pdf->serializeTCPDFtagParameters([$this->qr['text'], $this->qr['type'], '', '', $this->qr['size']['width'], $this->qr['size']['height'], $this->qr['style'], $this->qr['align']]); |
| 366 | $qr_code_inline = '<tcpdf method="write2DBarcode" params="'.$qr_params.'" />'; |
| 367 | //$pdf->writeHTML(print_r($this->qr, true)); |
| 368 | |
| 369 | foreach($page_parts as $p) { |
| 370 | |
| 371 | $p = str_replace("{QRCODE_INLINE}", $qr_code_inline, $p); |
| 372 | |
| 373 | try { |
| 374 | if ($p == "{PAGEBREAK}") { |
| 375 | $pdf->AddPage(); |
| 376 | continue; |
| 377 | } |
| 378 | $teile = explode('{PAGEBREAK}', $p); |
| 379 | $counter = 0; |
| 380 | foreach($teile as $teil) { |
| 381 | $counter++; |
| 382 | if ($counter > 1) $pdf->AddPage(); |
| 383 | if ($teil == "{QRCODE}") { |
| 384 | if (!empty($this->qr['text'])) { |
| 385 | $qr = $this->getDefaultQRValues(); |
| 386 | $pdf->write2DBarcode($this->qr['text'], $this->qr['type'], $this->qr['pos']['x'], $this->qr['pos']['y'], $this->qr['size']['width'], $this->qr['size']['height'], $qr['style'], $qr['align']); |
| 387 | } |
| 388 | } else { |
| 389 | $pdf->writeHTML($teil, false, false, true, false, ''); |
| 390 | } |
| 391 | } |
| 392 | } catch(Exception $e) { } |
| 393 | } |
| 394 | |
| 395 | $pdf->lastPage(); |
| 396 | $pdf = $this->attachPDFs($pdf, $this->attach_pdfs); |
| 397 | |
| 398 | $this->cleanOutputBuffer(); |
| 399 | $this->outputPDF($pdf); |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | ?> |