From 6475ea0067be55bffaeca8808079cbe07e41646f Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 11 Jul 2019 03:36:39 +0200 Subject: [PATCH] Initial commit --- .gitignore | 3 + composer.json | 17 +++++ src/GlabelsException.php | 21 +++++ src/GlabelsLabel.php | 161 +++++++++++++++++++++++++++++++++++++++ src/LabelMerger.php | 103 +++++++++++++++++++++++++ 5 files changed, 305 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/GlabelsException.php create mode 100644 src/GlabelsLabel.php create mode 100644 src/LabelMerger.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d95451e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/composer.lock + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b5bce6c --- /dev/null +++ b/composer.json @@ -0,0 +1,17 @@ +{ + "name": "noccylabs/glabels", + "description": "Parse information from GLabels files", + "type": "library", + "license": "GPL-3.0-or-later", + "authors": [ + { + "name": "Christopher Vagnetoft", + "email": "cvagnetoft@gmail.com" + } + ], + "autoload": { + "psr-4": { + "NoccyLabs\\Glabels\\": "src/" + } + } +} \ No newline at end of file diff --git a/src/GlabelsException.php b/src/GlabelsException.php new file mode 100644 index 0000000..0ffb507 --- /dev/null +++ b/src/GlabelsException.php @@ -0,0 +1,21 @@ +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); + + } + +} + diff --git a/src/LabelMerger.php b/src/LabelMerger.php new file mode 100644 index 0000000..160afdf --- /dev/null +++ b/src/LabelMerger.php @@ -0,0 +1,103 @@ +label = $label; + try { + $this->fields = $label->getMergeFields(); + } catch (\Exception $e) { + $this->fields = []; + } + } + + public function getSupportedFields() + { + return $this->fields; + } + + public function addRow(array $data) + { + if (count($this->fields) > 0) { + $row = []; + foreach ($this->fields as $field) { + $row[] = array_key_exists($field, $data)?$data[$field]:null; + } + $this->rows[] = $row; + } else { + $this->rows[] = $data; + } + } + + public function mergeToPdf(string $output) + { + $tempfile = sprintf("/tmp/%s.tmp", uniqid("php_glabels_")); + + $fd = fopen($tempfile, "w"); + if (count($this->fields)>0) { + fputcsv($fd, $this->fields); + } else { + fputcsv($fd, array_keys(reset($this->rows))); + } + foreach ($this->rows as $row) { + fputcsv($fd, $row); + } + fclose($fd); + + $filename = $this->label->getFilename(); + $mergeCmdline = sprintf( + "glabels-3-batch -i %s -o %s %s", + escapeshellarg($tempfile), + escapeshellarg($output), + escapeshellarg($filename) + ); + + file_put_contents("/tmp/label.cmd", $mergeCmdline); + + exec($mergeCmdline, $out, $ret); + if ($ret != 0) { + throw new GlabelsException("Merge failed"); + } + + unlink($tempfile); + + /* + $template = $this->label->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); + */ + } + +} +