This repository has been archived by the owner on Jan 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (77 loc) · 3.37 KB
/
main.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
package main
/*
#cgo LDFLAGS: -lwkhtmltox
#cgo darwin CFLAGS: -I/usr/local/include/wkhtmltox
#cgo linux CFLAGS: -I/usr/include/wkhtmltox
#include <stdbool.h>
#include <stdio.h>
#include <image.h>
#include <pdf.h>
void wkhtmltopdfStrCallback(wkhtmltopdf_converter * converter, const char * str);
void wkhtmltopdfIntCallback(wkhtmltopdf_converter * converter, int val);
void wkhtmltopdfFinishedCallback(wkhtmltopdf_converter * converter, int val);
void wkhtmltopdfVoidCallback(wkhtmltopdf_converter * converter);
*/
import "C"
import (
"log"
"unsafe"
)
//export goWkhtmltopdfStrCallback
func goWkhtmltopdfStrCallback(converter *C.wkhtmltopdf_converter, str *C.char) {
log.Printf("Str: %v", C.GoString(str))
}
//export goWkhtmltopdfIntCallback
func goWkhtmltopdfIntCallback(converter *C.wkhtmltopdf_converter, percent C.int) {
log.Printf("Progress: %d", percent)
}
//export goWkhtmltopdfFinishedCallback
func goWkhtmltopdfFinishedCallback(converter *C.wkhtmltopdf_converter, intval C.int) {
log.Printf("Done! %v", intval)
}
//export goWkhtmltopdfVoidCallback
func goWkhtmltopdfVoidCallback(converter *C.wkhtmltopdf_converter) {
var phaseCount = C.wkhtmltopdf_phase_count(converter) - 1
var currentPhase = C.wkhtmltopdf_current_phase(converter)
log.Printf("%v (%d/%d)", C.GoString(C.wkhtmltopdf_phase_description(converter, currentPhase)), currentPhase, phaseCount)
}
func main() {
log.Printf("Version %v", C.GoString(C.wkhtmltopdf_version()))
var isExtendedQT = C.wkhtmltopdf_extended_qt() == C.int(1)
log.Printf("Extended QT? %v", isExtendedQT)
C.wkhtmltopdf_init(C.int(map[bool]int{true: 1, false: 0}[false]))
var out = "out.pdf"
var settings = C.wkhtmltopdf_create_global_settings()
var globalSettings = [][2]string{
{"out", out},
{"size.paperSize", "A4"},
{"orientation", "Portrait"},
}
for _, setting := range globalSettings {
C.wkhtmltopdf_set_global_setting(settings, C.CString(setting[0]), C.CString(setting[1]))
}
var pageSettings = C.wkhtmltopdf_create_object_settings()
// {"page", "http://wkhtmltopdf.org/libwkhtmltox/"},
var objectSettings = [][2]string{
{"page", "http://www.google.de/"}, // - = read from stdin
{"web.defaultEncoding", "utf-8"},
{"web.enableJavascript", "true"},
{"footer.center", "[page] / [toPage]"},
}
for _, setting := range objectSettings {
C.wkhtmltopdf_set_object_setting(pageSettings, C.CString(setting[0]), C.CString(setting[1]))
}
var converter = C.wkhtmltopdf_create_converter(settings)
C.wkhtmltopdf_add_object(converter, pageSettings, nil)
C.wkhtmltopdf_set_warning_callback(converter, (C.wkhtmltopdf_str_callback)(unsafe.Pointer(C.wkhtmltopdfStrCallback)))
C.wkhtmltopdf_set_error_callback(converter, (C.wkhtmltopdf_str_callback)(unsafe.Pointer(C.wkhtmltopdfStrCallback)))
C.wkhtmltopdf_set_phase_changed_callback(converter, (C.wkhtmltopdf_void_callback)(unsafe.Pointer(C.wkhtmltopdfVoidCallback)))
C.wkhtmltopdf_set_progress_changed_callback(converter, (C.wkhtmltopdf_int_callback)(unsafe.Pointer(C.wkhtmltopdfIntCallback)))
C.wkhtmltopdf_set_finished_callback(converter, (C.wkhtmltopdf_int_callback)(unsafe.Pointer(C.wkhtmltopdfFinishedCallback)))
log.Printf("%d", C.wkhtmltopdf_convert(converter)) // 1 success, 0 failure
// wkhtmltopdf_get_output if out is missing
C.wkhtmltopdf_destroy_object_settings(pageSettings)
C.wkhtmltopdf_destroy_converter(converter)
C.wkhtmltopdf_destroy_global_settings(settings)
C.wkhtmltopdf_deinit()
}