forked from cheggaaa/go-poppler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoppler.go
54 lines (48 loc) · 1.1 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
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
fn := C.g_filename_to_uri((*C.gchar)(C.CString(filename)), 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,
openedPopplerPages: []*C.struct__PopplerPage{},
}
return
}
func Load(data []byte) (doc *Document, err error) {
var e *C.GError
var d poppDoc
var b *C.GBytes
b = C.g_bytes_new((C.gconstpointer)(unsafe.Pointer(&data[0])), (C.ulong)(len(data)))
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())
}