-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdf.go
48 lines (40 loc) · 1.05 KB
/
pdf.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
package led
import (
"io"
"net/http"
"os"
"os/exec"
)
func (f *FileHandler) pdf2txt(w http.ResponseWriter, req *http.Request) {
err := req.ParseMultipartForm(10 << 20)
if err != nil {
http.Error(w, "Unable to parse form", http.StatusBadRequest)
return
}
pdf, _, err := req.FormFile("pdf")
if err != nil {
http.Error(w, "Unable to get the file", http.StatusBadRequest)
return
}
defer pdf.Close()
tmp, err := os.CreateTemp("", "uploaded*.pdf")
if err != nil {
http.Error(w, "Unable to create a temporary file", http.StatusInternalServerError)
return
}
defer tmp.Close()
defer os.Remove(tmp.Name())
if _, err = io.Copy(tmp, pdf); err != nil {
http.Error(w, "Unable to save the file", http.StatusInternalServerError)
return
}
txt := tmp.Name() + ".txt"
cmd := exec.Command("/usr/local/bin/pdf2txt", tmp.Name(), txt)
output, err := cmd.CombinedOutput()
if err != nil {
http.Error(w, "pdf2txt err: "+err.Error()+"\n"+string(output), http.StatusInternalServerError)
return
}
defer os.Remove(txt)
http.ServeFile(w, req, txt)
}