From e914c59514da0d291277444b0de1b572fb9544d9 Mon Sep 17 00:00:00 2001 From: estenssoros Date: Thu, 18 Jul 2019 15:25:55 -0600 Subject: added imageoptionsreader --- fpdf.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fpdf_test.go | 23 +++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/fpdf.go b/fpdf.go index 35496c2..c34226c 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2984,6 +2984,73 @@ func (f *Fpdf) ImageOptions(imageNameStr string, x, y, w, h float64, flow bool, return } +// ImageOptionsReader registers an image, reading it from Reader r, and puts a +// JPEG, PNG or GIF image in the current page. The size it will take on the +// page can be specified in different ways. If both w and h are 0, the image +// is rendered at 96 dpi. If either w or h is zero, it will be calculated from +// the other dimension so that the aspect ratio is maintained. If w and/or h are -1, +// the dpi for that dimension will be read from the ImageInfoType object. PNG +// files can contain dpi information, and if present, this information will be +// populated in the ImageInfoType object and used in Width, Height, and Extent +// calculations. Otherwise, the SetDpi function can be used to change the dpi +// from the default of 72. +// +// If w and h are any other negative value, their absolute values +// indicate their dpi extents. +// +// Supported JPEG formats are 24 bit, 32 bit and gray scale. Supported PNG +// formats are 24 bit, indexed color, and 8 bit indexed gray scale. If a GIF +// image is animated, only the first frame is rendered. Transparency is +// supported. It is possible to put a link on the image. +// +// imageNameStr may be the name of an image as registered with a call to either +// RegisterImageReader() or RegisterImage(). In the first case, the image is +// loaded using an io.Reader. This is generally useful when the image is +// obtained from some other means than as a disk-based file. In the second +// case, the image is loaded as a file. Alternatively, imageNameStr may +// directly specify a sufficiently qualified filename. +// +// r is an io.Reader that implements the Read() function +// +// However the image is loaded, if it is used more than once only one copy is +// embedded in the file. +// +// If x is negative, the current abscissa is used. +// +// If flow is true, the current y value is advanced after placing the image and +// a page break may be made if necessary. +// +// If link refers to an internal page anchor (that is, it is non-zero; see +// AddLink()), the image will be a clickable internal link. Otherwise, if +// linkStr specifies a URL, the image will be a clickable external link. +func (f *Fpdf) ImageOptionsReader(imageNameStr string, r io.Reader, x, y, w, h float64, flow bool, options ImageOptions, link int, linkStr string) { + if f.err != nil { + return + } + + var info *ImageInfoType + + i, ok := f.images[imageNameStr] + if ok { + info = i + } else { + if options.ImageType == "" { + pos := strings.LastIndex(imageNameStr, ".") + if pos < 0 { + f.err = fmt.Errorf("image file has no extension and no type was specified: %s", imageNameStr) + return + } + options.ImageType = imageNameStr[pos+1:] + } + info = f.RegisterImageOptionsReader(imageNameStr, options, r) + if f.err != nil { + return + } + } + f.imageOut(info, x, y, w, h, options.AllowNegativePosition, flow, link, linkStr) + return +} + // RegisterImageReader registers an image, reading it from Reader r, adding it // to the PDF file but not adding it to the page. // diff --git a/fpdf_test.go b/fpdf_test.go index 7ee0831..5ef88aa 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -858,6 +858,29 @@ func ExampleFpdf_ImageOptions() { // Successfully generated pdf/Fpdf_ImageOptions.pdf } +// ImageOption struct can be used to affect horizontal image placement. +func ExampleFpdf_ImageOptionsReader() { + var opt gofpdf.ImageOptions + + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.AddPage() + pdf.SetFont("Arial", "", 11) + pdf.SetX(60) + opt.ImageType = "png" + bytes, err := ioutil.ReadAll(example.ImageFile("logo.png")) + if err!=nil{ + t.Fatal(err) + } + pdf.ImageOptionsReader(example.ImageFile("logo.png"), bytes.NewReader(bytes),-10, 10, 30, 0, false, opt, 0, "") + opt.AllowNegativePosition = true + pdf.ImageOptionsReader(example.ImageFile("logo.png"),bytes.NewReader(bytes), -10, 50, 30, 0, false, opt, 0, "") + fileStr := example.Filename("Fpdf_ImageOptionsReader") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_ImageOptionsReader.pdf +} + // This examples demonstrates Landscape mode with images. func ExampleFpdf_SetAcceptPageBreakFunc() { var y0 float64 -- cgit v1.2.1-24-ge1ad From 19ac748275dda1bd9cb6a618c380a18482a2da71 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 20 Jul 2019 10:10:49 -0400 Subject: Use file as reader for ImageOptionsReader --- fpdf_test.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/fpdf_test.go b/fpdf_test.go index 5ef88aa..54de6c2 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -861,22 +861,29 @@ func ExampleFpdf_ImageOptions() { // ImageOption struct can be used to affect horizontal image placement. func ExampleFpdf_ImageOptionsReader() { var opt gofpdf.ImageOptions + var pdfStr, imgStr string + var fl *os.File + var err error + pdfStr = example.Filename("Fpdf_ImageOptionsReader") pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "", 11) pdf.SetX(60) opt.ImageType = "png" - bytes, err := ioutil.ReadAll(example.ImageFile("logo.png")) - if err!=nil{ - t.Fatal(err) + imgStr = example.ImageFile("logo.png") + fl, err = os.Open(imgStr) + if err == nil { + pdf.ImageOptionsReader(imgStr, fl, -10, 10, 30, 0, false, opt, 0, "") + opt.AllowNegativePosition = true + _, err = fl.Seek(0, 0) + if err == nil { + pdf.ImageOptionsReader(imgStr, fl, -10, 50, 30, 0, false, opt, 0, "") + err = pdf.OutputFileAndClose(pdfStr) + } + fl.Close() } - pdf.ImageOptionsReader(example.ImageFile("logo.png"), bytes.NewReader(bytes),-10, 10, 30, 0, false, opt, 0, "") - opt.AllowNegativePosition = true - pdf.ImageOptionsReader(example.ImageFile("logo.png"),bytes.NewReader(bytes), -10, 50, 30, 0, false, opt, 0, "") - fileStr := example.Filename("Fpdf_ImageOptionsReader") - err := pdf.OutputFileAndClose(fileStr) - example.Summary(err, fileStr) + example.Summary(err, pdfStr) // Output: // Successfully generated pdf/Fpdf_ImageOptionsReader.pdf } -- cgit v1.2.1-24-ge1ad From d17d13df9df41d4505501e831ff0ada5b2222ff8 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sat, 20 Jul 2019 12:23:47 -0400 Subject: Remove ImageOptionsReader and rework example to demonstrate RegisterImageOptionsReader() --- fpdf.go | 67 ------------------------------------------------------------ fpdf_test.go | 40 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 87 deletions(-) diff --git a/fpdf.go b/fpdf.go index 319e89d..ca48fb5 100644 --- a/fpdf.go +++ b/fpdf.go @@ -2986,73 +2986,6 @@ func (f *Fpdf) ImageOptions(imageNameStr string, x, y, w, h float64, flow bool, return } -// ImageOptionsReader registers an image, reading it from Reader r, and puts a -// JPEG, PNG or GIF image in the current page. The size it will take on the -// page can be specified in different ways. If both w and h are 0, the image -// is rendered at 96 dpi. If either w or h is zero, it will be calculated from -// the other dimension so that the aspect ratio is maintained. If w and/or h are -1, -// the dpi for that dimension will be read from the ImageInfoType object. PNG -// files can contain dpi information, and if present, this information will be -// populated in the ImageInfoType object and used in Width, Height, and Extent -// calculations. Otherwise, the SetDpi function can be used to change the dpi -// from the default of 72. -// -// If w and h are any other negative value, their absolute values -// indicate their dpi extents. -// -// Supported JPEG formats are 24 bit, 32 bit and gray scale. Supported PNG -// formats are 24 bit, indexed color, and 8 bit indexed gray scale. If a GIF -// image is animated, only the first frame is rendered. Transparency is -// supported. It is possible to put a link on the image. -// -// imageNameStr may be the name of an image as registered with a call to either -// RegisterImageReader() or RegisterImage(). In the first case, the image is -// loaded using an io.Reader. This is generally useful when the image is -// obtained from some other means than as a disk-based file. In the second -// case, the image is loaded as a file. Alternatively, imageNameStr may -// directly specify a sufficiently qualified filename. -// -// r is an io.Reader that implements the Read() function -// -// However the image is loaded, if it is used more than once only one copy is -// embedded in the file. -// -// If x is negative, the current abscissa is used. -// -// If flow is true, the current y value is advanced after placing the image and -// a page break may be made if necessary. -// -// If link refers to an internal page anchor (that is, it is non-zero; see -// AddLink()), the image will be a clickable internal link. Otherwise, if -// linkStr specifies a URL, the image will be a clickable external link. -func (f *Fpdf) ImageOptionsReader(imageNameStr string, r io.Reader, x, y, w, h float64, flow bool, options ImageOptions, link int, linkStr string) { - if f.err != nil { - return - } - - var info *ImageInfoType - - i, ok := f.images[imageNameStr] - if ok { - info = i - } else { - if options.ImageType == "" { - pos := strings.LastIndex(imageNameStr, ".") - if pos < 0 { - f.err = fmt.Errorf("image file has no extension and no type was specified: %s", imageNameStr) - return - } - options.ImageType = imageNameStr[pos+1:] - } - info = f.RegisterImageOptionsReader(imageNameStr, options, r) - if f.err != nil { - return - } - } - f.imageOut(info, x, y, w, h, options.AllowNegativePosition, flow, link, linkStr) - return -} - // RegisterImageReader registers an image, reading it from Reader r, adding it // to the PDF file but not adding it to the page. // diff --git a/fpdf_test.go b/fpdf_test.go index 54de6c2..63c28c5 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -858,37 +858,37 @@ func ExampleFpdf_ImageOptions() { // Successfully generated pdf/Fpdf_ImageOptions.pdf } -// ImageOption struct can be used to affect horizontal image placement. -func ExampleFpdf_ImageOptionsReader() { - var opt gofpdf.ImageOptions - var pdfStr, imgStr string - var fl *os.File - var err error +// ExampleFpdf_RegisterImageOptionsReader demonstrates how to load an image +// from a io.Reader (in this case, a file) and register it with options. +func ExampleFpdf_RegisterImageOptionsReader() { + var ( + opt gofpdf.ImageOptions + pdfStr string + fl *os.File + err error + ) - pdfStr = example.Filename("Fpdf_ImageOptionsReader") + pdfStr = example.Filename("Fpdf_RegisterImageOptionsReader") pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "", 11) - pdf.SetX(60) - opt.ImageType = "png" - imgStr = example.ImageFile("logo.png") - fl, err = os.Open(imgStr) + fl, err = os.Open(example.ImageFile("logo.png")) if err == nil { - pdf.ImageOptionsReader(imgStr, fl, -10, 10, 30, 0, false, opt, 0, "") + opt.ImageType = "png" opt.AllowNegativePosition = true - _, err = fl.Seek(0, 0) - if err == nil { - pdf.ImageOptionsReader(imgStr, fl, -10, 50, 30, 0, false, opt, 0, "") - err = pdf.OutputFileAndClose(pdfStr) - } + _ = pdf.RegisterImageOptionsReader("logo", opt, fl) fl.Close() + for x := -20.0; x <= 40.0; x += 5 { + pdf.ImageOptions("logo", x, x+30, 0, 0, false, opt, 0, "") + } + err = pdf.OutputFileAndClose(pdfStr) } example.Summary(err, pdfStr) // Output: - // Successfully generated pdf/Fpdf_ImageOptionsReader.pdf + // Successfully generated pdf/Fpdf_RegisterImageOptionsReader.pdf } -// This examples demonstrates Landscape mode with images. +// This example demonstrates Landscape mode with images. func ExampleFpdf_SetAcceptPageBreakFunc() { var y0 float64 var crrntCol int @@ -948,7 +948,7 @@ func ExampleFpdf_SetAcceptPageBreakFunc() { // Successfully generated pdf/Fpdf_SetAcceptPageBreakFunc_landscape.pdf } -// This examples tests corner cases as reported by the gocov tool. +// This example tests corner cases as reported by the gocov tool. func ExampleFpdf_SetKeywords() { var err error fileStr := example.Filename("Fpdf_SetKeywords") -- cgit v1.2.1-24-ge1ad