-
Notifications
You must be signed in to change notification settings - Fork 3
/
actionFill.go
94 lines (71 loc) · 2.43 KB
/
actionFill.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"os"
"path/filepath"
"github.com/spf13/cobra"
)
var (
drawGrid bool
drawCellBorder bool
useDummyValues bool
)
// GetFillCommand returns the cobra command for the "fill" action.
func GetFillCommand() (cmd *cobra.Command) {
fillCmd := &cobra.Command{
Use: "fill <template> <infile> <outfile>",
Aliases: []string{"f"},
Short: "Fill out a single chronicle sheet",
Long: "Fill out a single chronicle sheet with parameters provided on the command line.",
Args: cobra.MinimumNArgs(3),
Run: executeFill,
}
fillCmd.Flags().BoolVarP(&drawGrid, "grid", "g", false, "Draw a coordinate grid on the output file")
fillCmd.Flags().BoolVarP(&drawCellBorder, "cellBorder", "c", false, "Draw the cell borders of all added fields")
fillCmd.Flags().BoolVarP(&useDummyValues, "dummyValues", "d", false, "Use dummy values to fill out the chronicle.")
return fillCmd
}
func executeFill(cmd *cobra.Command, args []string) {
Assert(len(args) >= 3, "Number of arguments should be guaranteed by cobra settings")
tmplName := args[0]
inFile := args[1]
outFile := args[2]
Assert(inFile != outFile, "Input file and output file must not be identical")
cTmpl, err := GetTemplate(tmplName)
ExitOnError(err, "Error getting template")
// parse remaining arguments
var argStore ArgStore
if !useDummyValues {
argStore = ArgStoreFromArgs(args[3:])
} else {
argStore = ArgStoreFromTemplateExamples(cTmpl)
}
// prepare temporary working dir
workDir := GetTempDir()
defer os.RemoveAll(workDir)
pdf, err := NewPdf(inFile)
AssertNoError(err)
// extract chronicle page from pdf
extractedPage, err := pdf.ExtractPage(-1, workDir)
ExitOnError(err, "Error extracing page from %v", pdf.filename)
width, height := extractedPage.GetDimensionsInPoints()
// create stamp
stamp := NewStamp(width, height)
if drawCellBorder {
stamp.SetCellBorder(true)
}
// add content to stamp
for key, value := range argStore {
//fmt.Printf("Processing Key='%v', value='%v'\n", key, *value)
content, exists := cTmpl.GetContent(key)
Assert(exists, "No content with key="+key)
stamp.AddContent(content, &value)
}
if drawGrid {
stamp.CreateMeasurementCoordinates(25, 5)
}
// write stamp
stampFile := filepath.Join(workDir, "stamp.pdf")
stamp.WriteToFile(stampFile)
// add watermark/stamp to page
extractedPage.StampIt(stampFile, outFile)
}