This repository has been archived by the owner on Mar 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_print.go
116 lines (104 loc) · 2.92 KB
/
controller_print.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package nametagprinter
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strings"
"text/template"
)
type PrintController struct {
template string
}
func NewPrintController(template string) (c *PrintController) {
c = new(PrintController)
c.template = template
return
}
func (c *PrintController) PrintTagHandler(w http.ResponseWriter, r *http.Request, matches []string) {
if r.Method != "POST" {
HttpProblem(w, http.StatusBadRequest, "Expected POST got "+r.Method)
return
}
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
HttpProblem(w, http.StatusBadRequest, "Expected application/json got "+r.Header.Get("Content-Type"))
return
}
tplSource, err := ioutil.ReadFile(c.template)
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed to read template")
log.Println(err.Error())
return
}
b, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
HttpProblem(w, http.StatusBadRequest, "Failed to read body: "+err.Error())
log.Println(err.Error())
return
}
var data map[string]interface{}
unmarshalErr := json.Unmarshal(b, &data)
if unmarshalErr != nil {
HttpProblem(w, http.StatusBadRequest, "Failed to parse request: "+bytes.NewBuffer(b).String())
return
}
// TODO: read this from nametag template
fields := []string{"firstname", "lastname", "twitter", "tag1", "tag2", "tag3"}
for _, field := range fields {
if data[field] == nil {
data[field] = ""
}
}
// Write template
f, err := ioutil.TempFile("", "nametag-")
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed to open temp file")
log.Println(err.Error())
return
}
log.Println("Writing to " + f.Name())
defer f.Close()
filename := f.Name()
tpl := template.Must(template.New("nametag").Parse(string(tplSource)))
err = tpl.Execute(f, data)
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed execute template")
log.Println(err.Error())
return
}
f.Close()
os.Rename(filename, filename+".svg")
svgfile := filename + ".svg"
pdffile := filename + ".pdf"
// Convert to pdf
toPdfCmd := exec.Command("inkscape", "--export-pdf="+pdffile, svgfile)
err = toPdfCmd.Start()
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed to execute inkscape")
log.Println(err.Error())
}
err = toPdfCmd.Wait()
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed convert to PDF")
log.Println(err.Error())
}
log.Println(pdffile + " written")
// Print
printCmd := exec.Command("lpr", "-P", "Brother_QL-720NW_USB", pdffile)
err = printCmd.Start()
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Failed send job to printer")
log.Println(err.Error())
}
err = printCmd.Wait()
if err != nil {
HttpProblem(w, http.StatusInternalServerError, "Printing failed")
log.Println(err.Error())
}
log.Println(pdffile + " printed")
w.Header().Add("X-Nametagprinter-Version", VERSION)
}