labelstudio-js-client
v0.2.3
Published
Small JavaScript client for the custom LabelStudio service
Maintainers
Readme
LabelStudio JS Client
Small JavaScript client for your custom LabelStudio service.
Install
npm i labelstudio-js-clientQuick Start
import LabelStudio from 'labelstudio-js-client';
await LabelStudio.configure({
baseUrl: 'http://localhost:3966',
preferredPrinters: ['DYMO LabelWriter 450', 'DYMO']
}).init();
const printers = await LabelStudio.getPrinters();
LabelStudio.setPrinter(printers[0]);
LabelStudio.setLabel('dymo.address.30252');
const label = new LabelStudio.Label();
label
.addText('Order #100045', {
left: 60,
top: 70,
width: 500,
height: 80,
fontSize: 40,
textAlign: 'left',
autoFit: false
}).done()
.addText('Property of SecureErase, LLC', {
left: 150,
top: 220,
width: 540,
height: 70
}).align('center').autoFit(true, { width: 540, height: 70 }).done()
.addBarcode('0192-XY-445901').dimensions(780, 190).at(55, 130).done()
.addQrCode('https://example.com/track/INV-100045').size(240).at(40, 20).done();
await LabelStudio.print(label, { copies: 2 });API
LabelStudio.configure(options)LabelStudio.init()LabelStudio.getPrinters()LabelStudio.getLabels()LabelStudio.setPrinter(name)LabelStudio.setLabel(labelId)LabelStudio.print(labelOrDesign, options?)LabelStudio.printBulk(labelsOrDesigns, options?)LabelStudio.renderTemplate(template, data)LabelStudio.convertJsonToJs(payload, options?)new LabelStudio.Label()
new LabelStudio.Lable() is also supported as an alias.
Builder Methods
Element builder methods work for text/barcode/qr/image and return to label with .done():
.at(left, top).size(value).dimensions(width, height).scale(x, y?).color(fill).font(family).weight(fontWeight).align('left' | 'center' | 'right').autoFit(enabled, { width?, height? }).options(object)
Label methods:
addText(text, options?)addBarcode(data, options?)addQrCode(data, options?)addImage(src, options?)addObject(object)toJSON()
JSON to JS Converter
Convert a JSON payload into builder-style JavaScript:
const payload = {
printer: 'DYMO LabelWriter 450',
label: 'dymo.address.30252',
objects: [
{
type: 'textbox',
text: 'Order #100045',
left: 60,
top: 70,
width: 500,
height: 80,
fontSize: 40,
textAlign: 'left',
autoFit: false
},
{ type: 'barcode', barcodeType: 'code128', barcodeData: '0192-XY-445901', left: 55, top: 130 }
]
};
const code = LabelStudio.convertJsonToJs(payload, { includePrintCall: true });
console.log(code);Error Handling
Common failure cases now throw explicit errors:
ServiceUnavailableError: backend service is not reachable.DiscoveryErrorwith codeNO_PRINTERS: no printers discovered.DiscoveryErrorwith codeNO_LABELS: no labels discovered.
