github.com/phpdave11/gofpdf@v1.4.2/contrib/barcode/barcode_test.go (about) 1 package barcode_test 2 3 import ( 4 "testing" 5 6 "github.com/boombuler/barcode/code128" 7 "github.com/boombuler/barcode/qr" 8 "github.com/phpdave11/gofpdf" 9 "github.com/phpdave11/gofpdf/contrib/barcode" 10 "github.com/phpdave11/gofpdf/internal/example" 11 ) 12 13 func createPdf() (pdf *gofpdf.Fpdf) { 14 pdf = gofpdf.New("L", "mm", "A4", "") 15 pdf.SetFont("Helvetica", "", 12) 16 pdf.SetFillColor(200, 200, 220) 17 pdf.AddPage() 18 return 19 } 20 21 func ExampleRegister() { 22 pdf := createPdf() 23 24 fileStr := example.Filename("contrib_barcode_Register") 25 26 bcode, err := code128.Encode("gofpdf") 27 28 if err == nil { 29 key := barcode.Register(bcode) 30 var width float64 = 100 31 var height float64 = 10.0 32 barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) 33 } 34 35 err = pdf.OutputFileAndClose(fileStr) 36 example.Summary(err, fileStr) 37 // Output: 38 // Successfully generated ../../pdf/contrib_barcode_Register.pdf 39 } 40 41 func ExampleRegisterCodabar() { 42 pdf := createPdf() 43 44 key := barcode.RegisterCode128(pdf, "codabar") 45 var width float64 = 100 46 var height float64 = 10 47 barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) 48 49 fileStr := example.Filename("contrib_barcode_RegisterCodabar") 50 err := pdf.OutputFileAndClose(fileStr) 51 example.Summary(err, fileStr) 52 // Output: 53 // Successfully generated ../../pdf/contrib_barcode_RegisterCodabar.pdf 54 } 55 56 func ExampleRegisterAztec() { 57 pdf := createPdf() 58 59 key := barcode.RegisterAztec(pdf, "aztec", 33, 0) 60 barcode.Barcode(pdf, key, 15, 15, 100, 100, false) 61 62 fileStr := example.Filename("contrib_barcode_RegisterAztec") 63 err := pdf.OutputFileAndClose(fileStr) 64 example.Summary(err, fileStr) 65 // Output: 66 // Successfully generated ../../pdf/contrib_barcode_RegisterAztec.pdf 67 } 68 69 func ExampleRegisterCode128() { 70 pdf := createPdf() 71 72 key := barcode.RegisterCode128(pdf, "code128") 73 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 74 75 fileStr := example.Filename("contrib_barcode_RegisterCode128") 76 err := pdf.OutputFileAndClose(fileStr) 77 example.Summary(err, fileStr) 78 // Output: 79 // Successfully generated ../../pdf/contrib_barcode_RegisterCode128.pdf 80 } 81 82 func ExampleRegisterCode39() { 83 pdf := createPdf() 84 85 key := barcode.RegisterCode39(pdf, "CODE39", false, true) 86 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 87 88 fileStr := example.Filename("contrib_barcode_RegisterCode39") 89 err := pdf.OutputFileAndClose(fileStr) 90 example.Summary(err, fileStr) 91 // Output: 92 // Successfully generated ../../pdf/contrib_barcode_RegisterCode39.pdf 93 } 94 95 func ExampleRegisterDataMatrix() { 96 pdf := createPdf() 97 98 key := barcode.RegisterDataMatrix(pdf, "datamatrix") 99 barcode.Barcode(pdf, key, 15, 15, 20, 20, false) 100 101 fileStr := example.Filename("contrib_barcode_RegisterDataMatrix") 102 err := pdf.OutputFileAndClose(fileStr) 103 example.Summary(err, fileStr) 104 // Output: 105 // Successfully generated ../../pdf/contrib_barcode_RegisterDataMatrix.pdf 106 } 107 108 func ExampleRegisterEAN() { 109 pdf := createPdf() 110 111 key := barcode.RegisterEAN(pdf, "96385074") 112 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 113 114 fileStr := example.Filename("contrib_barcode_RegisterEAN") 115 err := pdf.OutputFileAndClose(fileStr) 116 example.Summary(err, fileStr) 117 // Output: 118 // Successfully generated ../../pdf/contrib_barcode_RegisterEAN.pdf 119 } 120 121 func ExampleRegisterQR() { 122 pdf := createPdf() 123 124 key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode) 125 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 126 127 fileStr := example.Filename("contrib_barcode_RegisterQR") 128 err := pdf.OutputFileAndClose(fileStr) 129 example.Summary(err, fileStr) 130 // Output: 131 // Successfully generated ../../pdf/contrib_barcode_RegisterQR.pdf 132 } 133 134 func ExampleRegisterTwoOfFive() { 135 pdf := createPdf() 136 137 key := barcode.RegisterTwoOfFive(pdf, "1234567895", true) 138 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 139 140 fileStr := example.Filename("contrib_barcode_RegisterTwoOfFive") 141 err := pdf.OutputFileAndClose(fileStr) 142 example.Summary(err, fileStr) 143 // Output: 144 // Successfully generated ../../pdf/contrib_barcode_RegisterTwoOfFive.pdf 145 } 146 147 func ExampleRegisterPdf417() { 148 pdf := createPdf() 149 150 key := barcode.RegisterPdf417(pdf, "1234567895", 10, 5) 151 barcode.Barcode(pdf, key, 15, 15, 100, 10, false) 152 153 fileStr := example.Filename("contrib_barcode_RegisterPdf417") 154 err := pdf.OutputFileAndClose(fileStr) 155 example.Summary(err, fileStr) 156 // Output: 157 // Successfully generated ../../pdf/contrib_barcode_RegisterPdf417.pdf 158 } 159 160 // TestRegisterCode128 ensures that no panic arises when an invalid barcode is registered. 161 func TestRegisterCode128(t *testing.T) { 162 pdf := createPdf() 163 barcode.RegisterCode128(pdf, "Invalid character: é") 164 } 165 166 // TestBarcodeUnscalable shows that the barcode may be scaled or not by providing optional heights and widths. 167 func TestBarcodeUnscalable(t *testing.T) { 168 pdf := createPdf() 169 170 key := barcode.RegisterCode128(pdf, "code128") 171 var width float64 = 100 172 var height float64 = 10 173 barcode.BarcodeUnscalable(pdf, key, 15, 15, &width, &height, false) 174 barcode.BarcodeUnscalable(pdf, key, 15, 35, nil, &height, false) 175 barcode.BarcodeUnscalable(pdf, key, 15, 55, &width, nil, false) 176 barcode.BarcodeUnscalable(pdf, key, 15, 75, nil, nil, false) 177 178 fileStr := example.Filename("contrib_barcode_Barcode") 179 err := pdf.OutputFileAndClose(fileStr) 180 example.Summary(err, fileStr) 181 // Output: 182 // Successfully generated ../../pdf/contrib_barcode_Barcode.pdf 183 } 184 185 // TestGetUnscaledBarcodeDimensions shows that the width and height returned by the function match that of the barcode 186 func TestGetUnscaledBarcodeDimensions(t *testing.T) { 187 pdf := createPdf() 188 189 key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode) 190 barcode.BarcodeUnscalable(pdf, key, 15, 15, nil, nil, false) 191 w, h := barcode.GetUnscaledBarcodeDimensions(pdf, key) 192 193 pdf.SetDrawColor(255, 0, 0) 194 pdf.Line(15, 15, 15+w, 15+h) 195 196 fileStr := example.Filename("contrib_barcode_GetBarcodeDimensions") 197 err := pdf.OutputFileAndClose(fileStr) 198 example.Summary(err, fileStr) 199 // Output: 200 // Successfully generated ../../pdf/contrib_barcode_GetBarcodeDimensions.pdf 201 } 202 203 // TestBarcodeNonIntegerScalingFactors shows that the barcode may be scaled to non-integer sizes 204 func TestBarcodeNonIntegerScalingFactors(t *testing.T) { 205 pdf := gofpdf.New("L", "in", "A4", "") 206 pdf.SetFont("Helvetica", "", 12) 207 pdf.SetFillColor(200, 200, 220) 208 pdf.AddPage() 209 210 key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode) 211 var scale float64 = 1.5 212 barcode.BarcodeUnscalable(pdf, key, 0.5, 0.5, &scale, &scale, false) 213 214 pdf.SetDrawColor(255, 0, 0) 215 pdf.Line(0.5, 0.5, 0.5+scale, 0.5+scale) 216 217 fileStr := example.Filename("contrib_barcode_BarcodeScaling") 218 err := pdf.OutputFileAndClose(fileStr) 219 example.Summary(err, fileStr) 220 // Output: 221 // Successfully generated ../../pdf/contrib_barcode_BarcodeScaling.pdf 222 }