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.32k stars 780 forks source link

Alignment doesn't take alias expansion into account #214

Open lassik opened 5 years ago

lassik commented 5 years ago

Hi and thanks for the excellent library!

I found the following minor bug with the latest version: When you make a cell with right-aligned text, the width of the text is calculated before expanding aliases. If the alias is wider than its expansion (e.g. {nb} is wider than 1) then the text is not aligned all the way to the right. Instead, there's a gap between the text and the right border. The following program demonstrates it:

package main

import "github.com/jung-kurt/gofpdf"

func main() {
    pdf := gofpdf.New("P", "mm", "A4", "")
    pdf.SetFont("Helvetica", "", 16)
    pdf.AddPage()
    pdf.CellFormat(50, 10, "Without alias", "1", 0, "R", false, 0, "")
    pdf.CellFormat(50, 10, "With alias: {nb}", "1", 0, "R", false, 0, "")
    pdf.AliasNbPages("")
    pdf.OutputFileAndClose("test.pdf")
}
jung-kurt commented 5 years ago

Thanks for the report, @lassik. Aliasing is pretty lame with this library; the substitutions are strictly lexical with no awareness of the special formatting that put the '{nb}' key where it lands in the document. We could maybe implement some logic that puts the space difference between the key (for example, '{nb}') and the substitution value (for example, '42') some place. I will muse on this a bit.