ZPLGFA Golang Package

May 27, 2026 · View on GitHub

convert pictures to ZPL compatible ^GF-elements with Golang and because of WASM even in the Browser: https://simonwaldherr.github.io/zplgfa/

DOI GoDoc Coverage Status Go Report Card license

The ZPLGFA Golang package implements some functions to convert PNG, JPEG and GIF encoded graphic files to ZPL compatible ^GF-elements (Graphic Fields).

If you need a ready to use application and don't want to hassle around with source code, take a look at the ZPLGFA CLI Tool which is based on this package.

You can also try the package directly in your browser — the WebAssembly build powers a live demo with a converter and a minimal graphical editor (sources in docs/).

features

  • convert image.Image values to complete ZPL labels with ConvertToZPL
  • generate raw ^GF graphic fields with ConvertToGraphicField
  • choose between ASCII, Binary, CompressedASCII and Z64 graphic field encodings
  • decode ZPL ^GF graphic fields back to black and white images with ConvertZPLToImage
  • output black pixel runs as ZPL ^GB line/box commands with ConvertToZPLLines
  • flatten images with alpha transparency against a white background with FlattenImage
  • compress ASCII graphic data with CompressASCII
  • position graphics on the label with ConvertToZPLAt
  • configure origin and reverse-field output with ConvertToZPLWithOptions
  • decode and convert PNG, JPEG and GIF data directly from readers or files with ConvertReaderToZPL and ConvertFileToZPL

install

  1. install Golang
  2. go get simonwaldherr.de/go/zplgfa

example

take a look at the example application
or at this sample code:

package main

import (
    "simonwaldherr.de/go/zplgfa"
    "fmt"
    "image"
    _ "image/gif"
    _ "image/jpeg"
    _ "image/png"
    "log"
    "os"
)

func main() {
    // open file
    file, err := os.Open("label.png")
    if err != nil {
        log.Printf("Warning: could not open the file: %s\n", err)
        return
    }

    defer file.Close()

    // load image head information
    config, format, err := image.DecodeConfig(file)
    if err != nil {
        log.Printf("Warning: image not compatible, format: %s, config: %v, error: %s\n", format, config, err)
    }

    // reset file pointer to the beginning of the file
    file.Seek(0, 0)

    // load and decode image
    img, _, err := image.Decode(file)
    if err != nil {
        log.Printf("Warning: could not decode the file, %s\n", err)
        return
    }

    // flatten image
    flat := zplgfa.FlattenImage(img)

    // convert image to zpl compatible type
    gfimg := zplgfa.ConvertToZPL(flat, zplgfa.CompressedASCII)

    // output zpl with graphic field data to stdout
    fmt.Println(gfimg)
}

package api

Convert an image

flat := zplgfa.FlattenImage(img)
zpl := zplgfa.ConvertToZPL(flat, zplgfa.CompressedASCII)

Use zplgfa.Z64 when you want zlib-compressed, base64 encoded ZPL graphic data:

zpl := zplgfa.ConvertToZPL(flat, zplgfa.Z64)

Convert and position an image

zpl := zplgfa.ConvertToZPLAt(flat, zplgfa.CompressedASCII, 120, 80)

Convert with options

zpl := zplgfa.ConvertToZPLWithOptions(flat, zplgfa.ConvertOptions{
    GraphicType: zplgfa.CompressedASCII,
    X:           120,
    Y:           80,
    Reverse:     true,
})

Convert from a reader or file

ConvertReaderToZPL and ConvertFileToZPL decode PNG, JPEG and GIF input, flatten the image and return a complete ZPL label:

zplFromReader, err := zplgfa.ConvertReaderToZPL(reader, zplgfa.CompressedASCII)
zplFromFile, err := zplgfa.ConvertFileToZPL("label.png", zplgfa.CompressedASCII)

Generate only a graphic field

gf := zplgfa.ConvertToGraphicField(flat, zplgfa.ASCII)

Convert ZPL graphics back to an image

img, err := zplgfa.ConvertZPLToImage(zpl)

Output lines instead of a graphic field

zpl := zplgfa.ConvertToZPLLines(flat)

test and benchmark

Run the full test suite:

go test ./...

Run benchmarks:

go test -bench=. ./...

label server

If you have dozens of label printers in use and need to fill and print label templates, this tool will help you:

SimonWaldherr/ups - GitHub

License

MIT