Recent

Author Topic: PDF Creation using wkhtmltox  (Read 602 times)

sam_des

  • Newbie
  • Posts: 1
PDF Creation using wkhtmltox
« on: July 09, 2024, 05:49:12 pm »
Hi,

I have been trying to use the wkhtmltox.dll (64-BIT) with my program to create PDF from HTML file.

I am declaring wrapper functions as follows,
Code: Pascal  [Select][+][-]
  1. function wkhtmltopdf_init(useGraphics: Integer): Integer; stdcall; external 'wkhtmltox.dll';
  2. procedure wkhtmltopdf_deinit(); stdcall; external 'wkhtmltox.dll';
  3.  
  4. function wkhtmltopdf_create_global_settings: Pointer; stdcall; external 'wkhtmltox.dll';
  5. procedure wkhtmltopdf_set_global_setting(globalSettings: Pointer; name, value: PChar); stdcall; external 'wkhtmltox.dll';
  6. procedure wkhtmltopdf_set_global_setting(globalSettings: Pointer; name, value: PChar; vs: Integer); stdcall; external 'wkhtmltox.dll';
  7.  
  8. function wkhtmltopdf_create_object_settings: Pointer; stdcall; external 'wkhtmltox.dll';
  9. procedure wkhtmltopdf_set_object_setting(objectSettings: Pointer; name, value: PChar); stdcall; external 'wkhtmltox.dll';
  10. procedure wkhtmltopdf_set_object_setting(objectSettings: Pointer; name, value: PChar; vs: Integer); stdcall; external 'wkhtmltox.dll';
  11.  
  12. function wkhtmltopdf_create_converter(globalSettings: Pointer): Pointer; stdcall; external 'wkhtmltox.dll';
  13. procedure wkhtmltopdf_destroy_converter(pdfConverter: Pointer); stdcall; external 'wkhtmltox.dll';
  14.  
  15. procedure wkhtmltopdf_add_object(pdfConverter, objectSettings: Pointer; data: PChar); stdcall; external 'wkhtmltox.dll';
  16.  
  17. function wkhtmltopdf_convert(pdfConverter: Pointer): Integer; stdcall; external 'wkhtmltox.dll';  
  18.  

Then I am calling following code on button click,
Code: Pascal  [Select][+][-]
  1. var
  2.     globalSettings  : Pointer;
  3.     objectSettings  : Pointer;
  4.     pdfConverter    : Pointer;
  5. begin
  6.     if wkhtmltopdf_init(0) <> 0  then begin
  7.         globalSettings := wkhtmltopdf_create_global_settings();
  8.         wkhtmltopdf_set_global_setting(globalSettings, PChar('out'), PChar('test.pdf'));
  9.         wkhtmltopdf_set_global_setting(globalSettings, PChar('size.PageSize'), PChar('A4'));
  10.         wkhtmltopdf_set_global_setting(globalSettings, PChar('orientation'), PChar('Landscape'));
  11.  
  12.         objectSettings := wkhtmltopdf_create_object_settings();
  13.         wkhtmltopdf_set_object_setting(objectSettings, PChar('page'), PChar('rpt.html'));
  14.         wkhtmltopdf_set_object_setting(objectSettings, PChar('load.blockLocalFileAccess'), PChar('false'));
  15.  
  16.         wkhtmltopdf_set_object_setting(objectSettings, PChar('header.fontSize'), PChar('10'));
  17.         wkhtmltopdf_set_object_setting(objectSettings, PChar('header.fontName'), PChar('Arial'));
  18.         wkhtmltopdf_set_object_setting(objectSettings, PChar('header.right'), PChar('Header Right'));
  19.         wkhtmltopdf_set_object_setting(objectSettings, PChar('header.line'), PChar('true'));
  20.         wkhtmltopdf_set_object_setting(objectSettings, PChar('header.spacing'), PChar('5'));
  21.  
  22.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.fontSize'), PChar('10'));
  23.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.fontName'), PChar('Arial'));
  24.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.left'), PChar('test-left'));
  25.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.right'), PChar('[page] / [topage]'));
  26.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.line'), PChar('true'));
  27.         wkhtmltopdf_set_object_setting(objectSettings, PChar('footer.spacing'), PChar('5'));
  28.  
  29.         pdfConverter := wkhtmltopdf_create_converter(globalSettings);
  30.         wkhtmltopdf_add_object(pdfConverter, objectSettings, nil);                    //<------------ Problem
  31.         if wkhtmltopdf_convert(pdfConverter) = 0 then begin                             //<------------ Exception
  32.             MessageDlg('PDF', 'Conversion Failed', mtInformation, [mbOK], 0);
  33.         end;
  34.  
  35.         wkhtmltopdf_destroy_converter(pdfConverter);
  36.         wkhtmltopdf_deinit();
  37.     end
  38.     else begin;
  39.         MessageDlg('PDF', 'Init Failed', mtInformation, [mbOK], 0);
  40.     end;
  41. end;    
  42.  

I am facing problem with line..
Code: Pascal  [Select][+][-]
  1.         wkhtmltopdf_add_object(pdfConverter, objectSettings, nil);
  2.  

Since I want to convert local html file, as per docs, I need to pass nil as 3rd argument.
But I get the exception "FLT INVALID OPERATION' when next line is executed.

If I pass, PChar('somestring') instead of nil then PDF gets generated with 'somestring' & all the header/footer object settings as expected.

Also I have tried the same html file with same dll, only in c++ and I get entire PDF as expected.

Any help would be greatly appreciated.

sam_des

 

TinyPortal © 2005-2018