Example #1
0
 /**
  * Sends this ClientPrintJob object to the client for further processing.
  * The ClientPrintJob object will be processed by the WCPP installed at the client machine.
  * @return string A string representing a ClientPrintJob object.
  */
 public function sendToClient()
 {
     header('Content-type: application/octet-stream');
     $cpjHeader = chr(99) . chr(112) . chr(106) . chr(2);
     $buffer = '';
     if (!Utils::isNullOrEmptyString($this->printerCommands)) {
         if ($this->formatHexValues) {
             $buffer = Utils::formatHexValues($this->printerCommands);
         } else {
             $buffer = $this->printerCommands;
         }
     } else {
         if (isset($this->printFile)) {
             $buffer = $this->printFile->serialize();
         } else {
             if (isset($this->printFileGroup)) {
                 $buffer = 'wcpPFG:';
                 $zip = new ZipArchive();
                 $cacheFileName = (Utils::strEndsWith(WebClientPrint::$wcpCacheFolder, '/') ? WebClientPrint::$wcpCacheFolder : WebClientPrint::$wcpCacheFolder . '/') . 'PFG' . uniqid() . '.zip';
                 $res = $zip->open($cacheFileName, ZipArchive::CREATE);
                 if ($res === TRUE) {
                     foreach ($this->printFileGroup as $printFile) {
                         $zip->addFromString($printFile->fileName, $printFile->getFileContent());
                     }
                     $zip->close();
                     $handle = fopen($cacheFileName, 'rb');
                     $buffer .= fread($handle, filesize($cacheFileName));
                     fclose($handle);
                     unlink($cacheFileName);
                 } else {
                     $buffer = 'Creating PrintFileGroup failed. Cannot create zip file.';
                 }
             }
         }
     }
     $arrIdx1 = Utils::intToArray(strlen($buffer));
     if (!isset($this->clientPrinter)) {
         $this->clientPrinter = new UserSelectedPrinter();
     }
     $buffer .= $this->clientPrinter->serialize();
     $arrIdx2 = Utils::intToArray(strlen($buffer));
     $lo = '';
     if (Utils::isNullOrEmptyString(WebClientPrint::$licenseOwner)) {
         $lo = substr(uniqid(), 0, 8);
     } else {
         $lo = WebClientPrint::$licenseOwner;
     }
     $lk = '';
     if (Utils::isNullOrEmptyString(WebClientPrint::$licenseKey)) {
         $lk = substr(uniqid(), 0, 8);
     } else {
         $lk = WebClientPrint::$licenseKey;
     }
     $buffer .= $lo . chr(124) . $lk;
     return $cpjHeader . $arrIdx1 . $arrIdx2 . $buffer;
 }