Required libraries:jung-kurt/gofpdf
Since the CellFormat method does not support \nBrand line breaks, it will become garbled, and the MultiCell method will automatically locate the coordinates to the next line. Therefore, you need to implement the calculation and transformation of coordinates yourself. Draw cells through the Rect method, and the MultiCell method automatically wraps lines in the grid, repeats the cells in the calculation coordinates, and finally forms a row.
refer toExampleFpdf_Rect()
Implement automatic line wrapping tables
import "/jung-kurt/gofpdf" type pdfLine struct { pdf * h float64 // The required height x float64 // Record the coordinates at the beginning y float64 // Record the coordinates at the beginning style string // Style F only fill D only border or DF both alignStr string // For its method LCR is the horizontal left, middle and right, and TMBA is the vertical upper, middle, lower and baselines fontH float64 // Font height cells []pdfCell // } type pdfCell struct { w float64 // Width h float64 //Ride height txtStr string // text lines int // Determine how many lines the text will occupy} func (s *pdfLine) addLine(style string, alignStr string, cells ...pdfCell) { = style = alignStr _, = () // Record the maximum line height required for _, cell := range cells { lines := (, ) h := float64(len(lines)) * if < h { = h } = len(lines) = append(, cell) } _, _, _, mbottom := () // Get the current page margin _, pageh := () // Get the current page size x, y := () // Get the current location // When the remaining row height of the page is not enough, open a new page if ()+ > pageh-mbottom { () y = () } = x = y () } // Writefunc (s *pdfLine) write() { x := y := // Manually record and move coordinates for _, c := range { usedH := float64() * margin := ( - usedH) / 2.0 (x, , , , ) (x, y+margin) // Keep text in the cell with margins (, , , "", , false) x += (x, y) } // Reset the coordinates to the current position of the next row (, +) // Reset variables = nil = 0 } // Use to generate a table with 4 columns per rowfunc main() { pdf := ("P", "mm", "A4", "") () pdf.AddUTF8Font("NotoSansSC-Regular", "", "src/font/") ("NotoSansSC-Regular", "", 12) myPdf := pdfLine{pdf: pdf} width, _ := () // Page width left, _, right, _ := () // Left and left margins usable := width - left - right // Available page width _,h := () // Font height tableH := h + 2 // Row height 2mm more margin tableWidth := usable / 4 // Width of each unit (233, 233, 233) // Table header ("FD", "CM", []pdfCell{ {w: tableWidth, h: tableH, txtStr: "Table 1"}, {w: tableWidth, h: tableH, txtStr: "Table 2"}, {w: tableWidth, h: tableH, txtStr: "Table 3"}, {w: tableWidth, h: tableH, txtStr: "Table 4"}, }...) // content ("", "CM", []pdfCell{ {w: tableWidth, h: tableH, txtStr: "Content 1"}, {w: tableWidth, h: tableH, txtStr: "Suppose there is a very long content here, you can replace it yourself."}, {w: tableWidth, h: tableH, txtStr: "Content 3"}, {w: tableWidth, h: tableH, txtStr: "Content 4"}, }...) }
Create a page, specify a font
// Add a page () // Load fonts pdf.AddUTF8Font("NotoSansSC-Regular", "", "src/font/") // Set font ("NotoSansSC-Regular", "", 12)
When loading the font, the directory specified by the previous New method and the directory file specified by the AddUTF8Font method will be spliced together.
Other commonly used writing methods
// Simple cell, receiving parameters are 1. Cell length 2. Row height 3. Text (cellWeight, h, "my text") // Automatically wrap the cell, calling the left side of this method will return to the beginning of the next line (0, h, "Suppose it is a very long cell") // Set the fill color (233, 233, 233) // Specify the format parameter 1. Cell length 2. Row height 3. Text 4. Border form (1 full border, or LTRB represents upper left, lower right, respectively) 5. Cell // Coordinates after writing (1 starts with the next line and 2 next line of the current coordinate) 6. Method (LCR is the horizontal left, middle, and right, and TMBA is the vertical upper, middle, and middle, // Lower, baseline) 7. Whether to fill the current grid 8. Connect 9. Connect url (tableWidth, tableH, "Total Cost", "1", 0, "M", true, 0, "") // Insert the picture. The parameters are 1 picture position 2x coordinates 3y coordinates 4 picture width 5 picture height ("src/font/", width-right-25, 5, 25, 0, false, opt, 0, "")
This is the article about using golang to realize automatic line wrapping tables in pdf. For more related contents of automatic line wrapping of golang pdf tables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!