Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Created April 17, 2020 22:14
Show Gist options
  • Save leandrosilva/56b7a76bae018088949175eae81f06f0 to your computer and use it in GitHub Desktop.
Save leandrosilva/56b7a76bae018088949175eae81f06f0 to your computer and use it in GitHub Desktop.
wkpything - lab with python and wkhtmltox C library
import sys
import ctypes
class wkhtmltopdf_global_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_object_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_converter(ctypes.Structure):
__fields_ = []
if __name__ == "__main__":
# Windows DLL is shipped in
if sys.platform == "win32":
libwkhtmltox_path = "lib\wkhtmltox.dll"
else:
libwkhtmltox_path = "libwkhtmltox.so"
# Load the shared library into ctypes
api = ctypes.cdll.LoadLibrary(libwkhtmltox_path)
wkhtmltopdf_is_init = api.wkhtmltopdf_init(False)
print("wkhtmltopdf_is_init:", wkhtmltopdf_is_init)
api.wkhtmltopdf_create_global_settings.argtypes = None
api.wkhtmltopdf_create_global_settings.restype = ctypes.POINTER(wkhtmltopdf_global_settings)
global_settings = api.wkhtmltopdf_create_global_settings()
print("global_settings:", global_settings)
api.wkhtmltopdf_create_converter.argtypes = [ctypes.POINTER(wkhtmltopdf_global_settings)]
api.wkhtmltopdf_create_converter.restype = ctypes.POINTER(wkhtmltopdf_converter)
converter = api.wkhtmltopdf_create_converter(global_settings)
print("converter:", converter)
api.wkhtmltopdf_create_object_settings.argtypes = None
api.wkhtmltopdf_create_object_settings.restype = ctypes.POINTER(wkhtmltopdf_object_settings)
object_settings = api.wkhtmltopdf_create_object_settings()
print("object_settings:", object_settings)
api.wkhtmltopdf_set_object_setting.argtypes = [ctypes.POINTER(wkhtmltopdf_object_settings), ctypes.c_char_p, ctypes.c_char_p]
api.wkhtmltopdf_set_object_setting.restype = None
api.wkhtmltopdf_set_object_setting(object_settings, b"page", b"http://www.google.com.br");
print("page:", "http://www.google.com.br")
api.wkhtmltopdf_add_object.argtypes = [ctypes.POINTER(wkhtmltopdf_converter), ctypes.POINTER(wkhtmltopdf_object_settings), ctypes.c_char_p]
api.wkhtmltopdf_add_object.restype = None
api.wkhtmltopdf_add_object(converter, object_settings, None)
print("wkhtmltopdf_add_object")
result = api.wkhtmltopdf_convert(converter)
print("result:", result)
http_error_code = api.wkhtmltopdf_http_error_code(converter)
print("http_error_code:", http_error_code)
pdf_content = ctypes.POINTER(ctypes.c_ubyte)()
api.wkhtmltopdf_get_output.argtypes = [ctypes.POINTER(wkhtmltopdf_converter), ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte))]
api.wkhtmltopdf_get_output.restype = ctypes.c_long
pdf_content_length = api.wkhtmltopdf_get_output(converter, ctypes.byref(pdf_content))
print("pdf_content_length:", pdf_content_length)
print("pdf_content:", pdf_content)
try:
print("---")
content = bytearray(pdf_content_length)
print("content:", len(content))
for i in range(pdf_content_length):
content[i] = pdf_content[i]
if content == 0:
break
with open("_ouput_stream_google.pdf", "wb") as pdf:
pdf.write(content)
print("---")
except e:
print("fail to read pdf_content:", e)
api.wkhtmltopdf_destroy_converter(converter)
print("wkhtmltopdf_destroy_converter")
api.wkhtmltopdf_deinit()
print("wkhtmltopdf_deinit")
import sys
import ctypes
class wkhtmltopdf_global_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_object_settings(ctypes.Structure):
_fields_ = []
class wkhtmltopdf_converter(ctypes.Structure):
__fields_ = []
if __name__ == "__main__":
# Windows DLL is shipped in
if sys.platform == "win32":
libwkhtmltox_path = "lib\wkhtmltox.dll"
else:
libwkhtmltox_path = "libwkhtmltox.so"
# Load the shared library into ctypes
api = ctypes.cdll.LoadLibrary(libwkhtmltox_path)
wkhtmltopdf_is_init = api.wkhtmltopdf_init(False)
print("wkhtmltopdf_is_init:", wkhtmltopdf_is_init)
api.wkhtmltopdf_create_global_settings.argtypes = None
api.wkhtmltopdf_create_global_settings.restype = ctypes.POINTER(wkhtmltopdf_global_settings)
global_settings = api.wkhtmltopdf_create_global_settings()
print("global_settings:", global_settings)
api.wkhtmltopdf_set_global_setting.argtypes = [ctypes.POINTER(wkhtmltopdf_global_settings), ctypes.c_char_p, ctypes.c_char_p]
api.wkhtmltopdf_set_global_setting.restype = None
api.wkhtmltopdf_set_global_setting(global_settings, b'out', b'_auto_save_google.pdf')
print("wkhtmltopdf_set_global_setting")
api.wkhtmltopdf_create_converter.argtypes = [ctypes.POINTER(wkhtmltopdf_global_settings)]
api.wkhtmltopdf_create_converter.restype = ctypes.POINTER(wkhtmltopdf_converter)
converter = api.wkhtmltopdf_create_converter(global_settings)
print("converter:", converter)
api.wkhtmltopdf_create_object_settings.argtypes = None
api.wkhtmltopdf_create_object_settings.restype = ctypes.POINTER(wkhtmltopdf_object_settings)
object_settings = api.wkhtmltopdf_create_object_settings()
print("object_settings:", object_settings)
api.wkhtmltopdf_set_object_setting.argtypes = [ctypes.POINTER(wkhtmltopdf_object_settings), ctypes.c_char_p, ctypes.c_char_p]
api.wkhtmltopdf_set_object_setting.restype = None
api.wkhtmltopdf_set_object_setting(object_settings, b"page", b"http://www.google.com.br");
print("page:", "http://www.google.com.br")
api.wkhtmltopdf_add_object.argtypes = [ctypes.POINTER(wkhtmltopdf_converter), ctypes.POINTER(wkhtmltopdf_object_settings), ctypes.c_char_p]
api.wkhtmltopdf_add_object.restype = None
api.wkhtmltopdf_add_object(converter, object_settings, None)
print("wkhtmltopdf_add_object")
result = api.wkhtmltopdf_convert(converter)
print("result:", result)
http_error_code = api.wkhtmltopdf_http_error_code(converter)
print("http_error_code:", http_error_code)
api.wkhtmltopdf_destroy_converter(converter)
print("wkhtmltopdf_destroy_converter")
api.wkhtmltopdf_deinit()
print("wkhtmltopdf_deinit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment