github.com/hi-fi/sss/print@v0.0.0-20230212204231-b8661fcee5d7/pkg/pdf/utils_test.go (about) 1 package pdf 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/phpdave11/gofpdf" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 var pdf *gofpdf.Fpdf 13 14 func init() { 15 init := &gofpdf.InitType{ 16 UnitStr: "point", 17 Size: gofpdf.SizeType{ 18 Wd: 105 * 2.835, 19 Ht: 297 * 2.835, 20 }, 21 } 22 os.Chdir("../../") 23 pdf = initFPDF(init) 24 pdf.SetFont("dejavu", "", 10) 25 fmt.Println("PDF init") 26 } 27 28 func TestHeightShortLines(t *testing.T) { 29 testString := "1\n2\n3\n4\n5" 30 31 needed := getCellHeightNeeded(pdf, testString, 100, 11) 32 33 assert.Equal(t, float64(55), needed, "Short line needed space should be amount of lines times height.") 34 } 35 36 func TestHeightLongLine(t *testing.T) { 37 testString := "123456789 123456789" 38 39 needed := getCellHeightNeeded(pdf, testString, 100, 11) 40 41 assert.Equal(t, float64(22), needed, "Both lines should fit to single line.") 42 } 43 44 func TestHeightLongLineBiggerFont(t *testing.T) { 45 testString := "12345 6789 12345 6789" 46 pdf.SetFont("dejavu", "", 15) 47 48 needed := getCellHeightNeeded(pdf, testString, 100, 16) 49 50 assert.Equal(t, float64(32), needed, "Font is bigger, so each line takes one line.") 51 } 52 53 // func TestHeightCalculationAgainstPdf(t *testing.T) { 54 // init := &gofpdf.InitType{ 55 // UnitStr: "point", 56 // Size: gofpdf.SizeType{ 57 // Wd: 105 * 2.835, 58 // Ht: 297 * 2.835, 59 // }, 60 // } 61 // pdf := initFPDF(init) 62 // testString := "12345\n6789\n12345\n6789" 63 64 // needed := getCellHeightNeeded(testString, 100, 16, 15) 65 // pdf.AddPage() 66 // pdf.SetY(20) 67 // pdf.SetFont("dejavu", "B", 15) 68 // startY := pdf.GetY() 69 // pdf.MultiCell(100, 16, testString, "1" "", true) 70 // pdf.Ln(-1) 71 // pdf.MultiCell(100, 16, testString, "", "", false) 72 // endY := pdf.GetY() 73 // fmt.Println(endY) 74 // assert.Equal(t, endY-startY, needed, "Font is bigger, so each line takes one line.") 75 76 // }