unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.46k stars 250 forks source link

Incorrect table display occurs when multiple rows across pages #534

Closed schooltech closed 7 months ago

schooltech commented 8 months ago

Hi, Incorrect table display occurs when multiple rows across pages and the code is as below: `

pdfData := make([]map[string]string, 0) for i := 0; i < 37*20; i++ { m := make(map[string]string) m["index"] = "1" pdfData = append(pdfData, m) }

table := c.NewTable(5) table.SetMargins(10, 10, 10, 10)

drawCell := func(table *creator.Table, text string, font *model.PdfFont, colspan int, rowspan int) {
    p := c.NewStyledParagraph()
    p.Append(text).Style.Font = font
    p.SetMargins(0, 0, 5, 5)

    cell := table.MultiCell(rowspan, colspan)
    cell.SetBorder(creator.CellBorderSideAll, creator.CellBorderStyleSingle, 0.5)
    cell.SetHorizontalAlignment(creator.CellHorizontalAlignmentCenter)
    cell.SetVerticalAlignment(creator.CellVerticalAlignmentMiddle)
    cell.SetIndent(0)
    cell.SetContent(p)
}

//收费方式
drawCell(table, "multi col title", font, 5, 1)

drawCell(table, "name", font, 1, 1)
drawCell(table, "pay type", font, 1, 1)
drawCell(table, "qty", font, 1, 1)
drawCell(table, "amount", font, 1, 1)
drawCell(table, "refund", font, 1, 1)

for row, _ := range pdfData {
    if row%37 == 0 {
        drawCell(table, "multi rows", font, 2, 37)
    }
    drawCell(table, fmt.Sprintf("row %d", row), font, 2, 1)
    drawCell(table, fmt.Sprintf("row %d", row), font, 1, 1)

}

c.Draw(table)

` multirow_tbl.pdf

sampila commented 8 months ago

Hi @schooltech,

Currently the issue is taken care and will be available in the next release.

Thanks

sampila commented 7 months ago

Hi @schooltech,

We release new UniPDF version to solve this issue https://github.com/unidoc/unipdf/releases/tag/v3.52.0.

We closing this issue for now and you can re-open this issue if you still having problem after updating to the latest version of UniPDF.

schooltech commented 7 months ago

Hi @sampila , I just tested the latest v3.52.0 version and the bug is still there when span rows across pages , ` font, err := model.NewCompositePdfFontFromTTFFile("./web/public/dist/fonts/microsoft.ttf") if err != nil { log.Fatal(err) } c := creator.New() c.EnableFontSubsetting(font) var PPMM = float64(72 1.0 / 25.4) c.SetPageSize(creator.PageSize{210 PPMM, 297 * PPMM}) c.SetPageMargins(50, 50, 50, 50)

drawCell := func(table *creator.Table, text string, font *model.PdfFont, colspan int, rowspan int) {
    p := c.NewStyledParagraph()
    p.Append(text).Style.Font = font
    p.SetMargins(0, 0, 5, 5)

    cell := table.MultiCell(rowspan, colspan)
    cell.SetBorder(creator.CellBorderSideAll, creator.CellBorderStyleSingle, 0.5)
    cell.SetHorizontalAlignment(creator.CellHorizontalAlignmentCenter)
    cell.SetVerticalAlignment(creator.CellVerticalAlignmentMiddle)
    cell.SetIndent(0)
    cell.SetContent(p)
}
drawFooter("", c, font)

list := make([]map[string]string, 0)
for i := 0; i < 200; i++ {
    m := make(map[string]string)
    m[fmt.Sprintf("row-%v", i)] = fmt.Sprintf("name:tom-%v;no:naom-%v;p-0%v", i, i, i)
    list = append(list, m)
}

table := c.NewTable(3)
table.SetMargins(10, 10, 10, 10)

index := 0
for _, mp := range list {
    for k, v := range mp {
        items := strings.Split(v, ";") 
        drawCell(table, k, font, 1, len(items))
        subIndex := 0
        for _, item := range items {
            drawCell(table, item, font, 1, 1)
            if subIndex == 0 {
                drawCell(table, k, font, 1, len(items))
            }
            subIndex++
        }

    }
    index++
}

c.Draw(table)

c.WriteToFile("d:/span-row-over-page.pdf")

` span-rows-across-pages.pdf