Пример #1
1
func MakeBarcode(rw http.ResponseWriter, req *http.Request) {
	var id = req.FormValue("id")
	var bigint *big.Int = big.NewInt(0)
	var code barcode.Barcode
	var uuid cassandra.UUID
	var err error

	if id == "" {
		http.NotFound(rw, req)
		return
	}

	uuid, err = cassandra.ParseUUID(id)
	if err != nil {
		log.Print("Error parsing UUID: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error parsing UUID: " + err.Error()))
		return
	}

	bigint.SetBytes([]byte(uuid))
	id = bigint.String()

	code, err = code128.Encode(id)
	if err != nil {
		log.Print("Error generating barcode: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error generating barcode: " + err.Error()))
		return
	}

	code, err = barcode.Scale(code, code.Bounds().Max.X, 24*code.Bounds().Max.Y)
	if err != nil {
		log.Print("Error scaling barcode: ", err)
		rw.WriteHeader(http.StatusInternalServerError)
		rw.Write([]byte("Error scaling barcode: " + err.Error()))
		return
	}

	rw.Header().Set("Content-Type", "image/png")
	rw.Header().Set("Content-Disposition", "inline; filename="+uuid.String()+".png")
	err = png.Encode(rw, code)
	if err != nil {
		log.Print("Error writing out image: ", err)
		rw.Header().Set("Content-Type", "text/plain; charset=utf8")
		rw.Header().Set("Content-Disposition", "inline")
		rw.Write([]byte("Error writing out image: " + err.Error()))
	}
}
Пример #2
0
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
}
Пример #3
0
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
}
Пример #4
0
// RegisterCode128 registers a barcode of type Code128 to the PDF, but not to
// the page. Use Barcode() with the return value to put the barcode on the page.
func RegisterCode128(pdf barcodePdf, code string) string {
	bcode, err := code128.Encode(code)
	return registerBarcode(pdf, bcode, err)
}
Пример #5
-1
func createCode128(data string, scale image.Point) image.Image {
	bar, err := code128.Encode(data)
	chk(err)
	imgWidth := bar.Bounds().Dx() * scale.X
	imgHeight := bar.Bounds().Dy() * scale.Y
	barScale, err := barcode.Scale(bar, imgWidth, imgHeight)
	chk(err)
	return barScale
}