| 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 | pub struct C.wkhtmltopdf_global_settings {} |
| 17 | |
| 18 | pub struct C.wkhtmltopdf_object_settings {} |
| 19 | |
| 20 | pub struct C.wkhtmltopdf_converter {} |
| 21 | |
| 22 | fn C.wkhtmltopdf_init(use_graphics bool) i32 |
| 23 | |
| 24 | fn C.wkhtmltopdf_deinit() i32 |
| 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, |
| 45 | data &char) |
| 46 | |
| 47 | fn C.wkhtmltopdf_convert(converter &C.wkhtmltopdf_converter) bool |
| 48 | |
| 49 | fn C.wkhtmltopdf_http_error_code(converter &C.wkhtmltopdf_converter) i32 |
| 50 | |
| 51 | fn C.wkhtmltopdf_get_output(converter &C.wkhtmltopdf_converter, data &&char) i32 |
| 52 | |
| 53 | fn main() { |
| 54 | // init |
| 55 | init := C.wkhtmltopdf_init(0) |
| 56 | println('wkhtmltopdf_init: ${init}') |
| 57 | version := unsafe { cstring_to_vstring(&char(C.wkhtmltopdf_version())) } |
| 58 | println('wkhtmltopdf_version: ${version}') |
| 59 | global_settings := C.wkhtmltopdf_create_global_settings() |
| 60 | println('wkhtmltopdf_create_global_settings: ${voidptr(global_settings)}') |
| 61 | object_settings := C.wkhtmltopdf_create_object_settings() |
| 62 | println('wkhtmltopdf_create_object_settings') |
| 63 | converter := C.wkhtmltopdf_create_converter(global_settings) |
| 64 | println('wkhtmltopdf_create_converter: ${voidptr(converter)}') |
| 65 | // convert |
| 66 | mut result := C.wkhtmltopdf_set_object_setting(object_settings, c'page', |
| 67 | c'http://www.google.com.br') |
| 68 | println('wkhtmltopdf_set_object_setting: ${result} [page = http://www.google.com.br]') |
| 69 | C.wkhtmltopdf_add_object(converter, object_settings, 0) |
| 70 | println('wkhtmltopdf_add_object') |
| 71 | result = C.wkhtmltopdf_convert(converter) |
| 72 | println('wkhtmltopdf_convert: ${result}') |
| 73 | error_code := C.wkhtmltopdf_http_error_code(converter) |
| 74 | println('wkhtmltopdf_http_error_code: ${error_code}') |
| 75 | if result { |
| 76 | pdata := &char(unsafe { nil }) |
| 77 | ppdata := &pdata |
| 78 | nbytes := C.wkhtmltopdf_get_output(converter, voidptr(ppdata)) |
| 79 | println('wkhtmltopdf_get_output: ${nbytes} bytes') |
| 80 | mut file := os.open_file('./google.pdf', 'w+', 0o666) or { |
| 81 | println('ERR: ${err}') |
| 82 | return |
| 83 | } |
| 84 | wrote := unsafe { file.write_ptr(pdata, nbytes) } |
| 85 | println('write_bytes: ${wrote} [./google.pdf]') |
| 86 | file.flush() |
| 87 | file.close() |
| 88 | } |
| 89 | // destroy |
| 90 | C.wkhtmltopdf_destroy_converter(converter) |
| 91 | println('wkhtmltopdf_destroy_converter') |
| 92 | C.wkhtmltopdf_destroy_object_settings(object_settings) |
| 93 | println('wkhtmltopdf_destroy_object_settings: ${voidptr(object_settings)}') |
| 94 | C.wkhtmltopdf_destroy_global_settings(global_settings) |
| 95 | println('wkhtmltopdf_destroy_global_settings') |
| 96 | deinit := C.wkhtmltopdf_deinit() |
| 97 | println('wkhtmltopdf_deinit: ${deinit}') |
| 98 | } |
| 99 | |