1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package barcode
19
20 import (
21 "bytes"
22 "errors"
23 "image/jpeg"
24 "io"
25 "strconv"
26 "sync"
27
28 "github.com/boombuler/barcode"
29 "github.com/boombuler/barcode/aztec"
30 "github.com/boombuler/barcode/codabar"
31 "github.com/boombuler/barcode/code128"
32 "github.com/boombuler/barcode/code39"
33 "github.com/boombuler/barcode/datamatrix"
34 "github.com/boombuler/barcode/ean"
35 "github.com/boombuler/barcode/qr"
36 "github.com/boombuler/barcode/twooffive"
37 "github.com/jung-kurt/gofpdf"
38 "github.com/ruudk/golang-pdf417"
39 )
40
41
42
43
44 var barcodes struct {
45 sync.Mutex
46 cache map[string]barcode.Barcode
47 }
48
49
50
51 type barcodePdf interface {
52 GetConversionRatio() float64
53 GetImageInfo(imageStr string) *gofpdf.ImageInfoType
54 Image(imageNameStr string, x, y, w, h float64, flow bool, tp string, link int, linkStr string)
55 RegisterImageReader(imgName, tp string, r io.Reader) *gofpdf.ImageInfoType
56 SetError(err error)
57 }
58
59
60
61 func printBarcode(pdf barcodePdf, code string, x, y float64, w, h *float64, flow bool) {
62 barcodes.Lock()
63 unscaled, ok := barcodes.cache[code]
64 barcodes.Unlock()
65
66 if !ok {
67 err := errors.New("Barcode not found")
68 pdf.SetError(err)
69 return
70 }
71
72 bname := uniqueBarcodeName(code, x, y)
73 info := pdf.GetImageInfo(bname)
74 scaleToWidth := unscaled.Bounds().Dx()
75 scaleToHeight := unscaled.Bounds().Dy()
76
77 if info == nil {
78 bcode, err := barcode.Scale(
79 unscaled,
80 scaleToWidth,
81 scaleToHeight,
82 )
83
84 if err != nil {
85 pdf.SetError(err)
86 return
87 }
88
89 err = registerScaledBarcode(pdf, bname, bcode)
90 if err != nil {
91 pdf.SetError(err)
92 return
93 }
94 }
95
96 scaleToWidthF := float64(scaleToWidth)
97 scaleToHeightF := float64(scaleToHeight)
98
99 if w != nil {
100 scaleToWidthF = *w
101 }
102 if h != nil {
103 scaleToHeightF = *h
104 }
105
106 pdf.Image(bname, x, y, scaleToWidthF, scaleToHeightF, flow, "jpg", 0, "")
107
108 }
109
110
111
112
113
114
115 func BarcodeUnscalable(pdf barcodePdf, code string, x, y float64, w, h *float64, flow bool) {
116 printBarcode(pdf, code, x, y, w, h, flow)
117 }
118
119
120
121
122
123
124
125 func Barcode(pdf barcodePdf, code string, x, y, w, h float64, flow bool) {
126 printBarcode(pdf, code, x, y, &w, &h, flow)
127 }
128
129
130
131 func GetUnscaledBarcodeDimensions(pdf barcodePdf, code string) (w, h float64) {
132 barcodes.Lock()
133 unscaled, ok := barcodes.cache[code]
134 barcodes.Unlock()
135
136 if !ok {
137 err := errors.New("Barcode not found")
138 pdf.SetError(err)
139 return
140 }
141
142 return convertFrom96Dpi(pdf, float64(unscaled.Bounds().Dx())),
143 convertFrom96Dpi(pdf, float64(unscaled.Bounds().Dy()))
144 }
145
146
147
148 func Register(bcode barcode.Barcode) string {
149 barcodes.Lock()
150 if len(barcodes.cache) == 0 {
151 barcodes.cache = make(map[string]barcode.Barcode)
152 }
153
154 key := barcodeKey(bcode)
155 barcodes.cache[key] = bcode
156 barcodes.Unlock()
157
158 return key
159 }
160
161
162
163
164
165 func RegisterAztec(pdf barcodePdf, code string, minECCPercent int, userSpecifiedLayers int) string {
166 bcode, err := aztec.Encode([]byte(code), minECCPercent, userSpecifiedLayers)
167 return registerBarcode(pdf, bcode, err)
168 }
169
170
171
172 func RegisterCodabar(pdf barcodePdf, code string) string {
173 bcode, err := codabar.Encode(code)
174 return registerBarcode(pdf, bcode, err)
175 }
176
177
178
179 func RegisterCode128(pdf barcodePdf, code string) string {
180 bcode, err := code128.Encode(code)
181 return registerBarcode(pdf, bcode, err)
182 }
183
184
185
186
187
188 func RegisterCode39(pdf barcodePdf, code string, includeChecksum, fullASCIIMode bool) string {
189 bcode, err := code39.Encode(code, includeChecksum, fullASCIIMode)
190 return registerBarcode(pdf, bcode, err)
191 }
192
193
194
195
196 func RegisterDataMatrix(pdf barcodePdf, code string) string {
197 bcode, err := datamatrix.Encode(code)
198 return registerBarcode(pdf, bcode, err)
199 }
200
201
202
203
204
205
206
207
208 func RegisterPdf417(pdf barcodePdf, code string, columns int, securityLevel int) string {
209 bcode := pdf417.Encode(code, columns, securityLevel)
210 return registerBarcode(pdf, bcode, nil)
211 }
212
213
214
215
216 func RegisterEAN(pdf barcodePdf, code string) string {
217 bcode, err := ean.Encode(code)
218 return registerBarcode(pdf, bcode, err)
219 }
220
221
222
223
224
225 func RegisterQR(pdf barcodePdf, code string, ecl qr.ErrorCorrectionLevel, mode qr.Encoding) string {
226 bcode, err := qr.Encode(code, ecl, mode)
227 return registerBarcode(pdf, bcode, err)
228 }
229
230
231
232
233
234
235 func RegisterTwoOfFive(pdf barcodePdf, code string, interleaved bool) string {
236 bcode, err := twooffive.Encode(code, interleaved)
237 return registerBarcode(pdf, bcode, err)
238 }
239
240
241
242
243
244 func registerBarcode(pdf barcodePdf, bcode barcode.Barcode, err error) string {
245 if err != nil {
246 pdf.SetError(err)
247 return ""
248 }
249
250 return Register(bcode)
251 }
252
253
254
255
256 func uniqueBarcodeName(code string, x, y float64) string {
257 xStr := strconv.FormatFloat(x, 'E', -1, 64)
258 yStr := strconv.FormatFloat(y, 'E', -1, 64)
259
260 return "barcode-" + code + "-" + xStr + yStr
261 }
262
263
264
265
266 func barcodeKey(bcode barcode.Barcode) string {
267 return bcode.Metadata().CodeKind + bcode.Content()
268 }
269
270
271
272
273 func registerScaledBarcode(pdf barcodePdf, code string, bcode barcode.Barcode) error {
274 buf := new(bytes.Buffer)
275 err := jpeg.Encode(buf, bcode, nil)
276
277 if err != nil {
278 return err
279 }
280
281 reader := bytes.NewReader(buf.Bytes())
282 pdf.RegisterImageReader(code, "jpg", reader)
283
284 return nil
285 }
286
287
288
289
290
291
292
293
294 func convertTo96Dpi(pdf barcodePdf, value float64) float64 {
295 return value * pdf.GetConversionRatio() / 72 * 96
296 }
297
298
299
300 func convertFrom96Dpi(pdf barcodePdf, value float64) float64 {
301 return value / pdf.GetConversionRatio() * 72 / 96
302 }
303
View as plain text