/
barcode.go
50 lines (41 loc) · 1.04 KB
/
barcode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package dymolabelmanager
import (
"github.com/jung-kurt/gofpdf"
"github.com/boombuler/barcode/code128"
"github.com/boombuler/barcode"
"image/jpeg"
"io/ioutil"
"os"
)
func Barcode(data string) (pdf *gofpdf.Fpdf, err error) {
b, err := code128.Encode(data)
if err != nil { return }
b, err = barcode.Scale(b, b.Bounds().Size().X, 10)
if err != nil {
panic(err)
}
out, err := ioutil.TempFile("", "lblmgrpnpbarcode")
if err != nil {
panic(err)
}
var opt jpeg.Options
opt.Quality = 100
err = jpeg.Encode(out, b, &opt)
if err != nil { return }
imgHeight := float64(b.Bounds().Size().Y)
imgWidth := float64(b.Bounds().Size().X / 3)
pdf = gofpdf.NewCustom(&gofpdf.InitType{
UnitStr: "mm",
Size: gofpdf.SizeType{Wd: imgWidth, Ht: imgHeight},
})
pdf.AddPage()
height, width := pdf.GetPageSize()
pdf.Image(out.Name(), 0, 0, height, width, false, "jpg", 0, "")
pdf.SetFillColor(255, 255, 255)
pdf.Rect(0, 7, imgWidth, 5, "F")
pdf.SetFont("Arial", "B", 6)
pdf.Text(0, 9.1, data)
out.Close()
os.Remove(out.Name())
return
}