// ${name: wkhtmltox}
// ${title: 调用 wkhtmltox 将网页转换为 PDF}

import console;

console.setTitle("wkhtmltopdf")

// https://wkhtmltopdf.org/libwkhtmltox/
// https://wkhtmltopdf.org/libwkhtmltox/pagesettings.html 

// PDF c-bindings

/*
- wkhtmltopdf_init is called.
- A wkhtmltopdf_global_settings object is creating by calling wkhtmltopdf_create_global_settings.
- Non web page specific Setting for the conversion are set by multiple calls to wkhtmltopdf_set_global_setting.
- A wkhtmltopdf_converter object is created by calling wkhtmltopdf_create_converter, which consumes the global_settings instance.
- A number of object (web pages) are added to the conversion process, this is done by
	- Creating a wkhtmltopdf_object_settings instance by calling wkhtmltopdf_create_object_settings.
	- Setting web page specific Setting by multiple calls to wkhtmltopdf_set_object_setting.
	- Adding the object to the conversion process by calling wkhtmltopdf_add_object
- A number of callback function are added to the converter object.
- The conversion is performed by calling wkhtmltopdf_convert.
- The converter object is destroyed by calling wkhtmltopdf_destroy_converter.
*/

var dll = raw.loadDll( "D:\Tools\wkhtmltox\bin\wkhtmltox.dll" )

//获取版本号
console.printf('wkhtmltopdf %s (with patched qt)\n', raw.tostring( dll.wkhtmltopdf_versionP() ) )

// 1. CAPI(int) wkhtmltopdf_init(int use_graphics);
dll.wkhtmltopdf_init(1)

// 2. CAPI(wkhtmltopdf_global_settings *) wkhtmltopdf_create_global_settings();
var pgs = dll.wkhtmltopdf_create_global_settingsP()

// 3. CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value);
dll.wkhtmltopdf_set_global_setting(pgs, "out", io.fullpath("~\help\update-log.pdf") )

// 4. CAPI(wkhtmltopdf_converter *) wkhtmltopdf_create_converter(wkhtmltopdf_global_settings * settings);
var converter = dll.wkhtmltopdf_create_converterP( pgs )

// 5. CAPI(wkhtmltopdf_object_settings *) wkhtmltopdf_create_object_settings();
var pos = dll.wkhtmltopdf_create_object_settingsP()

// 6. CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value);
dll.wkhtmltopdf_set_object_setting(pos, "page", "http://ide.update.aardio.com/log/")

var objSettings = {
    ["header.fontSize"] = 6,
    ["header.fontName"] = "微软雅黑",
    ["header.spacing"] = 2.0,
	["header.right"] = "更 新 日 志",
	["footer.fontSize"] = 5,
	["footer.fontName"] = "Cascadia Code",
	["footer.spacing"] = 2.0,
	["footer.left"] = "[page]/[topage]",
	["footer.right"] = "http://ide.update.aardio.com/log",
}

for(k,v in objSettings) dll.wkhtmltopdf_set_object_setting(pos, k, tostring(v))

// 7. CAPI(void) wkhtmltopdf_add_object(wkhtmltopdf_converter * converter, wkhtmltopdf_object_settings * setting, const char * data);
dll.wkhtmltopdf_add_object(converter, pos, null)

// Add callback function
dll.wkhtmltopdf_set_progress_changed_callback(converter, raw.tocdecl(
    function(cter,val){
        var msg = {
			"Loading pages",
			"Counting pages",
			"Resolving links",
			"Loading headers and footers",
			"Printing pages"
		}
    	if(val==100) table.remove(msg)
		console.showLoading( string.format(" %s %s%%", msg[1], val) );	
	}, "void(point,int)"
))

dll.wkhtmltopdf_set_finished_callback(converter, raw.tocdecl(
    function(cter, val){
    	console.log('Convert pdf done.\n')
	},"void(point,int)"
))

// 8. CAPI(int) wkhtmltopdf_convert(wkhtmltopdf_converter * converter);
dll.wkhtmltopdf_convert(converter)

// 9. CAPI(void) wkhtmltopdf_destroy_converter(wkhtmltopdf_converter * converter);
dll.wkhtmltopdf_destroy_converter(converter)

// Destroy settings pointer
dll.wkhtmltopdf_destroy_global_settings(pgs)
dll.wkhtmltopdf_destroy_object_settings(pos)

dll.wkhtmltopdf_deinit()

console.pause(true);

/*
_URL = "https://suiang.cn/aardio/lib/wkhtmltox.tar.lzma"
_IMPORTURL["wkhtmltox"] = _URL
*/

// https://suiang.cn/aardio/code/wkhtmltox