1 | import os |
2 | |
3 | // Example of C interop for a very handy task. |
4 | // |
5 | // wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to |
6 | // render HTML into PDF and various image formats using the Qt WebKit rendering |
7 | // engine. These run entirely "headless" and do not require a display or display |
8 | // service. |
9 | // |
10 | // https://github.com/wkhtmltopdf/wkhtmltopdf |
11 | // https://wkhtmltopdf.org/downloads.html |
12 | // https://wkhtmltopdf.org/libwkhtmltox/ |
13 | #flag -lwkhtmltox |
14 | #include "wkhtmltox/pdf.h" # You can install the C package for your system from the wkhtmltopdf.org/downloads.html page |
15 | |
16 | struct C.wkhtmltopdf_global_settings {} |
17 | |
18 | struct C.wkhtmltopdf_object_settings {} |
19 | |
20 | struct C.wkhtmltopdf_converter {} |
21 | |
22 | fn C.wkhtmltopdf_init(use_graphics bool) int |
23 | |
24 | fn C.wkhtmltopdf_deinit() int |
25 | |
26 | fn C.wkhtmltopdf_version() &char |
27 | |
28 | fn C.wkhtmltopdf_create_global_settings() &C.wkhtmltopdf_global_settings |
29 | |
30 | fn C.wkhtmltopdf_destroy_global_settings(global_settings &C.wkhtmltopdf_global_settings) |
31 | |
32 | fn C.wkhtmltopdf_set_global_setting(global_settings &C.wkhtmltopdf_global_settings, name &char, value &char) bool |
33 | |
34 | fn C.wkhtmltopdf_create_object_settings() &C.wkhtmltopdf_object_settings |
35 | |
36 | fn C.wkhtmltopdf_destroy_object_settings(object_settings &C.wkhtmltopdf_object_settings) |
37 | |
38 | fn C.wkhtmltopdf_set_object_setting(object_settings &C.wkhtmltopdf_object_settings, name &char, value &char) bool |
39 | |
40 | fn C.wkhtmltopdf_create_converter(global_settings &C.wkhtmltopdf_global_settings) &C.wkhtmltopdf_converter |
41 | |
42 | fn C.wkhtmltopdf_destroy_converter(converter &C.wkhtmltopdf_converter) |
43 | |
44 | fn C.wkhtmltopdf_add_object(converter &C.wkhtmltopdf_converter, object_settings &C.wkhtmltopdf_object_settings, data &char) |
45 | |
46 | fn C.wkhtmltopdf_convert(converter &C.wkhtmltopdf_converter) bool |
47 | |
48 | fn C.wkhtmltopdf_http_error_code(converter &C.wkhtmltopdf_converter) int |
49 | |
50 | fn C.wkhtmltopdf_get_output(converter &C.wkhtmltopdf_converter, data &&char) int |
51 | |
52 | fn main() { |
53 | // init |
54 | init := C.wkhtmltopdf_init(0) |
55 | println('wkhtmltopdf_init: ${init}') |
56 | version := unsafe { cstring_to_vstring(&char(C.wkhtmltopdf_version())) } |
57 | println('wkhtmltopdf_version: ${version}') |
58 | global_settings := C.wkhtmltopdf_create_global_settings() |
59 | println('wkhtmltopdf_create_global_settings: ${voidptr(global_settings)}') |
60 | object_settings := C.wkhtmltopdf_create_object_settings() |
61 | println('wkhtmltopdf_create_object_settings') |
62 | converter := C.wkhtmltopdf_create_converter(global_settings) |
63 | println('wkhtmltopdf_create_converter: ${voidptr(converter)}') |
64 | // convert |
65 | mut result := C.wkhtmltopdf_set_object_setting(object_settings, c'page', c'http://www.google.com.br') |
66 | println('wkhtmltopdf_set_object_setting: ${result} [page = http://www.google.com.br]') |
67 | C.wkhtmltopdf_add_object(converter, object_settings, 0) |
68 | println('wkhtmltopdf_add_object') |
69 | result = C.wkhtmltopdf_convert(converter) |
70 | println('wkhtmltopdf_convert: ${result}') |
71 | error_code := C.wkhtmltopdf_http_error_code(converter) |
72 | println('wkhtmltopdf_http_error_code: ${error_code}') |
73 | if result { |
74 | pdata := &char(0) |
75 | ppdata := &pdata |
76 | size := C.wkhtmltopdf_get_output(converter, voidptr(ppdata)) |
77 | println('wkhtmltopdf_get_output: ${size} bytes') |
78 | mut file := os.open_file('./google.pdf', 'w+', 0o666) or { |
79 | println('ERR: ${err}') |
80 | return |
81 | } |
82 | wrote := unsafe { file.write_ptr(pdata, size) } |
83 | println('write_bytes: ${wrote} [./google.pdf]') |
84 | file.flush() |
85 | file.close() |
86 | } |
87 | // destroy |
88 | C.wkhtmltopdf_destroy_converter(converter) |
89 | println('wkhtmltopdf_destroy_converter') |
90 | C.wkhtmltopdf_destroy_object_settings(object_settings) |
91 | println('wkhtmltopdf_destroy_object_settings: ${voidptr(object_settings)}') |
92 | C.wkhtmltopdf_destroy_global_settings(global_settings) |
93 | println('wkhtmltopdf_destroy_global_settings') |
94 | deinit := C.wkhtmltopdf_deinit() |
95 | println('wkhtmltopdf_deinit: ${deinit}') |
96 | } |