Files
qr_labels/main.go

169 lines
3.8 KiB
Go
Raw Normal View History

2026-04-28 08:26:35 +09:30
package main
import (
"fmt"
2026-04-28 12:20:14 +09:30
"io"
2026-04-28 10:52:48 +09:30
"log/slog"
2026-04-28 12:20:14 +09:30
"os"
2026-04-28 08:26:35 +09:30
"codeberg.org/go-pdf/fpdf"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
2026-04-28 12:20:14 +09:30
var textTopPadding = 0.0 // typically none is required
var qrTopPadding = 5.0 // below the baseline of the text, remember the descenders like 'y', 'q'
var requiredCellPadding = 3.0 // half on each side, effectively
2026-04-28 10:52:48 +09:30
2026-04-28 12:20:14 +09:30
func main() {
f, _ := os.Create("hello.pdf")
err := generatePage(f, PageOptions{
qrURL: "https://nanocat.net/dom/sdfih/dsifj/sidhof/sdifho/sdfiuoh/safiuho",
qrLabel: "Á[Hjqy]|",
qrSize: 54,
font: "Helvetica",
fontStyle: "B",
fontSize: 34.0,
rows: 4,
cols: 3,
borders: true,
})
if err != nil {
slog.Error("could not generate pdf", "error", err)
os.Exit(1)
}
2026-04-28 11:59:04 +09:30
2026-04-28 12:20:14 +09:30
}
2026-04-28 10:52:48 +09:30
2026-04-28 12:20:14 +09:30
type PageOptions struct {
qrURL string
qrLabel string
qrSize float64
font string
fontStyle string
fontSize float64
rows int
cols int
borders bool
}
2026-04-28 08:26:35 +09:30
2026-04-28 12:20:14 +09:30
func generatePage(w io.Writer, po PageOptions) error {
fontSizeMM := po.fontSize / 72 * 25.4
2026-04-28 10:52:48 +09:30
2026-04-28 12:20:14 +09:30
qrc, err := qrcode.New(po.qrURL)
2026-04-28 10:52:48 +09:30
2026-04-28 08:26:35 +09:30
if err != nil {
2026-04-28 12:20:14 +09:30
return fmt.Errorf("could not generate QRCode: %v", err)
2026-04-28 08:26:35 +09:30
}
b := newBWC()
qrBytes := standard.NewWithWriter(&b,
2026-04-28 09:45:18 +09:30
// standard.WithQRWidth(uint8(qrSize)),
standard.WithBorderWidth(0),
2026-04-28 08:26:35 +09:30
standard.WithBuiltinImageEncoder(standard.PNG_FORMAT),
2026-04-28 10:52:48 +09:30
// 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,
// }),
2026-04-28 08:26:35 +09:30
)
// save file
if err = qrc.Save(qrBytes); err != nil {
fmt.Printf("could not save image: %v", err)
}
2026-04-28 09:45:18 +09:30
// dim := qrc.Dimension()
2026-04-28 08:26:35 +09:30
pdf := fpdf.New("P", "mm", "A4", "")
2026-04-28 09:45:18 +09:30
pdf.SetMargins(0, 0, 0)
2026-04-28 08:26:35 +09:30
pageWidth, pageHeight := pdf.GetPageSize()
// register the qr
pdf.RegisterImageOptionsReader("qr", fpdf.ImageOptions{
ImageType: "png",
ReadDpi: false,
AllowNegativePosition: false,
}, b)
pdf.AddPage()
2026-04-28 12:20:14 +09:30
pdf.SetFont(po.font, po.fontStyle, float64(po.fontSize))
2026-04-28 08:26:35 +09:30
2026-04-28 12:20:14 +09:30
textWidth := pdf.GetStringWidth(po.qrLabel)
2026-04-28 08:26:35 +09:30
2026-04-28 12:20:14 +09:30
colWidth := pageWidth / float64(po.cols)
rowHeight := pageHeight / float64(po.rows)
2026-04-28 08:26:35 +09:30
2026-04-28 12:20:14 +09:30
cellHeight := textTopPadding + fontSizeMM + qrTopPadding + po.qrSize
cellWidth := po.qrSize
2026-04-28 11:59:04 +09:30
if cellHeight > rowHeight-requiredCellPadding {
slog.Warn("qr is too big for row", "cell_height", cellHeight, "row_height", rowHeight)
2026-04-28 12:20:14 +09:30
return fmt.Errorf("qr is too big for row size")
2026-04-28 10:52:48 +09:30
}
2026-04-28 12:20:14 +09:30
// slog.Info("check width", "cell_width", cellWidth, "col_width", colWidth)
2026-04-28 11:59:04 +09:30
if cellWidth > colWidth-requiredCellPadding {
slog.Warn("qr is too big for column", "cell_width", cellWidth, "col_width", colWidth)
2026-04-28 12:20:14 +09:30
return fmt.Errorf("qr is too big for col size")
2026-04-28 10:52:48 +09:30
}
if textWidth > colWidth {
slog.Warn("text probably too wide")
2026-04-28 12:20:14 +09:30
return fmt.Errorf("text is too wide for col size")
2026-04-28 10:52:48 +09:30
}
2026-04-28 12:20:14 +09:30
for rowIdx := range po.rows {
for colIdx := range po.cols {
2026-04-28 09:45:18 +09:30
topLeftX := colWidth * float64(colIdx)
topLeftY := rowHeight * float64(rowIdx)
2026-04-28 11:59:04 +09:30
// draw box
2026-04-28 12:20:14 +09:30
if po.borders {
pdf.SetDashPattern([]float64{1, 1}, 0)
pdf.Rect(topLeftX, topLeftY, colWidth, rowHeight, "D")
pdf.SetDashPattern([]float64{}, 0)
}
2026-04-28 11:59:04 +09:30
2026-04-28 09:45:18 +09:30
// label at the top
pdf.Text(
topLeftX+colWidth/2-textWidth/2,
2026-04-28 11:59:04 +09:30
topLeftY+fontSizeMM+textTopPadding,
2026-04-28 12:20:14 +09:30
po.qrLabel, // +fmt.Sprintf("%d/%d", rowIdx, colIdx)
2026-04-28 09:45:18 +09:30
)
2026-04-28 10:52:48 +09:30
// qr at the bottom
2026-04-28 09:45:18 +09:30
pdf.ImageOptions("qr",
2026-04-28 12:20:14 +09:30
topLeftX+colWidth/2-po.qrSize/2,
2026-04-28 11:01:03 +09:30
topLeftY+textTopPadding+fontSizeMM+qrTopPadding,
2026-04-28 12:20:14 +09:30
po.qrSize, po.qrSize,
2026-04-28 09:45:18 +09:30
false,
fpdf.ImageOptions{AllowNegativePosition: true}, 0, "")
2026-04-28 08:26:35 +09:30
}
}
2026-04-28 12:20:14 +09:30
err = pdf.Output(w)
2026-04-28 08:26:35 +09:30
if err != nil {
2026-04-28 12:20:14 +09:30
return fmt.Errorf("could not output PDF: %w", err)
2026-04-28 08:26:35 +09:30
}
2026-04-28 12:20:14 +09:30
return nil
2026-04-28 08:26:35 +09:30
}