github.com/phpdave11/gofpdf@v1.4.2/internal/example/example.go (about)

     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  
    15  // Package example provides some helper routines for the test packages of
    16  // gofpdf and its various contributed packages located beneath the contrib
    17  // directory.
    18  package example
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/phpdave11/gofpdf"
    28  )
    29  
    30  var gofpdfDir string
    31  
    32  func init() {
    33  	setRoot()
    34  	gofpdf.SetDefaultCompression(false)
    35  	gofpdf.SetDefaultCatalogSort(true)
    36  	gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
    37  	gofpdf.SetDefaultModificationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
    38  }
    39  
    40  // setRoot assigns the relative path to the gofpdfDir directory based on current working
    41  // directory
    42  func setRoot() {
    43  	wdStr, err := os.Getwd()
    44  	if err == nil {
    45  		gofpdfDir = ""
    46  		list := strings.Split(filepath.ToSlash(wdStr), "/")
    47  		for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- {
    48  			gofpdfDir = filepath.Join(gofpdfDir, "..")
    49  		}
    50  	} else {
    51  		panic(err)
    52  	}
    53  }
    54  
    55  // ImageFile returns a qualified filename in which the path to the image
    56  // directory is prepended to the specified filename.
    57  func ImageFile(fileStr string) string {
    58  	return filepath.Join(gofpdfDir, "image", fileStr)
    59  }
    60  
    61  // FontDir returns the path to the font directory.
    62  func FontDir() string {
    63  	return filepath.Join(gofpdfDir, "font")
    64  }
    65  
    66  // FontFile returns a qualified filename in which the path to the font
    67  // directory is prepended to the specified filename.
    68  func FontFile(fileStr string) string {
    69  	return filepath.Join(FontDir(), fileStr)
    70  }
    71  
    72  // TextFile returns a qualified filename in which the path to the text
    73  // directory is prepended to the specified filename.
    74  func TextFile(fileStr string) string {
    75  	return filepath.Join(gofpdfDir, "text", fileStr)
    76  }
    77  
    78  // PdfDir returns the path to the PDF output directory.
    79  func PdfDir() string {
    80  	return filepath.Join(gofpdfDir, "pdf")
    81  }
    82  
    83  // PdfFile returns a qualified filename in which the path to the PDF output
    84  // directory is prepended to the specified filename.
    85  func PdfFile(fileStr string) string {
    86  	return filepath.Join(PdfDir(), fileStr)
    87  }
    88  
    89  // Filename returns a qualified filename in which the example PDF directory
    90  // path is prepended and the suffix ".pdf" is appended to the specified
    91  // filename.
    92  func Filename(baseStr string) string {
    93  	return PdfFile(baseStr + ".pdf")
    94  }
    95  
    96  // referenceCompare compares the specified file with the file's reference copy
    97  // located in the 'reference' subdirectory. All bytes of the two files are
    98  // compared except for the value of the /CreationDate field in the PDF. This
    99  // function succeeds if both files are equivalent except for their
   100  // /CreationDate values or if the reference file does not exist.
   101  func referenceCompare(fileStr string) (err error) {
   102  	var refFileStr, refDirStr, dirStr, baseFileStr string
   103  	dirStr, baseFileStr = filepath.Split(fileStr)
   104  	refDirStr = filepath.Join(dirStr, "reference")
   105  	err = os.MkdirAll(refDirStr, 0755)
   106  	if err == nil {
   107  		refFileStr = filepath.Join(refDirStr, baseFileStr)
   108  		err = gofpdf.ComparePDFFiles(fileStr, refFileStr, false)
   109  	}
   110  	return
   111  }
   112  
   113  // Summary generates a predictable report for use by test examples. If the
   114  // specified error is nil, the filename delimiters are normalized and the
   115  // filename printed to standard output with a success message. If the specified
   116  // error is not nil, its String() value is printed to standard output.
   117  func Summary(err error, fileStr string) {
   118  	if err == nil {
   119  		fileStr = filepath.ToSlash(fileStr)
   120  		fmt.Printf("Successfully generated %s\n", fileStr)
   121  	} else {
   122  		fmt.Println(err)
   123  	}
   124  }
   125  
   126  // SummaryCompare generates a predictable report for use by test examples. If
   127  // the specified error is nil, the generated file is compared with a reference
   128  // copy for byte-for-byte equality. If the files match, then the filename
   129  // delimiters are normalized and the filename printed to standard output with a
   130  // success message. If the files do not match, this condition is reported on
   131  // standard output. If the specified error is not nil, its String() value is
   132  // printed to standard output.
   133  func SummaryCompare(err error, fileStr string) {
   134  	if err == nil {
   135  		err = referenceCompare(fileStr)
   136  	}
   137  	if err == nil {
   138  		fileStr = filepath.ToSlash(fileStr)
   139  		fmt.Printf("Successfully generated %s\n", fileStr)
   140  	} else {
   141  		fmt.Println(err)
   142  	}
   143  }