forked from sajari/docconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.go
64 lines (56 loc) · 1.48 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package docconv
import (
"fmt"
"io"
"log"
"os/exec"
"strings"
"time"
)
// ConvertPDF converts a PDF file to text.
func ConvertPDF(r io.Reader) (string, map[string]string, error) {
f, err := NewLocalFile(r, "/tmp", "sajari-convert-")
if err != nil {
return "", nil, fmt.Errorf("error creating local file: %v", err)
}
defer f.Done()
// Meta data
mc := make(chan map[string]string, 1)
go func() {
meta := make(map[string]string)
metaStr, err := exec.Command("pdfinfo", f.Name()).Output()
if err != nil {
// TODO: Remove this.
log.Println("pdfinfo:", err)
}
// Parse meta output
for _, line := range strings.Split(string(metaStr), "\n") {
if parts := strings.SplitN(line, ":", 2); len(parts) > 1 {
meta[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
}
// Convert parsed dates into unix timestamps
if tmp, ok := meta["ModDate"]; ok {
if t, err := time.Parse(time.ANSIC, tmp); err == nil {
meta["ModifiedDate"] = fmt.Sprintf("%d", t.Unix())
}
}
if tmp, ok := meta["CreationDate"]; ok {
if t, err := time.Parse(time.ANSIC, tmp); err == nil {
meta["CreatedDate"] = fmt.Sprintf("%d", t.Unix())
}
}
mc <- meta
}()
// Document body
bc := make(chan string, 1)
go func() {
body, err := exec.Command("pdftotext", "-q", "-nopgbrk", "-enc", "UTF-8", "-eol", "unix", f.Name(), "-").Output()
if err != nil {
// TODO: Remove this.
log.Println("pdftotext:", err)
}
bc <- string(body)
}()
return <-bc, <-mc, nil
}