|
| 1 | +/** |
| 2 | + * @author william-davis-dev |
| 3 | + * @copyright Crown Copyright 2019 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | +import Operation from "../Operation.mjs"; |
| 7 | +import OperationError from "../errors/OperationError.mjs"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Convert Label ZPL Into Usable Formats |
| 11 | + */ |
| 12 | +class ZPLConvert extends Operation { |
| 13 | + |
| 14 | + /** |
| 15 | + * ZPLConvert constructor |
| 16 | + */ |
| 17 | + constructor() { |
| 18 | + super(); |
| 19 | + |
| 20 | + this.name = "ZPL Converter"; |
| 21 | + this.module = "Default"; |
| 22 | + this.description = [ |
| 23 | + "Takes ZPL (Zebra Printer Language)-encoded text data and converts renders it to readable images.", |
| 24 | + "<br><br>", |
| 25 | + "Uses the <a href='https://labelary.com/service.html'>Labelary</a> API for full support of all ZPL instructions.", |
| 26 | + "<br><br>", |
| 27 | + "Use the 'Render Image' operation to see the final label output." |
| 28 | + ].join("\n"); |
| 29 | + this.infoURL = "https://en.wikipedia.org/wiki/Zebra_Programming_Language"; |
| 30 | + this.inputType = "string"; |
| 31 | + this.outputType = "ArrayBuffer"; |
| 32 | + this.manualBake = true; |
| 33 | + this.args = [ |
| 34 | + { |
| 35 | + name: "Width", |
| 36 | + type: "number", |
| 37 | + value: 4 |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Height", |
| 41 | + type: "number", |
| 42 | + value: 6 |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Label Index", |
| 46 | + type: "number", |
| 47 | + value: 0 |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Label Resolution", |
| 51 | + type: "option", |
| 52 | + value: [ |
| 53 | + "6 dpmm (152 dpi)", |
| 54 | + "8 dpmm (203 dpi)", |
| 55 | + "12 dpmm (300 dpi)", |
| 56 | + "24 dpmm (600 dpi)" |
| 57 | + ], |
| 58 | + defaultIndex: 1 |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Labelary Endpoint", |
| 62 | + type: "text", |
| 63 | + value: "https://api.labelary.com" |
| 64 | + } |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param {string} input |
| 70 | + * @param {Object[]} args |
| 71 | + * @returns {ArrayBuffer} |
| 72 | + */ |
| 73 | + run(input, args) { |
| 74 | + const [widthArg, heightArg, index, labelResolutionArg, labelaryApi] = args; |
| 75 | + // The first segment of the resolution arg is the numeric indicator of the resolution |
| 76 | + const labelResolution = labelResolutionArg.toString().split(" ")[0]; |
| 77 | + |
| 78 | + const labelaryUrl = `${labelaryApi}/v1/printers/${labelResolution}dpmm/labels/${widthArg}x${heightArg}/${index}` |
| 79 | + |
| 80 | + return fetch(labelaryUrl, { |
| 81 | + method: 'POST', |
| 82 | + headers: {"accept": "image/png", "Content-Type": "application/x-www-form-urlencoded"}, |
| 83 | + body: input, |
| 84 | + }).then(response => { |
| 85 | + if (!response.ok) { |
| 86 | + return response.text() |
| 87 | + .then(text => { |
| 88 | + throw new OperationError(text); |
| 89 | + }); |
| 90 | + } |
| 91 | + return response.blob(); |
| 92 | + }).then(blob => { |
| 93 | + return blob.arrayBuffer() |
| 94 | + }).then(data => { |
| 95 | + return data; |
| 96 | + }).catch(e => { |
| 97 | + throw new OperationError(`Error making request to ${labelaryUrl} with message: ${e.toString()}`); |
| 98 | + }); |
| 99 | + |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default ZPLConvert; |
0 commit comments