github.com/phpdave11/gofpdf@v1.4.2/contrib/tiff/tiff.go (about) 1 /* 2 * Copyright (c) 2016 Kurt Jung (Gmail: kurt.w.jung) 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 // Package tiff allows standard (LZW-compressed) TIFF images to be used in 18 // documents generated with gofpdf. 19 package tiff 20 21 import ( 22 "bytes" 23 "fmt" 24 "image" 25 "image/png" 26 "io" 27 "os" 28 29 "github.com/phpdave11/gofpdf" 30 "golang.org/x/image/tiff" 31 ) 32 33 // RegisterReader registers a TIFF image, adding it to the PDF file but not 34 // adding it to the page. imgName specifies the name that will be used in the 35 // call to Image() that actually places the image in the document. options 36 // specifies various image properties; in this case, the ImageType property 37 // should be set to "tiff". The TIFF image is a reader from the reader 38 // specified by r. 39 func RegisterReader(fpdf *gofpdf.Fpdf, imgName string, options gofpdf.ImageOptions, r io.Reader) (info *gofpdf.ImageInfoType) { 40 var err error 41 var img image.Image 42 var buf bytes.Buffer 43 if fpdf.Ok() { 44 if options.ImageType == "tiff" || options.ImageType == "tif" { 45 img, err = tiff.Decode(r) 46 if err == nil { 47 err = png.Encode(&buf, img) 48 if err == nil { 49 options.ImageType = "png" 50 info = fpdf.RegisterImageOptionsReader(imgName, options, &buf) 51 } 52 } 53 } else { 54 err = fmt.Errorf("expecting \"tiff\" or \"tif\" as image type, got \"%s\"", options.ImageType) 55 } 56 if err != nil { 57 fpdf.SetError(err) 58 } 59 } 60 return 61 } 62 63 // RegisterFile registers a TIFF image, adding it to the PDF file but not 64 // adding it to the page. imgName specifies the name that will be used in the 65 // call to Image() that actually places the image in the document. options 66 // specifies various image properties; in this case, the ImageType property 67 // should be set to "tiff". The TIFF image is read from the file specified by 68 // tiffFileStr. 69 func RegisterFile(fpdf *gofpdf.Fpdf, imgName string, options gofpdf.ImageOptions, tiffFileStr string) (info *gofpdf.ImageInfoType) { 70 var f *os.File 71 var err error 72 73 if fpdf.Ok() { 74 f, err = os.Open(tiffFileStr) 75 if err == nil { 76 info = RegisterReader(fpdf, imgName, options, f) 77 f.Close() 78 } else { 79 fpdf.SetError(err) 80 } 81 } 82 return 83 }