summaryrefslogtreecommitdiff
path: root/contrib/barcode/barcode_test.go
diff options
context:
space:
mode:
authorJelmer Snoeck <jelmer.snoeck@gmail.com>2015-08-29 14:41:39 +0100
committerJelmer Snoeck <jelmer.snoeck@gmail.com>2015-08-31 16:55:08 +0100
commitbb0b7372a6e097a5a0efcbb7ca6cf6177813989e (patch)
treece9b340f7c8d3ae63d96021d0d5631d3493c067d /contrib/barcode/barcode_test.go
parent706d7cc75e1f533b761a62d22cb2fc06d09c2de9 (diff)
Add Barcode contribution package.
This package adds the ability to easily add a barcode to the PDF. This barcode is generated with the github.com/boombuler/barcode package. It adds wrappers for all the available barcode types through the `Register()` and `Register<Type>()` methods. These register methods put the barcode on the PDF, but not on the page. They will return a unique key that should be used later on with the `Barcode()` method scale the barcode and put it on the page.
Diffstat (limited to 'contrib/barcode/barcode_test.go')
-rw-r--r--contrib/barcode/barcode_test.go126
1 files changed, 126 insertions, 0 deletions
diff --git a/contrib/barcode/barcode_test.go b/contrib/barcode/barcode_test.go
new file mode 100644
index 0000000..c12ff12
--- /dev/null
+++ b/contrib/barcode/barcode_test.go
@@ -0,0 +1,126 @@
+package barcode_test
+
+import (
+ "github.com/boombuler/barcode/code128"
+ "github.com/boombuler/barcode/qr"
+ "github.com/jung-kurt/gofpdf"
+ "github.com/jung-kurt/gofpdf/contrib/barcode"
+ "github.com/jung-kurt/gofpdf/internal/example"
+)
+
+func createPdf() (pdf *gofpdf.Fpdf) {
+ pdf = gofpdf.New("L", "mm", "A4", "")
+ pdf.SetFont("Helvetica", "", 12)
+ pdf.SetFillColor(200, 200, 220)
+ pdf.AddPage()
+ return
+}
+
+func ExampleRegister() {
+ pdf := createPdf()
+
+ fileStr := example.Filename("contrib_barcode_Register")
+
+ bcode, err := code128.Encode("gofpdf")
+
+ if err == nil {
+ key := barcode.Register(bcode)
+ barcode.Barcode(pdf, key, 15, 15, 100, 10, false)
+ }
+
+ err = pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_Register.pdf
+}
+
+func ExampleRegisterCodabar() {
+ pdf := createPdf()
+
+ key := barcode.RegisterCode128(pdf, "codabar")
+ barcode.Barcode(pdf, key, 15, 15, 100, 10, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterCodabar")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterCodabar.pdf
+}
+
+func ExampleRegisterCode128() {
+ pdf := createPdf()
+
+ key := barcode.RegisterCode128(pdf, "code128")
+ barcode.Barcode(pdf, key, 15, 15, 100, 10, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterCode128")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterCode128.pdf
+}
+
+func ExampleRegisterCode39() {
+ pdf := createPdf()
+
+ key := barcode.RegisterCode39(pdf, "CODE39", false, true)
+ barcode.Barcode(pdf, key, 15, 15, 100, 10, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterCode39")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterCode39.pdf
+}
+
+func ExampleRegisterDataMatrix() {
+ pdf := createPdf()
+
+ key := barcode.RegisterDataMatrix(pdf, "datamatrix")
+ barcode.Barcode(pdf, key, 15, 15, 20, 20, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterDataMatrix")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterDataMatrix.pdf
+}
+
+func ExampleRegisterEAN() {
+ pdf := createPdf()
+
+ key := barcode.RegisterEAN(pdf, "96385074")
+ barcode.Barcode(pdf, key, 15, 15, 100, 10, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterEAN")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterEAN.pdf
+}
+
+func ExampleRegisterQR() {
+ pdf := createPdf()
+
+ key := barcode.RegisterQR(pdf, "qrcode", qr.H, qr.Unicode)
+ barcode.Barcode(pdf, key, 15, 15, 20, 20, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterQR")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterQR.pdf
+}
+
+func ExampleRegisterTwoOfFive() {
+ pdf := createPdf()
+
+ key := barcode.RegisterTwoOfFive(pdf, "1234567895", true)
+ barcode.Barcode(pdf, key, 15, 15, 100, 20, false)
+
+ fileStr := example.Filename("contrib_barcode_RegisterTwoOfFive")
+ err := pdf.OutputFileAndClose(fileStr)
+ example.Summary(err, fileStr)
+ // Output:
+ // Successfully generated ../../pdf/contrib_barcode_RegisterTwoOfFive.pdf
+}