Recent

Author Topic: WkHtmlToX wrapper.  (Read 1403 times)

dstaniak

  • Newbie
  • Posts: 3
WkHtmlToX wrapper.
« on: September 17, 2021, 07:51:50 am »
Hi Guys.

Fighting with PDF creation in FreePascal and failing on encoding unicode letters ("zażółć gęślą jaźń" :) ) brought me to use https://wkhtmltopdf.org. Instead of executing external program I've tried making a wrapper for the so/dll.

The wrapper is very simple and based on C function prototypes found in include files bundled with WkHtmlToPDF. Everything is working fine on Linux and Windows 64-bit architecture, however I'm the problem is executing the converter on 32-bit. I assume that I have made a mistake in Pascal external prototypes, but I'm completely clueless where.

Pascal prototypes :

Code: Pascal  [Select][+][-]
  1. function wkhtmltopdf_init(use_graphics : Integer) : Integer; cdecl; external 'wkhtmltox.dll';
  2. function wkhtmltopdf_deinit : Integer; cdecl; external 'wkhtmltox.dll';
  3. function wkhtmltopdf_extended_qt : Integer; cdecl; external 'wkhtmltox.dll';
  4. function wkhtmltopdf_version : PChar; cdecl; external 'wkhtmltox.dll';
  5. function wkhtmltopdf_create_global_settings : Pointer; cdecl; external 'wkhtmltox.dll';
  6. procedure wkhtmltopdf_destroy_global_settings(settings : Pointer); cdecl; external 'wkhtmltox.dll';
  7. function wkhtmltopdf_create_object_settings : Pointer; cdecl; external 'wkhtmltox.dll';
  8. procedure wkhtmltopdf_destroy_object_settings(settings : Pointer); cdecl; external 'wkhtmltox.dll';
  9. function wkhtmltopdf_set_global_setting(settings : Pointer; name, value : PChar) : Integer; cdecl; external 'wkhtmltox.dll';
  10. function wkhtmltopdf_get_global_setting(settings : Pointer; name, value : PChar; vs : Integer) : Integer; cdecl; external 'wkhtmltox.dll';
  11. function wkhtmltopdf_set_object_setting(settings : Pointer; name, value : PChar) : Integer; cdecl; external 'wkhtmltox.dll';
  12. function wkhtmltopdf_get_object_setting(settings : Pointer; name, value : PChar; vs : Integer) : Integer; cdecl; external 'wkhtmltox.dll';
  13. function wkhtmltopdf_create_converter(settings : Pointer) : Pointer; cdecl; external 'wkhtmltox.dll';
  14. procedure wkhtmltopdf_destroy_converter(converter : Pointer); cdecl; external 'wkhtmltox.dll';
  15. function wkhtmltopdf_convert(converter : Pointer) : Integer; cdecl; external 'wkhtmltox.dll';
  16. procedure wkhtmltopdf_add_object(converter, object_settings : Pointer; data : PChar); cdecl; external 'wkhtmltox.dll';
  17. function wkhtmltopdf_get_output(converter : Pointer; data : Pointer) : Integer; cdecl; external 'wkhtmltox.dll';
  18.  

Code: C  [Select][+][-]
  1. CAPI(int) wkhtmltopdf_init(int use_graphics);
  2. CAPI(int) wkhtmltopdf_deinit();
  3. CAPI(int) wkhtmltopdf_extended_qt();
  4. CAPI(const char *) wkhtmltopdf_version();
  5. CAPI(wkhtmltopdf_global_settings *) wkhtmltopdf_create_global_settings();
  6. CAPI(void) wkhtmltopdf_destroy_global_settings(wkhtmltopdf_global_settings *);
  7. CAPI(wkhtmltopdf_object_settings *) wkhtmltopdf_create_object_settings();
  8. CAPI(void) wkhtmltopdf_destroy_object_settings(wkhtmltopdf_object_settings *);
  9. CAPI(int) wkhtmltopdf_set_global_setting(wkhtmltopdf_global_settings * settings, const char * name, const char * value);
  10. CAPI(int) wkhtmltopdf_get_global_setting(wkhtmltopdf_global_settings * settings, const char * name, char * value, int vs);
  11. CAPI(int) wkhtmltopdf_set_object_setting(wkhtmltopdf_object_settings * settings, const char * name, const char * value);
  12. CAPI(int) wkhtmltopdf_get_object_setting(wkhtmltopdf_object_settings * settings, const char * name, char * value, int vs);
  13. CAPI(wkhtmltopdf_converter *) wkhtmltopdf_create_converter(wkhtmltopdf_global_settings * settings);
  14. CAPI(void) wkhtmltopdf_destroy_converter(wkhtmltopdf_converter * converter);
  15. CAPI(int) wkhtmltopdf_convert(wkhtmltopdf_converter * converter);
  16. CAPI(void) wkhtmltopdf_add_object(
  17.         wkhtmltopdf_converter * converter, wkhtmltopdf_object_settings * setting, const char * data);
  18. CAPI(long) wkhtmltopdf_get_output(wkhtmltopdf_converter * converter, const unsigned char **);
  19.  

Where "CAPI" is :
Code: C  [Select][+][-]
  1. #ifdef __cplusplus
  2.   #define CAPI(type) extern "C" DLL_PUBLIC type CALLTYPE
  3. #else
  4.   #define CAPI(type) DLL_PUBLIC type CALLTYPE
  5. #endif
  6.  

To convert a HTML to PDF one has to call :
  - create_global_settings,
  - create_converter,
  - create_object_settings,
  - add_object,
  - convert,
  - get_output,
  - destroy_object_settings,
  - destroy_converter,
  - destroy_global_settings

I'll push 64-bit version to the customer instead of 32-bit, but I cannot stand that I really don't have a clue where my prototype is wrong.

If someone proficient in C and Pascal could give me a hint, that would really be something.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: WkHtmlToX wrapper.
« Reply #1 on: September 17, 2021, 09:48:38 am »
check what CALLTYPE is. Might be stdcall on windows

dstaniak

  • Newbie
  • Posts: 3
Re: WkHtmlToX wrapper.
« Reply #2 on: September 20, 2021, 07:47:39 am »
Yes, you're right. It's stdcall, but only for WIN32. :) It's working now. Thank you!

 

TinyPortal © 2005-2018