-
Notifications
You must be signed in to change notification settings - Fork 31
/
poppler.go
58 lines (50 loc) · 1.16 KB
/
poppler.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
package poppler
// #cgo pkg-config: poppler-glib
// #include <poppler.h>
// #include <stdlib.h>
// #include <glib.h>
// #include <unistd.h>
import "C"
import (
"errors"
"path/filepath"
"unsafe"
)
type poppDoc *C.struct__PopplerDocument
func Open(filename string) (doc *Document, err error) {
filename, err = filepath.Abs(filename)
if err != nil {
return
}
var e *C.GError
cfilename := (*C.gchar)(C.CString(filename))
defer C.free(unsafe.Pointer(cfilename))
fn := C.g_filename_to_uri(cfilename, nil, nil)
var d poppDoc
d = C.poppler_document_new_from_file((*C.char)(fn), nil, &e)
if e != nil {
err = errors.New(C.GoString((*C.char)(e.message)))
}
doc = &Document{
doc: d,
openedPages: []*Page{},
}
return
}
func Load(data []byte) (doc *Document, err error) {
var e *C.GError
var d poppDoc
b := C.g_bytes_new((C.gconstpointer)(unsafe.Pointer(&data[0])), (C.ulong)(len(data)))
defer C.g_bytes_unref(b)
d = C.poppler_document_new_from_bytes(b, nil, &e)
if e != nil {
err = errors.New(C.GoString((*C.char)(e.message)))
}
doc = &Document{
doc: d,
}
return
}
func Version() string {
return C.GoString(C.poppler_get_version())
}