Search code examples
htmlc++cpdfwkhtmltopdf

Convert html string to pdf using wkhtmltopdf C library (wkhtmltox)


I am trying to convert html string to pdf using wkhtmltopdf C library. I checked the sample that comes together with the installer and I successfully compiled and converted the page. Here is the code that I used for reference.

wkhtmltopdf_global_settings * gs;
wkhtmltopdf_object_settings * os;
wkhtmltopdf_converter * c;

wkhtmltopdf_init(false);

gs = wkhtmltopdf_create_global_settings();

wkhtmltopdf_set_global_setting(gs, "out", "test.pdf");

os = wkhtmltopdf_create_object_settings();

wkhtmltopdf_set_object_setting(os, "page", "https://www.google.com.ph");

c = wkhtmltopdf_create_converter(gs);

wkhtmltopdf_set_progress_changed_callback(c, progress_changed);

wkhtmltopdf_set_phase_changed_callback(c, phase_changed);

wkhtmltopdf_set_error_callback(c, error);

wkhtmltopdf_set_warning_callback(c, warning);

wkhtmltopdf_add_object(c, os, NULL);

if (!wkhtmltopdf_convert(c))
    fprintf(stderr, "Convertion failed!");

printf("httpErrorCode: %d\n", wkhtmltopdf_http_error_code(c));

wkhtmltopdf_destroy_converter(c);

wkhtmltopdf_deinit();

I am suspecting that changing the parameters of the function

   wkhtmltopdf_set_object_setting(os, "page", "https://www.google.com.ph");

to something like

   wkhtmltopdf_set_object_setting(os, [name of setting], [html string]); 

will do the job, however the documentation didn't specify any name for a particular setting aside from the one included in the sample which is "page".

Maybe I missed it in the documentation or maybe this is not the right way to do it. Any suggestion will be very much appreciated.


Solution

  • Thanks you for your time looking into this. I ended up with workaround. Instead of string i passed the local path of an generated html file. Its not the solution that I am looking for but it does the same thing. Thanks again.