pdf.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package utils
  2. import (
  3. "github.com/signintech/gopdf"
  4. )
  5. const (
  6. ValignTop = 1
  7. ValignMiddle = 2
  8. ValignBottom = 3
  9. )
  10. const (
  11. AlignLeft = 4
  12. AlignCenter = 5
  13. AlignRight = 6
  14. )
  15. func RectFillColor(pdf *gopdf.GoPdf,
  16. text string,
  17. fontSize int,
  18. x, y, w, h float64,
  19. r, g, b uint8,
  20. align, valign int,
  21. ) {
  22. pdf.SetLineWidth(0.8)
  23. pdf.SetTextColor(r, g, b)
  24. pdf.SetX(x)
  25. pdf.SetY(y)
  26. pdf.CellWithOption(&gopdf.Rect{W: w, H: h}, text, gopdf.CellOption{Align: align | valign,
  27. Border: gopdf.Left | gopdf.Right | gopdf.Bottom | gopdf.Top,
  28. })
  29. }
  30. func RectFillColorMultiCell(pdf *gopdf.GoPdf,
  31. text string,
  32. fontSize int,
  33. x, y, w, h float64,
  34. r, g, b uint8,
  35. align, valign int,
  36. ) {
  37. pdf.SetLineWidth(0.8)
  38. pdf.SetFillColor(255, 255, 255) //setup fill color
  39. pdf.RectFromUpperLeftWithStyle(x, y, w, h, "FD")
  40. lineTexts, _ := pdf.SplitText(text, w)
  41. if len(lineTexts) == 0 {
  42. pdf.SetX(x)
  43. pdf.SetY(y)
  44. pdf.MultiCell(&gopdf.Rect{W: w, H: h}, "")
  45. return
  46. }
  47. if len(lineTexts) > 1 {
  48. w -= 4
  49. lineTexts, _ = pdf.SplitText(text, w)
  50. x += 4
  51. } else {
  52. if align == gopdf.Center {
  53. textw, _ := pdf.MeasureTextWidth(lineTexts[0])
  54. x = x + (w / 2) - (textw / 2)
  55. } else if align == gopdf.Right {
  56. textw, _ := pdf.MeasureTextWidth(lineTexts[0])
  57. x = x + w - textw
  58. }
  59. }
  60. pdf.SetTextColor(r, g, b)
  61. pdf.SetX(x)
  62. if valign == gopdf.Middle {
  63. y = y + (h-float64(fontSize*len(lineTexts)))/2
  64. } else if valign == gopdf.Bottom {
  65. y = y + h - float64(fontSize*len(lineTexts[0]))
  66. }
  67. pdf.SetY(y)
  68. pdf.MultiCell(&gopdf.Rect{W: w, H: h}, text)
  69. }