This commit is contained in:
2026-04-28 10:52:48 +09:30
parent 04ece93ad7
commit ef2afaa984

95
main.go
View File

@@ -2,22 +2,29 @@ package main
import ( import (
"fmt" "fmt"
"image/color" "log/slog"
"codeberg.org/go-pdf/fpdf" "codeberg.org/go-pdf/fpdf"
"github.com/yeqown/go-qrcode/v2" "github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard" "github.com/yeqown/go-qrcode/writer/standard"
) )
// 1
var cols = 4 var cols = 4
var rows = 5 var rows = 4
var qrSize = 40 var qrSize = 50.0
var fontSize = 20 var fontSize = 16.0 // pts
var fontSizeMM = fontSize / 72 * 25.4
var text = "meow!"
var textTopPadding = 5.0
var qrTopPadding = 5.0
// var probably some padding instead of offsets for qr/font placement
func main() { func main() {
qrc, err := qrcode.New("https://nanocat.net")
qrc, err := qrcode.New("https://nanocat.net/dom/sdfih/dsifj/sidhof/sdifho/sdfiuoh/safiuho")
if err != nil { if err != nil {
fmt.Printf("could not generate QRCode: %v", err) fmt.Printf("could not generate QRCode: %v", err)
return return
@@ -28,27 +35,27 @@ func main() {
// standard.WithQRWidth(uint8(qrSize)), // standard.WithQRWidth(uint8(qrSize)),
standard.WithBorderWidth(0), standard.WithBorderWidth(0),
standard.WithBuiltinImageEncoder(standard.PNG_FORMAT), standard.WithBuiltinImageEncoder(standard.PNG_FORMAT),
standard.WithCircleShape(), // standard.WithCircleShape(),
standard.WithFgGradient(&standard.LinearGradient{ // standard.WithFgGradient(&standard.LinearGradient{
Stops: []standard.ColorStop{{ // Stops: []standard.ColorStop{{
T: 0.0, // T: 0.0,
Color: color.RGBA{ // Color: color.RGBA{
R: 255, // R: 255,
G: 0, // G: 0,
B: 0, // B: 0,
A: 0, // A: 0,
}, // },
}, { // }, {
T: 1.0, // T: 1.0,
Color: color.RGBA{ // Color: color.RGBA{
R: 0, // R: 0,
G: 255, // G: 255,
B: 0, // B: 0,
A: 0, // A: 0,
}, // },
}}, // }},
Angle: 45, // Angle: 45,
}), standard.WithLogoImageFileJPEG("cat-pet-animal-domestic-104827.jpeg"), // }),
) )
// save file // save file
@@ -68,18 +75,35 @@ func main() {
AllowNegativePosition: false, AllowNegativePosition: false,
}, b) }, b)
info := pdf.GetImageInfo("qr") // info := pdf.GetImageInfo("qr")
// panic(info.Height()) // panic(info.Height())
pdf.AddPage() pdf.AddPage()
pdf.SetFont("Arial", "B", float64(fontSize)) pdf.SetFont("Arial", "B", float64(fontSize))
text := "foobar"
textWidth := pdf.GetStringWidth(text) textWidth := pdf.GetStringWidth(text)
colWidth := pageWidth / float64(cols) colWidth := pageWidth / float64(cols)
rowHeight := pageHeight / float64(rows) rowHeight := pageHeight / float64(rows)
// qrRowRatio := qrSize / rowHeight
// qrColRatio := qrSize / colWidth
// fmt.Printf("qr/row height ratio: %.2f\n", qrRowRatio)
// fmt.Printf("qr/col width ratio: %.2f\n", qrColRatio)
if textTopPadding+fontSizeMM+qrTopPadding+qrSize > rowHeight {
slog.Warn("qr is too big for row")
}
if qrSize > colWidth {
slog.Warn("qr is too big for column")
}
if textWidth > colWidth {
slog.Warn("text probably too wide")
}
for rowIdx := range rows { for rowIdx := range rows {
for colIdx := range cols { for colIdx := range cols {
topLeftX := colWidth * float64(colIdx) topLeftX := colWidth * float64(colIdx)
@@ -95,16 +119,15 @@ func main() {
// label at the top // label at the top
pdf.Text( pdf.Text(
topLeftX+colWidth/2-textWidth/2, topLeftX+colWidth/2-textWidth/2,
topLeftY+float64(fontSize), topLeftY+textTopPadding+fontSizeMM,
text, // +fmt.Sprintf("%d/%d", rowIdx, colIdx) text, // +fmt.Sprintf("%d/%d", rowIdx, colIdx)
) )
info.Height() // qr at the bottom
// pdf at the bottom
pdf.ImageOptions("qr", pdf.ImageOptions("qr",
topLeftX+info.Width()-float64(qrSize), //+float64(dim)/2, // centre topLeftX+colWidth/2-qrSize/2, //+float64(dim)/2, // centre
topLeftY+float64(fontSize)*1.3, // centre topLeftY+textTopPadding+fontSizeMM+qrTopPadding, // centre, below text line
float64(qrSize), float64(qrSize), qrSize, qrSize,
false, false,
fpdf.ImageOptions{AllowNegativePosition: true}, 0, "") fpdf.ImageOptions{AllowNegativePosition: true}, 0, "")
// pdf.Image("qr", // pdf.Image("qr",