Initial commit

This commit is contained in:
Chris 2019-07-11 03:36:39 +02:00
commit 6475ea0067
5 changed files with 305 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor/
/composer.lock

17
composer.json Normal file
View File

@ -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/"
}
}
}

21
src/GlabelsException.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace NoccyLabs\Glabels;
class GlabelsException extends \RuntimeException
{
public static function NoDocumentLoaded()
{
return new GlabelsException("No document has been loaded");
}
public static function MergeCommandFailed()
{
return new GlabelsException("Merge command failed");
}
public static function ConvertCommandFailed()
{
return new GlabelsException("Convert command failed");
}
}

161
src/GlabelsLabel.php Normal file
View File

@ -0,0 +1,161 @@
<?php
namespace NoccyLabs\Glabels;
use SimpleXMLElement;
class GlabelsLabel
{
const XMLNS_GLABELS = "http://glabels.org/xmlns/3.0/";
private $filename;
/** @var SimpleXMLElement */
private $root;
public function __construct(?string $filename=null)
{
if ($filename) {
$this->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);
}
}

103
src/LabelMerger.php Normal file
View File

@ -0,0 +1,103 @@
<?php
namespace NoccyLabs\Glabels;
use SimpleXMLElement;
class LabelMerger
{
/** @var GlabelsLabel $label */
private $label;
/** @var array $rows */
private $rows = [];
private $fields = [];
public function __construct(GLabelsLabel $label)
{
$this->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);
*/
}
}