Files
qr_labels/main.go

96 lines
1.9 KiB
Go
Raw Normal View History

2026-04-28 08:26:35 +09:30
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)
}