广告平台(站长使用)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

hdl_demo1.go 3.0 KiB

4 semanas atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package hdl
  2. import (
  3. "fmt"
  4. "github.com/boombuler/barcode"
  5. "github.com/boombuler/barcode/code128"
  6. "github.com/gin-gonic/gin"
  7. "github.com/jung-kurt/gofpdf"
  8. "image/jpeg"
  9. "net/url"
  10. "os"
  11. )
  12. func Demo1(c *gin.Context) {
  13. // 创建一个新的PDF文件
  14. pdf := gofpdf.New("P", "mm", "A4", "")
  15. // 添加一页
  16. pdf.AddPage()
  17. // 设置文本颜色(参数为RGB值)
  18. pdf.SetTextColor(0, 0, 0)
  19. // 换行
  20. pdf.Ln(-1)
  21. // 引入中文字体,需要相应的字体文件
  22. pdf.AddUTF8Font("SourceHanSansCN-Light", "", "./static/SourceHanSansCN-Light.ttf")
  23. // 引入中文字体后设置中文字体和字号
  24. left := 15.0
  25. pdf.Image("./static/图片1.png", left, 25, 50, 15, false, "PNG", 0, "")
  26. pdf.SetFont("SourceHanSansCN-Light", "", 10)
  27. pdf.Text(130, 40, "杭州激活鸟网络科技有限公司")
  28. //// 生成EAN-13条形码
  29. cs, _ := code128.Encode("jihuoniao-Ads-20240913-7653481486")
  30. // 创建一个要输出数据的文件
  31. file, _ := os.Create("./static/1.jpg")
  32. defer file.Close()
  33. // 设置图片像素大小
  34. qrCode, _ := barcode.Scale(cs, 350, 100)
  35. // 将code128的条形码编码为png图片
  36. jpeg.Encode(file, qrCode, nil)
  37. pdf.Image("./static/1.jpg", left, 50, 170, 30, false, "JPEG", 0, "")
  38. pdf.SetFont("SourceHanSansCN-Light", "", 28)
  39. pdf.Text(left, 95, "激活鸟 · 变现中心结算对账单")
  40. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  41. pdf.Text(left, 105, "结 算 单 号 : jihuoniao-Ads-20240913-7653481486")
  42. pdf.Text(left, 115, "制 表 日 期 : 2024/10/29")
  43. pdf.Text(left, 125, "结 算 日 期 : 2024.08.01 - 2024.08.31")
  44. pdf.Text(left, 135, "客 户 名 称 : 天津公交易通科技有限公司")
  45. // 定义表格内容
  46. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  47. table := []string{
  48. "业务类型", "基础收益(元)", "其他调整(元)", "实际结算总计(元)",
  49. "广告合作", "1", "2", "3",
  50. }
  51. // 定义表格列数
  52. col := 4
  53. // 定义单元格位置
  54. cellWidth := 10.0 * 4
  55. cellHeight := 8.0
  56. x := left
  57. y := 140.0
  58. for i := 0; i < len(table); {
  59. pdf.SetXY(x, y)
  60. if i == col {
  61. cellHeight = 13
  62. }
  63. for j := 0; j < col; j++ {
  64. if i < len(table) {
  65. // 循环绘制单元格并输入内容
  66. pdf.CellFormat(cellWidth, cellHeight, table[i], "1", 0, "C", false, 0, "")
  67. }
  68. i++
  69. }
  70. y = y + cellHeight
  71. }
  72. pdf.SetFont("SourceHanSansCN-Light", "", 12)
  73. pdf.Text(100, 170, "实际结算总计(元):")
  74. pdf.Text(160, 170, "63531.08")
  75. pdf.Text(210/2-20, 190, "数据确认回执栏")
  76. pdf.Line(left, 193, 180, 194)
  77. pdf.Text(left, 200, "数据确认")
  78. pdf.Text(left, 210, "公司(盖章或手印):")
  79. pdf.Text(left, 220, "授权人代表签字:")
  80. pdf.Text(left, 230, "日期:")
  81. // 保存PDF文件
  82. pdfName := "hello.pdf"
  83. err := pdf.OutputFileAndClose("./static/" + pdfName)
  84. if err != nil {
  85. fmt.Println(err)
  86. return
  87. }
  88. c.Header("Content-Type", "application/octet-stream")
  89. c.Header("Content-Disposition", "attachment; filename="+url.QueryEscape(pdfName))
  90. c.Header("Content-Transfer-Encoding", "binary")
  91. c.File("./static/" + pdfName)
  92. return
  93. }