diff options
| author | Kurt <kurt.w.jung@gmail.com> | 2017-08-25 16:51:57 -0400 | 
|---|---|---|
| committer | Kurt <kurt.w.jung@gmail.com> | 2017-08-25 16:51:57 -0400 | 
| commit | 4491d0fe5997270270f8e4023786afd99251f604 (patch) | |
| tree | 69b2e0e06f67890206a3bcc4a44010e9e5e24002 | |
| parent | ef5029596657de75a312ed095fd29ec7aacee058 (diff) | |
Demonstrate word-wrapping in table cells
| -rw-r--r-- | fpdf_test.go | 101 | 
1 files changed, 95 insertions, 6 deletions
| diff --git a/fpdf_test.go b/fpdf_test.go index 9b45486..db46528 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -164,13 +164,21 @@ func strDelimit(str string, sepstr string, sepcount int) string {  	return str  } +func loremList() []string { +	return []string{ +		"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " + +			"tempor incididunt ut labore et dolore magna aliqua.", +		"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut " + +			"aliquip ex ea commodo consequat.", +		"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum " + +			"dolore eu fugiat nulla pariatur.", +		"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " + +			"officia deserunt mollit anim id est laborum.", +	} +} +  func lorem() string { -	return "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod " + -		"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis " + -		"nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis " + -		"aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat " + -		"nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui " + -		"officia deserunt mollit anim id est laborum." +	return strings.Join(loremList(), " ")  }  // This example demonstrates the generation of a simple PDF document. Note that @@ -407,6 +415,87 @@ func ExampleFpdf_SetLeftMargin() {  	// Successfully generated pdf/Fpdf_SetLeftMargin_multicolumn.pdf  } +// This example demonstrates word-wrapped table cells +func ExampleFpdf_SplitLines_tables() { +	const ( +		colCount = 3 +		colWd    = 60.0 +		marginH  = 15.0 +		lineHt   = 5.5 +		cellGap  = 2.0 +	) +	// var colStrList [colCount]string +	type cellType struct { +		str  string +		list [][]byte +		ht   float64 +	} +	var ( +		cellList [colCount]cellType +		cell     cellType +	) + +	pdf := gofpdf.New("P", "mm", "A4", "") // 210 x 297 +	header := [colCount]string{"Column A", "Column B", "Column C"} +	alignList := [colCount]string{"L", "C", "R"} +	strList := loremList() +	pdf.SetMargins(marginH, 15, marginH) +	pdf.SetFont("Arial", "", 14) +	pdf.AddPage() + +	// Headers +	pdf.SetTextColor(224, 224, 224) +	pdf.SetFillColor(64, 64, 64) +	for colJ := 0; colJ < colCount; colJ++ { +		pdf.CellFormat(colWd, 10, header[colJ], "1", 0, "CM", true, 0, "") +	} +	pdf.Ln(-1) +	pdf.SetTextColor(24, 24, 24) +	pdf.SetFillColor(255, 255, 255) + +	// Rows +	y := pdf.GetY() +	count := 0 +	for rowJ := 0; rowJ < 2; rowJ++ { +		maxHt := lineHt +		// Cell height calculation loop +		for colJ := 0; colJ < colCount; colJ++ { +			count++ +			if count > len(strList) { +				count = 1 +			} +			cell.str = strings.Join(strList[0:count], " ") +			cell.list = pdf.SplitLines([]byte(cell.str), colWd-cellGap-cellGap) +			cell.ht = float64(len(cell.list)) * lineHt +			if cell.ht > maxHt { +				maxHt = cell.ht +			} +			cellList[colJ] = cell +		} +		// Cell render loop +		x := marginH +		for colJ := 0; colJ < colCount; colJ++ { +			pdf.Rect(x, y, colWd, maxHt+cellGap+cellGap, "D") +			cell = cellList[colJ] +			cellY := y + cellGap + (maxHt-cell.ht)/2 +			for splitJ := 0; splitJ < len(cell.list); splitJ++ { +				pdf.SetXY(x+cellGap, cellY) +				pdf.CellFormat(colWd-cellGap-cellGap, lineHt, string(cell.list[splitJ]), "", 0, +					alignList[colJ], false, 0, "") +				cellY += lineHt +			} +			x += colWd +		} +		y += maxHt + cellGap + cellGap +	} + +	fileStr := example.Filename("Fpdf_SplitLines_tables") +	err := pdf.OutputFileAndClose(fileStr) +	example.Summary(err, fileStr) +	// Output: +	// Successfully generated pdf/Fpdf_SplitLines_tables.pdf +} +  // This example demonstrates various table styles.  func ExampleFpdf_CellFormat_tables() {  	pdf := gofpdf.New("P", "mm", "A4", "") | 
