Last active
May 19, 2021 23:07
-
-
Save leandrosilva/b0e6d094ea3532fb21d49b59a176ea18 to your computer and use it in GitHub Desktop.
pinvoke-wkhtmltox - lab with C# .NET Core and wkhtmltox C library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
namespace pinvoke_wkhtmltox | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var pageIndex = 0; | |
var pageName = string.Empty; | |
var pages = new Dictionary<string, Dictionary<string, string>> | |
{ | |
{ "http://www.google.com.br", new Dictionary<string, string>() }, | |
{ "sample1.html", new Dictionary<string, string> {{ "load.windowStatus", "ready" }} }, | |
{ "http://www.uol.com.br", new Dictionary<string, string>() }, | |
{ "sample2.html", new Dictionary<string, string> {{ "load.windowStatus", "ready" }} } | |
}; | |
bool initialized = WkLib.wkhtmltopdf_init(false); | |
Console.WriteLine("initialized: " + initialized); | |
foreach (var page in pages) | |
{ | |
pageIndex++; | |
pageName = $"page_{pageIndex}.pdf"; | |
Console.WriteLine($">>> target {pageIndex}"); | |
IntPtr global_settings = WkLib.wkhtmltopdf_create_global_settings(); | |
Console.WriteLine("global_settings: " + global_settings); | |
// int sgs = WkLib.wkhtmltopdf_set_global_setting(global_settings, "out", "google.pdf"); | |
// Console.WriteLine("wkhtmltopdf_set_global_setting: " + sgs); | |
IntPtr converter = WkLib.wkhtmltopdf_create_converter(global_settings); | |
Console.WriteLine("converter: " + converter); | |
IntPtr object_settings = WkLib.wkhtmltopdf_create_object_settings(); | |
Console.WriteLine("object_settings: " + object_settings); | |
int sos = WkLib.wkhtmltopdf_set_object_setting(object_settings, "page", page.Key); | |
Console.WriteLine($"wkhtmltopdf_set_object_setting (page = {page.Key}): {sos}"); | |
foreach (var prop in page.Value) | |
{ | |
sos = WkLib.wkhtmltopdf_set_object_setting(object_settings, prop.Key, prop.Value); | |
Console.WriteLine($"wkhtmltopdf_set_object_setting ({prop.Key} = {prop.Value}): {sos}"); | |
} | |
WkLib.wkhtmltopdf_add_object(converter, object_settings, null); | |
Console.WriteLine("wkhtmltopdf_add_object"); | |
bool converted = WkLib.wkhtmltopdf_convert(converter); | |
Console.WriteLine("converted: " + converted); | |
if (converted) | |
{ | |
IntPtr data; | |
int data_length = WkLib.wkhtmltopdf_get_output(converter, out data); | |
Console.WriteLine("data_length: " + data_length); | |
Console.WriteLine("data: " + data); | |
var raw = new byte[data_length]; | |
Marshal.Copy(data, raw, 0, data_length); | |
File.WriteAllBytes(pageName, raw); | |
Console.WriteLine(pageName); | |
} | |
WkLib.wkhtmltopdf_destroy_converter(converter); | |
Console.WriteLine("wkhtmltopdf_destroy_converter"); | |
WkLib.wkhtmltopdf_destroy_object_settings(object_settings); | |
Console.WriteLine("wkhtmltopdf_destroy_object_settings"); | |
WkLib.wkhtmltopdf_destroy_global_settings(global_settings); | |
Console.WriteLine("wkhtmltopdf_destroy_global_settings"); | |
Console.WriteLine($"<<< target {pageIndex}"); | |
} | |
bool deinitialized = WkLib.wkhtmltopdf_deinit(); | |
Console.WriteLine("deinitialized: " + deinitialized); | |
} | |
} | |
static class WkLib | |
{ | |
const string DLLNAME = "wkhtmltox"; | |
const CharSet CHARSET = CharSet.Unicode; | |
const short LPUTF8Str = 48; // custom extension to UnmanagedType | |
#region PDF | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern bool wkhtmltopdf_init(bool useGraphics); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern bool wkhtmltopdf_deinit(); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern IntPtr wkhtmltopdf_create_global_settings(); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern void wkhtmltopdf_destroy_global_settings(IntPtr global_settings); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern int wkhtmltopdf_set_global_setting(IntPtr settings, | |
[MarshalAs(LPUTF8Str)] string name, | |
[MarshalAs(LPUTF8Str)] string value); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern IntPtr wkhtmltopdf_create_converter(IntPtr global_settings); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern void wkhtmltopdf_destroy_converter(IntPtr converter); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern IntPtr wkhtmltopdf_create_object_settings(); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern void wkhtmltopdf_destroy_object_settings(IntPtr object_settings); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern int wkhtmltopdf_set_object_setting(IntPtr object_settings, | |
[MarshalAs(LPUTF8Str)] string name, | |
[MarshalAs(LPUTF8Str)] string value); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern void wkhtmltopdf_add_object(IntPtr converter, IntPtr object_settings, string data); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern bool wkhtmltopdf_convert(IntPtr converter); | |
[DllImport(DLLNAME, CharSet = CHARSET)] | |
public static extern int wkhtmltopdf_get_output(IntPtr converter, out IntPtr data); | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Sample Page 1</title> | |
</head> | |
<body> | |
<h1>Sample Page 1</h1> | |
<h2 id="htwo"></h2> | |
<h3 id="wtime"></h3> | |
<script> | |
var start = Date.now(); | |
setTimeout(function () { | |
var end = Date.now(); | |
document.getElementById("htwo").innerText = "Boooom! After " + (end-start)/1000 + " secs waiting"; | |
document.getElementById("wtime").innerText = Date.now() | |
window.status = "ready"; | |
}, 5 * 1000); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Sample Page 2</title> | |
</head> | |
<body> | |
<h1>Sample Page 1</h1> | |
<h2 id="htwo"></h2> | |
<h3 id="wtime"></h3> | |
<script> | |
var start = Date.now(); | |
setTimeout(function () { | |
var end = Date.now(); | |
document.getElementById("htwo").innerText = "Boooom! After " + (end-start)/1000 + " secs waiting"; | |
document.getElementById("wtime").innerText = Date.now() | |
window.status = "ready"; | |
}, 3 * 1000); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment