Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
EMP Avatar asked Sep 05 '25 03:09

EMP


1 Answers

void wkhtmltopdf_add_object(wkhtmltopdf_converter * converter,
       wkhtmltopdf_object_settings * settings, const char * data)

You can provide non-null data parameter pointing to UTF-8 encoded HTML string that will be converted to PDF instead of page setting.

like image 168
Yaroslav Stavnichiy Avatar answered Sep 07 '25 23:09

Yaroslav Stavnichiy