package pdf import ( "bytes" "image" "image/color" "image/png" "testing" ) func testPNG(t *testing.T, w, h int) []byte { t.Helper() img := image.NewRGBA(image.Rect(0, 0, w, h)) for x := 0; x < w; x++ { img.Set(x, h/2, color.Black) } var buf bytes.Buffer if err := png.Encode(&buf, img); err != nil { t.Fatal(err) } return buf.Bytes() } func TestDocShape(t *testing.T) { var d Doc // A 90×29 mm label at 300 dpi is 1063×343 px. if err := d.AddImagePage(testPNG(t, 1063, 343), 90, 29); err != nil { t.Fatal(err) } if err := d.AddImagePage(testPNG(t, 1063, 343), 90, 29); err != nil { t.Fatal(err) } b, err := d.Bytes() if err != nil { t.Fatal(err) } // Not a PDF parser — the shape a viewer needs to find its way in. for _, want := range []string{"%PDF-1.4", "/Count 2", "/DeviceGray", "startxref", "%%EOF"} { if !bytes.Contains(b, []byte(want)) { t.Errorf("missing %q in output", want) } } // 90 mm = 255.118 pt — the page size a driver prints 1:1 on the roll. if !bytes.Contains(b, []byte("/MediaBox [0 0 255.1181 82.2047]")) { t.Errorf("media box is not the label size") } } func TestEmptyDocRefused(t *testing.T) { var d Doc if _, err := d.Bytes(); err == nil { t.Fatal("an empty document should refuse to render") } }