Add first wip
This commit is contained in:
18
go.mod
Normal file
18
go.mod
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module code.ppl.town/justin/qr_labels
|
||||||
|
|
||||||
|
go 1.25.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
codeberg.org/go-pdf/fpdf v0.11.1
|
||||||
|
github.com/yeqown/go-qrcode/v2 v2.2.5
|
||||||
|
github.com/yeqown/go-qrcode/writer/standard v1.3.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/fogleman/gg v1.3.0 // indirect
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
golang.org/x/image v0.15.0 // indirect
|
||||||
|
)
|
||||||
|
|
||||||
|
require github.com/yeqown/reedsolomon v1.0.0 // indirect
|
||||||
24
go.sum
Normal file
24
go.sum
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
codeberg.org/go-pdf/fpdf v0.11.1 h1:U8+coOTDVLxHIXZgGvkfQEi/q0hYHYvEHFuGNX2GzGs=
|
||||||
|
codeberg.org/go-pdf/fpdf v0.11.1/go.mod h1:Y0DGRAdZ0OmnZPvjbMp/1bYxmIPxm0ws4tfoPOc4LjU=
|
||||||
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8=
|
||||||
|
github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/yeqown/go-qrcode/v2 v2.2.5 h1:HCOe2bSjkhZyYoyyNaXNzh4DJZll6inVJQQw+8228Zk=
|
||||||
|
github.com/yeqown/go-qrcode/v2 v2.2.5/go.mod h1:uHpt9CM0V1HeXLz+Wg5MN50/sI/fQhfkZlOM+cOTHxw=
|
||||||
|
github.com/yeqown/go-qrcode/writer/standard v1.3.0 h1:chdyhEfRtUPgQtuPeaWVGQ/TQx4rE1PqeoW3U+53t34=
|
||||||
|
github.com/yeqown/go-qrcode/writer/standard v1.3.0/go.mod h1:O4MbzsotGCvy8upYPCR91j81dr5XLT7heuljcNXW+oQ=
|
||||||
|
github.com/yeqown/reedsolomon v1.0.0 h1:x1h/Ej/uJnNu8jaX7GLHBWmZKCAWjEJTetkqaabr4B0=
|
||||||
|
github.com/yeqown/reedsolomon v1.0.0/go.mod h1:P76zpcn2TCuL0ul1Fso373qHRc69LKwAw/Iy6g1WiiM=
|
||||||
|
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
|
||||||
|
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
95
main.go
Normal file
95
main.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"codeberg.org/go-pdf/fpdf"
|
||||||
|
"github.com/yeqown/go-qrcode/v2"
|
||||||
|
"github.com/yeqown/go-qrcode/writer/standard"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 1
|
||||||
|
|
||||||
|
var cols = 4
|
||||||
|
var rows = 7
|
||||||
|
var qrSize = 35
|
||||||
|
var offset = qrSize/2 + 5
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
qrc, err := qrcode.New("https://nanocat.net")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("could not generate QRCode: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
b := newBWC()
|
||||||
|
qrBytes := standard.NewWithWriter(&b,
|
||||||
|
standard.WithBuiltinImageEncoder(standard.PNG_FORMAT),
|
||||||
|
standard.WithCircleShape(),
|
||||||
|
standard.WithFgGradient(&standard.LinearGradient{
|
||||||
|
Stops: []standard.ColorStop{{
|
||||||
|
T: 0.0,
|
||||||
|
Color: color.RGBA{
|
||||||
|
R: 255,
|
||||||
|
G: 0,
|
||||||
|
B: 0,
|
||||||
|
A: 0,
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
T: 1.0,
|
||||||
|
Color: color.RGBA{
|
||||||
|
R: 0,
|
||||||
|
G: 255,
|
||||||
|
B: 0,
|
||||||
|
A: 0,
|
||||||
|
},
|
||||||
|
}},
|
||||||
|
Angle: 45,
|
||||||
|
}), standard.WithLogoImageFileJPEG("cat-pet-animal-domestic-104827.jpeg"),
|
||||||
|
)
|
||||||
|
|
||||||
|
// save file
|
||||||
|
if err = qrc.Save(qrBytes); err != nil {
|
||||||
|
fmt.Printf("could not save image: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pdf := fpdf.New("P", "mm", "A4", "")
|
||||||
|
pageWidth, pageHeight := pdf.GetPageSize()
|
||||||
|
|
||||||
|
// register the qr
|
||||||
|
pdf.RegisterImageOptionsReader("qr", fpdf.ImageOptions{
|
||||||
|
ImageType: "png",
|
||||||
|
ReadDpi: false,
|
||||||
|
AllowNegativePosition: false,
|
||||||
|
}, b)
|
||||||
|
|
||||||
|
pdf.AddPage()
|
||||||
|
pdf.SetFont("Arial", "B", 16)
|
||||||
|
|
||||||
|
text := "foobar"
|
||||||
|
textWidth := pdf.GetStringWidth(text)
|
||||||
|
|
||||||
|
colWidth := pageWidth / float64(cols)
|
||||||
|
rowHeight := pageHeight / float64(rows)
|
||||||
|
|
||||||
|
for i := 0; i < rows; i++ {
|
||||||
|
for j := 0; j < cols; j++ {
|
||||||
|
pdf.Text(colWidth*float64(j)+colWidth/2-textWidth/2,
|
||||||
|
rowHeight*float64(i)+rowHeight/2-float64(offset),
|
||||||
|
text)
|
||||||
|
|
||||||
|
pdf.Image("qr",
|
||||||
|
colWidth*float64(j)+colWidth/2-float64(qrSize)/2, rowHeight*float64(i)+rowHeight/2+float64(offset),
|
||||||
|
float64(qrSize), float64(qrSize),
|
||||||
|
false, "tp", 0, "")
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pdf.OutputFileAndClose("hello.pdf")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println(pageWidth, pageHeight)
|
||||||
|
}
|
||||||
17
structs.go
Normal file
17
structs.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "bytes"
|
||||||
|
|
||||||
|
type bufferWriteCloser struct {
|
||||||
|
*bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bwc *bufferWriteCloser) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBWC() bufferWriteCloser {
|
||||||
|
return bufferWriteCloser{
|
||||||
|
Buffer: &bytes.Buffer{},
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user