Skip to content

Commit

Permalink
add zplgfa package to convert pictures to zpl
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Oct 22, 2018
1 parent a184f8b commit 1e18745
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 23 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ to test the application you can simply follow these steps:
## Why

Why what? Why I wrote the application? Because I did not want to bother anymore with an unstable proprietary software!
Why I made some choices the way I made them? Mostly there is also a reasonable cause - you want an example? There is a function called ```cdatafy``` which adds ```CDATA```-sections to the XML-string. You may ask why on earth someone whould need such a function. Because many SAP developers don't know anything about XML in general and XML marshalling in specific, so they just concatenate strings and as result they create invalid XMLs. It seems like the favorite word of most SAP consultants is standard, but when it comes to actual W3C, WHATWG, IETF, ISO, … standards, they do not care.
Why I made some choices the way I made them? Mostly there is also a reasonable cause - you want an example?
There is a function called ```cdatafy``` which adds ```CDATA```-sections to the XML-string.
You may ask why on earth someone whould need such a function.
Because many SAP developers don't know anything about XML in general and XML marshalling in specific,
so they just concatenate strings and as result they create invalid XMLs.
It seems like the favorite word of most SAP consultants is standard,
but when it comes to actual W3C, WHATWG, IETF, ISO, … standards, they do not care.

## Is it any good?

[Yes](https://news.ycombinator.com/item?id=3067434)

## License

Expand Down
108 changes: 86 additions & 22 deletions ups.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"simonwaldherr.de/go/golibs/cache"
xfile "simonwaldherr.de/go/golibs/file"
"simonwaldherr.de/go/golibs/regex"
"simonwaldherr.de/go/zplgfa"
"strings"
"time"
)
Expand Down Expand Up @@ -401,6 +402,56 @@ func cdatafy(xml string, ele ...string) string {
return xml
}

func base64decode(s string) string {
data, err := base64.StdEncoding.DecodeString(s)
if err == nil {
return data
}
return ""
}

func isPicture(s string) bool {
fileExtension := strings.ToLower(filepath.Ext(s))
if fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" {
return true
}
return false
}

func convertPictureToZPL(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
log.Printf("Warning: could not open the file: %s\n", err)
return "", err
}

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)
return "", 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 "", err
}

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

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

func PrintMessages() {
var rtype string

Expand Down Expand Up @@ -430,49 +481,62 @@ func PrintMessages() {
fmt.Printf("printing %s labels\n", q.Head.Count)

if len(q.Head.Printer) != 0 {
var base64decoded string
printertype := PrinterType(q.Head.Printer)
zplPrintable := isZPLprintable(q.Head.Label)
labelIsPicture := isPicture(q.Head.Label)
printerUnknown := true
peelOff := false

if !zplPrintable {
base64decoded = base64decode(q.Head.Label)
}

if _, ok := Printer.Devs[q.Head.Printer]; ok {
printerUnknown = false
peelOff = Printer.Devs[q.Head.Printer].Peel
} else {
Warning.Printf("couldn't found printer %#v in printer table.\n", q.Head.Printer)
}

fmt.Printf("printerUnknown: %v, isZPLprintable: %v\n", printerUnknown, isZPLprintable(q.Head.Label))
fmt.Printf("printerUnknown: %v, isZPLprintable: %v\n", printerUnknown, zplPrintable)

if !printerUnknown && isZPLprintable(q.Head.Label) {
if !printerUnknown && (zplPrintable || labelIsPicture || base64decoded != "") {
var tmpl string

tmpl = Ltemplate[q.Head.Label]
if zplPrintable {
tmpl = Ltemplate[q.Head.Label]
} else if base64decoded != "" {
tmpl = base64decoded
}

for fieldName, fieldValue := range q.Data.Map {
if tmpl != "" {
for fieldName, fieldValue := range q.Data.Map {

fieldValue = strings.Replace(fieldValue, "\\", "\\1F", -1)
fieldValue = strings.Replace(fieldValue, "\\", "\\1F", -1)

for key, encoded := range sonderzeichen {
fieldValue = strings.Replace(fieldValue, key, encoded, -1)
}
for key, encoded := range sonderzeichen {
fieldValue = strings.Replace(fieldValue, key, encoded, -1)
}

tmpl = strings.Replace(tmpl, "$"+fieldName+"$", fieldValue, -1)
}
tmpl = strings.Replace(tmpl, "$"+fieldName+"$", fieldValue, -1)
}

tmpl = strings.Replace(tmpl, "$DATE$", time.Now().Format("2006-01-02"), -1)
tmpl = strings.Replace(tmpl, "$TIME$", time.Now().Format("15:04:05"), -1)
tmpl = strings.Replace(tmpl, "$PRINTER$", q.Head.Printer, -1)
tmpl = strings.Replace(tmpl, "^MTT", "^MTD", -1)
tmpl = strings.Replace(tmpl, "$DATE$", time.Now().Format("2006-01-02"), -1)
tmpl = strings.Replace(tmpl, "$TIME$", time.Now().Format("15:04:05"), -1)
tmpl = strings.Replace(tmpl, "$PRINTER$", q.Head.Printer, -1)
tmpl = strings.Replace(tmpl, "^MTT", "^MTD", -1)

if peelOff {
tmpl = strings.Replace(tmpl, "^MMT", "^MMK", -1)
} else {
tmpl = strings.Replace(tmpl, "^MMP", "^MMT", -1)
tmpl = strings.Replace(tmpl, "^MMK", "^MMT", -1)
if peelOff {
tmpl = strings.Replace(tmpl, "^MMT", "^MMK", -1)
} else {
tmpl = strings.Replace(tmpl, "^MMP", "^MMT", -1)
tmpl = strings.Replace(tmpl, "^MMK", "^MMT", -1)
}
tmpl, _ = regex.ReplaceAllString(tmpl, "\\^PR\\d+,\\d+", "^PR12,12")
} else if labelIsPicture {
tmpl, err = convertPictureToZPL(q.Head.Label)
}
tmpl, _ = regex.ReplaceAllString(tmpl, "\\^PR\\d+,\\d+", "^PR12,12")

fmt.Printf("sending label to printer %s: \n%s\n", q.Head.Printer, tmpl)

if q.Head.Count == "1" {
go sendLabelToZebra(Printer.Devs[q.Head.Printer].IP, printertype, tmpl, 3)
Expand Down

0 comments on commit 1e18745

Please sign in to comment.