Contents

scchn/swift-zpl

**Swift ZPL** provides a declarative interface to write [ZPL](https://developer.zebra.com/products/printers/zpl) faster, easier and safer.

Boxes

ZPL {
    LabelHome(x: 0, y: 0)
    
    let width = 50
    let spacing = 30
    
    for row in 0..<3 {
        for col in 0..<3 {
            let x = width * col + spacing * (col + 1)
            let y = width * row + spacing * (row + 1)
            let lineWidth = col == row ? width / 2 : 1
            
            Field(x: x, y: y) {
                GraphicBox(width: width, height: width, lineWidth: lineWidth)
            }
        }
    }
}

[boxes]

Barcodes

Code 128

ZPL {
    LabelHome(x: 0, y: 0)
    Field(x: 50, y: 50) {
        BarcodeCode128(data: "hello30678", height: 100, interpretation: .bottom)
    }
}

[boxes]

QR code

ZPL {
    LabelHome(x: 0, y: 0)
    Field(x: 50, y: 50) {
        BarcodeQR(data: "hello", magnificationFactor: 10)
    }
}

[boxes]

Text & Image

let image: UIImage = ...
let labelWidth = 4 * 203
let labelHeight = 6 * 203

ZPLImageEncoder.default.isCompressed = true

let zpl = ZPL {
    LabelHome(x: 0, y: 0)
    LabelReversePrint(enabled: true)
    
    let imageWidth = labelWidth / 2
    let imageSize = ZPLGeometryUtils.size(aspectRatio: image.size, fillWidth: imageWidth)
    
    Field(x: 0, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    Field(x: imageWidth, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    Field(x: imageWidth, y: 0) {
        GraphicBox(
            width: imageWidth,
            height: Int(imageSize.height),
            lineWidth: Int(min(imageSize.width, imageSize.height))
        )
    }
}

[image]

let image: UIImage = ...
let labelWidth = 72 * 8
let labelHeight = 72 * 9
let settings = ZPL {
    LabelHome(x: 0, y: 0)
    LabelReversePrint(enabled: true)
    ChangeAlphanumericDefaultFont(font: "A", height: 35)
}
let body = ZPL {
    Field(x: labelWidth / 2, y: 0) {
        GraphicBox(width: labelWidth / 2, height: labelHeight, lineWidth: labelWidth / 2)
    }
    
    let imageSize = ZPLGeometryUtils.size(aspectRatio: image.size, fillWidth: labelWidth)
    
    Field(x: 0, y: 0) {
        GraphicField(image: image, size: imageSize)
    }
    
    Field(x: 0, y: Int(imageSize.height)) {
        FieldBlock(width: labelWidth, lines: 1, justification: .center)
        FieldData(text: "Swift")
        Hyphen.return
    }
}
let zpl = ZPL {
    settings
    body
}

[text&image]

More Commands

This package currently only contains a few commands, if you can't find what you need:

You can create a new command with just a few lines of code:

struct NewCommand: ZPLCommandConvertible {
    var command: String {
        // command...
    }
}

ZPL {
    NewCommand()
}

or create a pull request to add a new command or improve/fix existing ones.

Preview

Labelary Online ZPL Viewer

Package Metadata

Repository: scchn/swift-zpl

Default branch: main

README: README.md