example.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung)
  2. //
  3. // Permission to use, copy, modify, and distribute this software for any purpose
  4. // with or without fee is hereby granted, provided that the above copyright notice
  5. // and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  12. // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // Package example provides some helper routines for the test packages of
  15. // gofpdf and its various contributed packages located beneath the contrib
  16. // directory.
  17. package example
  18. import (
  19. "fmt"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "time"
  24. "github.com/jung-kurt/gofpdf"
  25. )
  26. var gofpdfDir string
  27. func init() {
  28. setRoot()
  29. gofpdf.SetDefaultCompression(false)
  30. gofpdf.SetDefaultCatalogSort(true)
  31. gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
  32. gofpdf.SetDefaultModificationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
  33. }
  34. // setRoot assigns the relative path to the gofpdfDir directory based on current working
  35. // directory
  36. func setRoot() {
  37. wdStr, err := os.Getwd()
  38. if err == nil {
  39. gofpdfDir = ""
  40. list := strings.Split(filepath.ToSlash(wdStr), "/")
  41. for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- {
  42. gofpdfDir = filepath.Join(gofpdfDir, "..")
  43. }
  44. } else {
  45. panic(err)
  46. }
  47. }
  48. // ImageFile returns a qualified filename in which the path to the image
  49. // directory is prepended to the specified filename.
  50. func ImageFile(fileStr string) string {
  51. return filepath.Join(gofpdfDir, "image", fileStr)
  52. }
  53. // FontDir returns the path to the font directory.
  54. func FontDir() string {
  55. return filepath.Join(gofpdfDir, "font")
  56. }
  57. // FontFile returns a qualified filename in which the path to the font
  58. // directory is prepended to the specified filename.
  59. func FontFile(fileStr string) string {
  60. return filepath.Join(FontDir(), fileStr)
  61. }
  62. // TextFile returns a qualified filename in which the path to the text
  63. // directory is prepended to the specified filename.
  64. func TextFile(fileStr string) string {
  65. return filepath.Join(gofpdfDir, "text", fileStr)
  66. }
  67. // PdfDir returns the path to the PDF output directory.
  68. func PdfDir() string {
  69. return filepath.Join(gofpdfDir, "pdf")
  70. }
  71. // PdfFile returns a qualified filename in which the path to the PDF output
  72. // directory is prepended to the specified filename.
  73. func PdfFile(fileStr string) string {
  74. return filepath.Join(PdfDir(), fileStr)
  75. }
  76. // Filename returns a qualified filename in which the example PDF directory
  77. // path is prepended and the suffix ".pdf" is appended to the specified
  78. // filename.
  79. func Filename(baseStr string) string {
  80. return PdfFile(baseStr + ".pdf")
  81. }
  82. // referenceCompare compares the specified file with the file's reference copy
  83. // located in the 'reference' subdirectory. All bytes of the two files are
  84. // compared except for the value of the /CreationDate field in the PDF. This
  85. // function succeeds if both files are equivalent except for their
  86. // /CreationDate values or if the reference file does not exist.
  87. func referenceCompare(fileStr string) (err error) {
  88. var refFileStr, refDirStr, dirStr, baseFileStr string
  89. dirStr, baseFileStr = filepath.Split(fileStr)
  90. refDirStr = filepath.Join(dirStr, "reference")
  91. err = os.MkdirAll(refDirStr, 0755)
  92. if err == nil {
  93. refFileStr = filepath.Join(refDirStr, baseFileStr)
  94. err = gofpdf.ComparePDFFiles(fileStr, refFileStr, false)
  95. }
  96. return
  97. }
  98. // Summary generates a predictable report for use by test examples. If the
  99. // specified error is nil, the filename delimiters are normalized and the
  100. // filename printed to standard output with a success message. If the specified
  101. // error is not nil, its String() value is printed to standard output.
  102. func Summary(err error, fileStr string) {
  103. if err == nil {
  104. fileStr = filepath.ToSlash(fileStr)
  105. fmt.Printf("Successfully generated %s\n", fileStr)
  106. } else {
  107. fmt.Println(err)
  108. }
  109. }
  110. // SummaryCompare generates a predictable report for use by test examples. If
  111. // the specified error is nil, the generated file is compared with a reference
  112. // copy for byte-for-byte equality. If the files match, then the filename
  113. // delimiters are normalized and the filename printed to standard output with a
  114. // success message. If the files do not match, this condition is reported on
  115. // standard output. If the specified error is not nil, its String() value is
  116. // printed to standard output.
  117. func SummaryCompare(err error, fileStr string) {
  118. if err == nil {
  119. err = referenceCompare(fileStr)
  120. }
  121. if err == nil {
  122. fileStr = filepath.ToSlash(fileStr)
  123. fmt.Printf("Successfully generated %s\n", fileStr)
  124. } else {
  125. fmt.Println(err)
  126. }
  127. }