open($filename); } } public function open(string $filename) { $path = sprintf("compress.zlib://%s", realpath($filename)); $xml = file_get_contents($path); $root = @simplexml_load_string($xml, "SimpleXMLElement", LIBXML_BIGLINES); if ($root) { $root->registerXPathNamespace("glabels", self::XMLNS_GLABELS); $this->root = $root; } $this->filename = $filename; } public function getFilename(): ?string { return $this->filename; } public function getMergeFields(): array { if (!$this->root) { throw GlabelsException::NoDocumentLoaded(); } $fields = []; foreach ($this->root->xpath("//glabels:Field") as $field) { $fieldName = (string)$field['name']; if (!in_array($fieldName, $fields)) $fields[] = $fieldName; } return $fields; } public function getTemplateInformation(): array { if (!$this->root) { throw GlabelsException::NoDocumentLoaded(); } $template = $this->root->xpath("//glabels:Glabels-document/glabels:Template"); $template = array_shift($template); $templateBrand = (string)$template['brand']; $templatePart = (string)$template['part']; $templateSize = [ 'width' => (string)$template['width'], 'height' => (string)$template['height'] ]; $templateDescr = (string)$template['description']; $rotate = $this->root->xpath("//glabels:Objects"); $rotate = array_shift($rotate); $rotate = ($rotate['rotate'] == "True"); return [ 'brand' => $templateBrand, 'part' => $templatePart, 'description' => $templateDescr, 'size' => $templateSize, 'rotate' => $rotate ]; } public function getMergeType(): string { if (!$this->root) { throw GlabelsException::NoDocumentLoaded(); } $merge = $this->root->xpath("//glabels:Merge"); $merge = array_shift($merge); return (string)$merge['type']; } public function generatePreview(string $output, array $data=null) { if (!$data) { $fields = $this->getMergeFields(); $data = array_combine($fields, $fields); } $tempfile = sprintf("/tmp/%s.tmp", uniqid("php_glabels_")); $tempout = sprintf("/tmp/%s.pdf", uniqid("php_glabels_")); $fd = fopen($tempfile, "w"); fputcsv($fd, array_keys($data)); fputcsv($fd, array_values($data)); fclose($fd); $mergeCmdline = sprintf( "glabels-3-batch -i %s -o %s %s", escapeshellarg($tempfile), escapeshellarg($tempout), escapeshellarg($this->filename) ); echo "\$ ".$mergeCmdline."\n"; exec($mergeCmdline, $out, $ret); if ($ret != 0) { throw GlabelsException::MergeCommandFailed(); } unlink($tempfile); $template = $this->getTemplateInformation(); $args = [ "-density 300", escapeshellarg($tempout) ]; if ($template['rotate']) { $args[] = "-rotate 270"; } $args[] = escapeshellarg($output); $convertCmdline = sprintf("convert %s", join(" ",$args)); echo "\$ ".$convertCmdline."\n"; exec($convertCmdline, $out, $ret); if ($ret != 0) { throw GlabelsException::ConvertCommandFailed(); } unlink($tempout); } }