tiff.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // Package tiff allows standard (LZW-compressed) TIFF images to be used in
  17. // documents generated with gofpdf.
  18. package tiff
  19. import (
  20. "bytes"
  21. "fmt"
  22. "image"
  23. "image/png"
  24. "io"
  25. "os"
  26. "github.com/jung-kurt/gofpdf"
  27. "golang.org/x/image/tiff"
  28. )
  29. // RegisterReader registers a TIFF image, adding it to the PDF file but not
  30. // adding it to the page. imgName specifies the name that will be used in the
  31. // call to Image() that actually places the image in the document. options
  32. // specifies various image properties; in this case, the ImageType property
  33. // should be set to "tiff". The TIFF image is a reader from the reader
  34. // specified by r.
  35. func RegisterReader(fpdf *gofpdf.Fpdf, imgName string, options gofpdf.ImageOptions, r io.Reader) (info *gofpdf.ImageInfoType) {
  36. var err error
  37. var img image.Image
  38. var buf bytes.Buffer
  39. if fpdf.Ok() {
  40. if options.ImageType == "tiff" || options.ImageType == "tif" {
  41. img, err = tiff.Decode(r)
  42. if err == nil {
  43. err = png.Encode(&buf, img)
  44. if err == nil {
  45. options.ImageType = "png"
  46. info = fpdf.RegisterImageOptionsReader(imgName, options, &buf)
  47. }
  48. }
  49. } else {
  50. err = fmt.Errorf("expecting \"tiff\" or \"tif\" as image type, got \"%s\"", options.ImageType)
  51. }
  52. if err != nil {
  53. fpdf.SetError(err)
  54. }
  55. }
  56. return
  57. }
  58. // RegisterFile registers a TIFF image, adding it to the PDF file but not
  59. // adding it to the page. imgName specifies the name that will be used in the
  60. // call to Image() that actually places the image in the document. options
  61. // specifies various image properties; in this case, the ImageType property
  62. // should be set to "tiff". The TIFF image is read from the file specified by
  63. // tiffFileStr.
  64. func RegisterFile(fpdf *gofpdf.Fpdf, imgName string, options gofpdf.ImageOptions, tiffFileStr string) (info *gofpdf.ImageInfoType) {
  65. var f *os.File
  66. var err error
  67. if fpdf.Ok() {
  68. f, err = os.Open(tiffFileStr)
  69. if err == nil {
  70. info = RegisterReader(fpdf, imgName, options, f)
  71. f.Close()
  72. } else {
  73. fpdf.SetError(err)
  74. }
  75. }
  76. return
  77. }