Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Table with text vertically centered #313

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gopdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ func (gp *GoPdf) MultiCellWithOption(rectangle *Rect, text string, opt CellOptio
}

for _, text := range textSplits {
gp.CellWithOption(&Rect{W: rectangle.W, H: lineHeight}, string(text), opt)
gp.CellWithOption(&Rect{W: rectangle.W, H: rectangle.H}, string(text), opt)
gp.Br(lineHeight)
gp.SetX(x)
}
Expand Down
107 changes: 107 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,110 @@ func TestTable(t *testing.T) {
t.Errorf("Error saving PDF: %v", err)
}
}

func TestTableCenter(t *testing.T) {
// Create a new PDF document
pdf := &gopdf.GoPdf{}
// Start the PDF with a custom page size (we'll adjust it later)
pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 430, H: 200}})
// Add a new page to the document
pdf.AddPage()

err := pdf.AddTTFFont("LiberationSerif-Regular", "./test/res/LiberationSerif-Regular.ttf")
if err != nil {
t.Fatalf("Error loading font: %v", err)
return
}

err = pdf.SetFont("LiberationSerif-Regular", "", 11)
if err != nil {
t.Fatalf("Error set font: %v", err)
return
}
err = pdf.AddTTFFont("Ubuntu-L.ttf", "./examples/outline_example/Ubuntu-L.ttf")
if err != nil {
t.Fatalf("Error loading font: %v", err)
return
}

err = pdf.SetFont("Ubuntu-L.ttf", "", 11)
if err != nil {
t.Fatalf("Error set font: %v", err)
return
}

// Set the starting Y position for the table
tableStartY := 10.0
// Set the left margin for the table
marginLeft := 10.0

// Create a new table layout
table := pdf.NewTableLayout(marginLeft, tableStartY, 25, 5)

// Add columns to the table
table.AddColumn("CODE", 50, "center")
table.AddColumn("DESCRIPTION", 200, "center")
table.AddColumn("QTY.", 40, "center")
table.AddColumn("PRICE", 60, "center")
table.AddColumn("TOTAL", 60, "center")

// Add rows to the table
table.AddRow([]string{"001", "Product A", "2", "10.00", "20.00"})
table.AddRow([]string{"002", "Product B", "1", "15.00", "15.00"})
table.AddRow([]string{"003", "Product C", "3", "5.00", "15.00"})

// Set the style for table cells
table.SetTableStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Top: true,
Left: true,
Bottom: true,
Right: true,
Width: 1.0,
},
FillColor: gopdf.RGBColor{R: 255, G: 255, B: 255},
TextColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
FontSize: 10,
})

// Set the style for table header
table.SetHeaderStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Top: true,
Left: true,
Bottom: true,
Right: true,
Width: 2.0,
RGBColor: gopdf.RGBColor{R: 100, G: 150, B: 255},
},
FillColor: gopdf.RGBColor{R: 255, G: 200, B: 200},
TextColor: gopdf.RGBColor{R: 255, G: 100, B: 100},
Font: "Ubuntu-L.ttf",
FontSize: 12,
})

table.SetCellStyle(gopdf.CellStyle{
BorderStyle: gopdf.BorderStyle{
Right: true,
Bottom: true,
Width: 0.5,
RGBColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
},
FillColor: gopdf.RGBColor{R: 255, G: 255, B: 255},
TextColor: gopdf.RGBColor{R: 0, G: 0, B: 0},
Font: "LiberationSerif-Regular",
FontSize: 10,
})

// Draw the table
err = table.DrawTable()
if err != nil {
t.Errorf("Error drawing table: %v", err)
}

// Save the PDF to the specified path
err = pdf.WritePdf("examples/table/example_table_center.pdf")
if err != nil {
t.Errorf("Error saving PDF: %v", err)
}
}
Loading