go-pdf/fpdf

Need assistance in understanding TransformRotate()

wneessen opened this issue · 1 comments

Hi,

I am currently trying to rotate a block of text vertically in my PDF document and I have a hard time to understand how TransformRoate() operates - especially how to position the rotated element on my page. It seems that the X and Y parameters work in a different way that I would expect it.

Here is some example code I wrote:

func main() {
	pdf := fpdf.New("P", "mm", "A4", "")
	pdf.AddUTF8Font("opensans", "", "OpenSans-Regular.ttf")
	pdf.AddUTF8Font("opensans", "B", "OpenSans-Bold.ttf")
	pdf.AddUTF8Font("opensans", "I", "OpenSans-Italic.ttf")
	pdf.AddUTF8Font("opensans", "BI", "OpenSans-BoldItalic.ttf")
	pdf.AddPage()
	pdf.SetFont("opensans", "", 10)

	for i := 0.0; i <= 90; i = i + 10 {
		pdf.AddLayer(fmt.Sprintf("%f", i), true)
		pdf.SetXY(210.0/2, 297.0/2)
		pdf.TransformBegin()
		pdf.TransformRotate(i, 0, 0)
		pdf.CellFormat(0, 5, fmt.Sprintf("Angel: %f", i), "", 0, "", false, 0, "")
		pdf.TransformEnd()
	}
	if err := pdf.OutputFileAndClose("hello.pdf"); err != nil {
		fmt.Printf("error generating PDF: %s\n", err)
		os.Exit(1)
	}
}

I would expect that I have a piece of text that is starting centered on the page and is rotated from there in 10° increments. But in reallity it seem that the text is skewed in some weird way, even though the X and Y parameters are always 0. The result looks like this:

image

Does anybody has some advice or pointer to what I am doing wrong? As final goal I'd like to position vertical text boxes and a QR code in the upper left corner of the document, but given that I am basically blindly guessing right now where the rotated element would end up, I am not getting reliable re-producable results.

Any assistance is highly appreciated.

Hi again,

I believe I was able to figure it out by now. I think my error was to think that the pdf.SetXY(210.0/2, 297.0/2) would be enough to manifest the position of the transformed element and I should use 0 as value for X and Y in the TransformRotate(). When I specify the same XY coordinates in the TransformRotate() the result looks like what I was looking for. Sorry for the confusion.
image