GoPDF.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package lib
  2. import "github.com/signintech/gopdf"
  3. const (
  4. ValignTop = 1
  5. ValignMiddle = 2
  6. ValignBottom = 3
  7. )
  8. const (
  9. AlignLeft = 4
  10. AlignCenter = 5
  11. AlignRight = 6
  12. )
  13. // RectFillColorWithWrap 支持文字换行的矩形填充函数
  14. func RectFillColorWithWrap(pdf *gopdf.GoPdf,
  15. text string,
  16. fontSize int,
  17. x, y, w, h float64,
  18. r, g, b uint8,
  19. align, valign int,
  20. ) {
  21. // 绘制背景矩形
  22. pdf.SetLineWidth(0.1)
  23. pdf.SetFillColor(r, g, b) //setup fill color
  24. pdf.SetLineType("") // 线条样式
  25. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  26. pdf.SetFillColor(0, 0, 0)
  27. // 测量文字宽度,判断是否需要换行
  28. textw, _ := pdf.MeasureTextWidth(text)
  29. // 如果文字宽度超过单元格宽度,进行换行处理
  30. if textw > w-4 { // 预留一些边距
  31. // 换行时使用较小的字体,为上下留出空白
  32. adjustedFontSize := fontSize - 2
  33. if adjustedFontSize < 8 {
  34. adjustedFontSize = 8 // 最小字体大小
  35. }
  36. // 设置调整后的字体大小
  37. currentFont := "wts" // 假设当前使用的字体名称
  38. pdf.SetFont(currentFont, "", adjustedFontSize)
  39. // 将文字按字符分割,逐个测量直到超出宽度
  40. runes := []rune(text)
  41. lines := []string{}
  42. currentLine := ""
  43. for _, r := range runes {
  44. testLine := currentLine + string(r)
  45. testWidth, _ := pdf.MeasureTextWidth(testLine)
  46. if testWidth > w-4 && currentLine != "" {
  47. // 当前行已满,开始新行
  48. lines = append(lines, currentLine)
  49. currentLine = string(r)
  50. } else {
  51. currentLine = testLine
  52. }
  53. }
  54. // 添加最后一行
  55. if currentLine != "" {
  56. lines = append(lines, currentLine)
  57. }
  58. // 计算行高和总高度,为上下留出空白
  59. lineHeight := float64(adjustedFontSize) + 1 // 行间距稍微小一点
  60. totalTextHeight := float64(len(lines)) * lineHeight
  61. padding := 2.0 // 上下各留2个单位的空白
  62. startY := y + padding
  63. if valign == ValignMiddle {
  64. startY = y + (h / 2) - (totalTextHeight / 2)
  65. } else if valign == ValignBottom {
  66. startY = y + h - totalTextHeight - padding
  67. }
  68. // 绘制每一行
  69. for i, line := range lines {
  70. lineY := startY + float64(i)*lineHeight
  71. lineX := x + 2 // 预留左边距
  72. if align == AlignCenter {
  73. lineWidth, _ := pdf.MeasureTextWidth(line)
  74. lineX = x + (w / 2) - (lineWidth / 2)
  75. } else if align == AlignRight {
  76. lineWidth, _ := pdf.MeasureTextWidth(line)
  77. lineX = x + w - lineWidth - 2
  78. }
  79. pdf.SetX(lineX)
  80. pdf.SetY(lineY)
  81. pdf.Cell(nil, line)
  82. }
  83. // 恢复原来的字体大小
  84. pdf.SetFont(currentFont, "", fontSize)
  85. } else {
  86. // 文字不需要换行,使用原来的逻辑
  87. if align == AlignCenter {
  88. x = x + (w / 2) - (textw / 2)
  89. } else if align == AlignRight {
  90. x = x + w - textw
  91. }
  92. pdf.SetX(x)
  93. if valign == ValignMiddle {
  94. y = y + (h / 2) - (float64(fontSize) / 2)
  95. } else if valign == ValignBottom {
  96. y = y + h - float64(fontSize)
  97. }
  98. pdf.SetY(y)
  99. pdf.Cell(nil, text)
  100. }
  101. }
  102. func RectFillColor(pdf *gopdf.GoPdf,
  103. text string,
  104. fontSize int,
  105. x, y, w, h float64,
  106. r, g, b uint8,
  107. align, valign int,
  108. ) {
  109. pdf.SetLineWidth(0.1)
  110. pdf.SetFillColor(r, g, b) //setup fill color
  111. pdf.SetLineType("") // 线条样式
  112. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  113. pdf.SetFillColor(0, 0, 0)
  114. if align == AlignCenter {
  115. textw, _ := pdf.MeasureTextWidth(text)
  116. x = x + (w / 2) - (textw / 2)
  117. } else if align == AlignRight {
  118. textw, _ := pdf.MeasureTextWidth(text)
  119. x = x + w - textw
  120. }
  121. pdf.SetX(x)
  122. if valign == ValignMiddle {
  123. y = y + (h / 2) - (float64(fontSize) / 2)
  124. } else if valign == ValignBottom {
  125. y = y + h - float64(fontSize)
  126. }
  127. pdf.SetY(y)
  128. pdf.Cell(nil, text)
  129. }