You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用gofpdf生成PDF时如何实现换行且不重叠列表

gofpdf生成PDF列表内容重叠的解决方法

问题描述

使用gofpdf生成PDF时,循环渲染pdfData.Approval.ReviewedBy中的条目时出现重叠,期望每个条目单独一行显示,但实际所有内容都堆叠在同一位置。

原始代码:

pdf.SetFont("arial", "B", 12)
pdf.SetFont("Helvetica", "", fontXs)
pdf.Text(88, 129.9, "Prepared By")
prLeft := (120.1)
prRight := (544.20 - 2*10) / 4
pdf.SetX(prLeft)
pdf.SetY(prRight)
pdf.SetFont("Helvetica", "b", fontXs)
pdf.CellFormat(20, 4, "", "", 0, "b", false, 0, "")
pdf.CellFormat(40, 4, "Name", "1", 0, "b", false, 0, "")
pdf.CellFormat(41, 4, "Title", "1", 0, "b", false, 0, "")
pdf.CellFormat(43, 4, "Division", "1", 0, "b", false, 0, "")
pdf.CellFormat(40, 4, "Signature & Date", "1", 0, "b", false, 0, "")

for _, dataRev := range pdfData.Approval.ReviewedBy {
    pdf.SetFont("arial", "B", 12)
    pdf.SetFont("Helvetica", "", fontXs)
    pdf.Text(88, 148.1, "Reviewed By")
    asLeft := (220.1)
    asRight := (615.10 - 2*10) / 4
    pdf.SetX(asLeft)
    pdf.SetY(asRight)
    pdf.CellFormat(20, 12, "", "", 0, "", false, 0, "")
    pdf.CellFormat(40, 12, dataRev.Name, "1", 0, "", false, 0, "")
    pdf.CellFormat(41, 12, dataRev.Title, "1", 0, "", false, 0, "")
    pdf.CellFormat(43, 12, dataRev.Division, "1", 0, "", false, 0, "")
    pdf.CellFormat(40, 12, dataRev.SignatureDate, "1", 0, "", false, 0, "")
    
}

其中pdfData.Approval.ReviewedBy是包含多个元素的slice。

问题原因

  1. 循环内每次将Y坐标设置为固定值asRight,所有条目绘制在同一位置,造成重叠
  2. "Reviewed By"文本在循环内重复输出,多次覆盖同一位置
  3. 未处理换行逻辑,循环后未累加行高实现向下偏移

修改后的代码

pdf.SetFont("arial", "B", 12)
pdf.SetFont("Helvetica", "", fontXs)
pdf.Text(88, 129.9, "Prepared By")
prLeft := 120.1
prRight := (544.20 - 2*10) / 4
pdf.SetX(prLeft)
pdf.SetY(prRight)
pdf.SetFont("Helvetica", "b", fontXs)
pdf.CellFormat(20, 4, "", "", 0, "b", false, 0, "")
pdf.CellFormat(40, 4, "Name", "1", 0, "b", false, 0, "")
pdf.CellFormat(41, 4, "Title", "1", 0, "b", false, 0, "")
pdf.CellFormat(43, 4, "Division", "1", 0, "b", false, 0, "")
pdf.CellFormat(40, 4, "Signature & Date", "1", 0, "b", false, 0, "")

// 将"Reviewed By"移到循环外,仅绘制一次
pdf.SetFont("arial", "B", 12)
pdf.SetFont("Helvetica", "", fontXs)
pdf.Text(88, 148.1, "Reviewed By")
asLeft := 220.1
// 初始化起始Y坐标,后续动态累加
currentY := (615.10 - 2*10) / 4
// 统一行高,与CellFormat高度匹配
rowHeight := 12.0

for _, dataRev := range pdfData.Approval.ReviewedBy {
    pdf.SetFont("Helvetica", "", fontXs)
    pdf.SetX(asLeft)
    pdf.SetY(currentY)
    pdf.CellFormat(20, rowHeight, "", "", 0, "", false, 0, "")
    pdf.CellFormat(40, rowHeight, dataRev.Name, "1", 0, "", false, 0, "")
    pdf.CellFormat(41, rowHeight, dataRev.Title, "1", 0, "", false, 0, "")
    pdf.CellFormat(43, rowHeight, dataRev.Division, "1", 0, "", false, 0, "")
    pdf.CellFormat(40, rowHeight, dataRev.SignatureDate, "1", 0, "", false, 0, "")
    
    // 累加行高,实现换行
    currentY += rowHeight
}

关键修改说明

  • 把"Reviewed By"的渲染逻辑移到循环外,避免重复绘制
  • 用currentY变量动态跟踪当前行位置,每次循环后累加行高,确保下一行内容向下偏移
  • 统一行高变量,方便后续调整单元格高度

内容的提问来源于stack exchange,提问作者dimas tatag anditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 23:01:07