From c99f8c8fbf0124fcd2f509132edad09349aa3710 Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Thu, 7 Nov 2013 20:49:59 -0500 Subject: Lawrence Kesteloot provided code to allow an image's extent to be determined prior to placement. --- fpdf_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'fpdf_test.go') diff --git a/fpdf_test.go b/fpdf_test.go index ccdf1bf..b683b9c 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" "regexp" "strings" ) @@ -1083,3 +1084,52 @@ func ExampleFpdf_tutorial17() { // Output: // Successfully generated pdf/tutorial17.pdf } + +// Example to demonstrate Lawrence Kesteloot's image registration code. +func ExampleFpdf_tutorial18() { + const ( + margin = 10 + wd = 210 + ht = 297 + ) + fileList := []string{ + "logo-gray.png", + "logo.jpg", + "logo.png", + "logo-rgb.png", + "logo-progressive.jpg", + } + var infoPtr *gofpdf.ImageInfoType + var fileStr string + var imgWd, imgHt, lf, tp float64 + pdf := gofpdf.New("P", "mm", "A4", FONT_DIR) + pdf.AddPage() + pdf.SetMargins(10, 10, 10) + pdf.SetFont("Helvetica", "", 15) + for j, str := range fileList { + fileStr = filepath.Join(IMG_DIR, str) + infoPtr = pdf.RegisterImage(fileStr, "") + imgWd, imgHt = infoPtr.Extent() + switch j { + case 0: + lf = margin + tp = margin + case 1: + lf = wd - margin - imgWd + tp = margin + case 2: + lf = (wd - imgWd) / 2.0 + tp = (ht - imgHt) / 2.0 + case 3: + lf = margin + tp = ht - imgHt - margin + case 4: + lf = wd - imgWd - margin + tp = ht - imgHt - margin + } + pdf.Image(fileStr, lf, tp, imgWd, imgHt, false, "", 0, "") + } + pdf.OutputAndClose(docWriter(pdf, 18)) + // Output: + // Successfully generated pdf/tutorial18.pdf +} -- cgit v1.2.1-24-ge1ad