jung-kurt / gofpdf

A PDF document generator with high level support for text, drawing and images
http://godoc.org/github.com/jung-kurt/gofpdf
MIT License
4.31k stars 777 forks source link

Cell overflow #247

Closed crosslogic closed 5 years ago

crosslogic commented 5 years ago

Is there anyway to keep the text inside the cell if it's too long?

I want to reduce the size to fit inside the cell, or cutting the part that overflows.

joewestcott commented 5 years ago

I want to reduce the size to fit inside the cell

pdf.GetStringWidth() is really useful for this. The loop below will take $str, your string to fit, and $targetWidth the cell width. (Assuming you know this value) It'll reduce the font size until it fits within.

pdf.SetFontSize(72)
for pdf.GetStringWidth(str) > targetWidth {
    fontSize, _ := pdf.GetFontSize()
    fontSize -= 0.25
    pdf.SetFontSize(fontSize)
}

It doesn't handle newlines, and definitely could be optimised. Choose your start font size and step amounts wisely!

jung-kurt commented 5 years ago

Please reopen if this is still an issue.