2021-01-03 16:56:55 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
// Example of C interop for a very handy task.
|
2021-04-05 05:43:46 +02:00
|
|
|
//
|
2021-01-03 16:56:55 +01:00
|
|
|
// wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to
|
|
|
|
// render HTML into PDF and various image formats using the Qt WebKit rendering
|
|
|
|
// engine. These run entirely "headless" and do not require a display or display
|
|
|
|
// service.
|
2021-04-05 05:43:46 +02:00
|
|
|
//
|
2021-01-03 16:56:55 +01:00
|
|
|
// https://github.com/wkhtmltopdf/wkhtmltopdf
|
|
|
|
// https://wkhtmltopdf.org/downloads.html
|
|
|
|
// https://wkhtmltopdf.org/libwkhtmltox/
|
|
|
|
#flag -lwkhtmltox
|
|
|
|
#include "wkhtmltox/pdf.h" # You can install the C package for your system from the wkhtmltopdf.org/downloads.html page
|
2021-02-23 18:43:44 +01:00
|
|
|
|
2021-01-03 16:56:55 +01:00
|
|
|
struct C.wkhtmltopdf_global_settings {}
|
|
|
|
|
|
|
|
struct C.wkhtmltopdf_object_settings {}
|
|
|
|
|
|
|
|
struct C.wkhtmltopdf_converter {}
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_init(use_graphics bool) int
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_deinit() int
|
|
|
|
|
2021-04-05 05:43:46 +02:00
|
|
|
fn C.wkhtmltopdf_version() &char
|
2021-01-03 16:56:55 +01:00
|
|
|
|
|
|
|
fn C.wkhtmltopdf_create_global_settings() &C.wkhtmltopdf_global_settings
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_destroy_global_settings(global_settings &C.wkhtmltopdf_global_settings)
|
|
|
|
|
2021-04-05 05:43:46 +02:00
|
|
|
fn wkhtmltopdf_set_global_setting(global_settings &C.wkhtmltopdf_global_settings, name &char, value &char) bool
|
2021-01-03 16:56:55 +01:00
|
|
|
|
|
|
|
fn C.wkhtmltopdf_create_object_settings() &C.wkhtmltopdf_object_settings
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_destroy_object_settings(object_settings &C.wkhtmltopdf_object_settings)
|
|
|
|
|
2021-04-05 05:43:46 +02:00
|
|
|
fn C.wkhtmltopdf_set_object_setting(object_settings &C.wkhtmltopdf_object_settings, name &char, value &char) bool
|
2021-01-03 16:56:55 +01:00
|
|
|
|
|
|
|
fn C.wkhtmltopdf_create_converter(global_settings &C.wkhtmltopdf_global_settings) &C.wkhtmltopdf_converter
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_destroy_converter(converter &C.wkhtmltopdf_converter)
|
|
|
|
|
2021-04-05 05:43:46 +02:00
|
|
|
fn C.wkhtmltopdf_add_object(converter &C.wkhtmltopdf_converter, object_settings &C.wkhtmltopdf_object_settings, data &char)
|
2021-01-03 16:56:55 +01:00
|
|
|
|
|
|
|
fn C.wkhtmltopdf_convert(converter &C.wkhtmltopdf_converter) bool
|
|
|
|
|
|
|
|
fn C.wkhtmltopdf_http_error_code(converter &C.wkhtmltopdf_converter) int
|
|
|
|
|
2021-04-05 05:43:46 +02:00
|
|
|
fn C.wkhtmltopdf_get_output(converter &C.wkhtmltopdf_converter, data &&char) int
|
2021-01-03 16:56:55 +01:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// init
|
|
|
|
init := C.wkhtmltopdf_init(0)
|
|
|
|
println('wkhtmltopdf_init: $init')
|
2021-06-05 09:03:05 +02:00
|
|
|
version := unsafe { cstring_to_vstring(&char(C.wkhtmltopdf_version())) }
|
2021-01-03 16:56:55 +01:00
|
|
|
println('wkhtmltopdf_version: $version')
|
|
|
|
global_settings := C.wkhtmltopdf_create_global_settings()
|
|
|
|
println('wkhtmltopdf_create_global_settings: ${voidptr(global_settings)}')
|
|
|
|
object_settings := C.wkhtmltopdf_create_object_settings()
|
|
|
|
println('wkhtmltopdf_create_object_settings')
|
|
|
|
converter := C.wkhtmltopdf_create_converter(global_settings)
|
|
|
|
println('wkhtmltopdf_create_converter: ${voidptr(converter)}')
|
|
|
|
// convert
|
2021-05-20 08:17:44 +02:00
|
|
|
mut result := C.wkhtmltopdf_set_object_setting(object_settings, c'page', c'http://www.google.com.br')
|
2021-01-03 16:56:55 +01:00
|
|
|
println('wkhtmltopdf_set_object_setting: $result [page = http://www.google.com.br]')
|
|
|
|
C.wkhtmltopdf_add_object(converter, object_settings, 0)
|
|
|
|
println('wkhtmltopdf_add_object')
|
|
|
|
result = C.wkhtmltopdf_convert(converter)
|
|
|
|
println('wkhtmltopdf_convert: $result')
|
|
|
|
error_code := C.wkhtmltopdf_http_error_code(converter)
|
|
|
|
println('wkhtmltopdf_http_error_code: $error_code')
|
|
|
|
if result {
|
2021-06-05 09:03:05 +02:00
|
|
|
pdata := &char(0)
|
|
|
|
ppdata := &pdata
|
|
|
|
size := C.wkhtmltopdf_get_output(converter, voidptr(ppdata))
|
2021-01-03 16:56:55 +01:00
|
|
|
println('wkhtmltopdf_get_output: $size bytes')
|
|
|
|
mut file := os.open_file('./google.pdf', 'w+', 0o666) or {
|
|
|
|
println('ERR: $err')
|
|
|
|
return
|
|
|
|
}
|
2021-06-05 09:03:05 +02:00
|
|
|
wrote := unsafe { file.write_ptr(pdata, size) }
|
2021-01-03 16:56:55 +01:00
|
|
|
println('write_bytes: $wrote [./google.pdf]')
|
|
|
|
file.flush()
|
|
|
|
file.close()
|
|
|
|
}
|
|
|
|
// destroy
|
|
|
|
C.wkhtmltopdf_destroy_converter(converter)
|
|
|
|
println('wkhtmltopdf_destroy_converter')
|
|
|
|
C.wkhtmltopdf_destroy_object_settings(object_settings)
|
|
|
|
println('wkhtmltopdf_destroy_object_settings: ${voidptr(object_settings)}')
|
|
|
|
C.wkhtmltopdf_destroy_global_settings(global_settings)
|
|
|
|
println('wkhtmltopdf_destroy_global_settings')
|
|
|
|
deinit := C.wkhtmltopdf_deinit()
|
|
|
|
println('wkhtmltopdf_deinit: $deinit')
|
|
|
|
}
|