Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ZPL Conversion Function #1871

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@
"HTML To Text",
"Generate Lorem Ipsum",
"Numberwang",
"XKCD Random Number"
"XKCD Random Number",
"ZPL Converter"
]
},
{
Expand Down
103 changes: 103 additions & 0 deletions src/core/operations/ZPLConvert.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @author william-davis-dev
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Convert Label ZPL Into Usable Formats
*/
class ZPLConvert extends Operation {

/**
* ZPLConvert constructor
*/
constructor() {
super();

this.name = "ZPL Converter";
this.module = "Default";
this.description = [
"Takes a ZPL (Zebra Printer Language) string and renders it into a png.",
"<br><br>",
"Uses the <a href='https://labelary.com/service.html'>Labelary</a> API for full support of all ZPL instructions.",
"<br><br>",
"Use the 'Render Image' operation to see the final label output."
].join("\n");
this.infoURL = "https://en.wikipedia.org/wiki/Zebra_Programming_Language";
this.inputType = "string";
this.outputType = "ArrayBuffer";
this.manualBake = true;
this.args = [
{
name: "Width",
type: "number",
value: 4
},
{
name: "Height",
type: "number",
value: 6
},
{
name: "Label Index",
type: "number",
value: 0
},
{
name: "Label Resolution",
type: "option",
value: [
"6 dpmm (152 dpi)",
"8 dpmm (203 dpi)",
"12 dpmm (300 dpi)",
"24 dpmm (600 dpi)"
],
defaultIndex: 1
},
{
name: "Labelary Endpoint",
type: "text",
value: "https://api.labelary.com"
}
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {ArrayBuffer}
*/
run(input, args) {
const [widthArg, heightArg, index, labelResolutionArg, labelaryApi] = args;
// The first segment of the resolution arg is the numeric indicator of the resolution
const labelResolution = labelResolutionArg.toString().split(" ")[0];

const labelaryUrl = `${labelaryApi}/v1/printers/${labelResolution}dpmm/labels/${widthArg}x${heightArg}/${index}`;

return fetch(labelaryUrl, {
method: "POST",
headers: {"accept": "image/png", "Content-Type": "application/x-www-form-urlencoded"},
body: input,
}).then(response => {
if (!response.ok) {
return response.text()
.then(text => {
throw new OperationError(text);
});
}
return response.blob();
}).then(blob => {
return blob.arrayBuffer();
}).then(data => {
return data;
}).catch(e => {
throw new OperationError(`Error making request to ${labelaryUrl} with message: ${e.toString()}`);
});

}
}

export default ZPLConvert;
Loading