image_c_api.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. * vi:set ts=4 sts=4 sw=4 noet :
  3. *
  4. * Copyright 2010, 2011 wkhtmltopdf authors
  5. *
  6. * This file is part of wkhtmltopdf.
  7. *
  8. * wkhtmltopdf is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * wkhtmltopdf is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with wkhtmltopdf. If not, see <http: *www.gnu.org/licenses/>.
  20. */
  21. /* This is a simple example program showing how to use the wkhtmltopdf c bindings */
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include <wkhtmltox/image.h>
  25. /* Print out loading progress information */
  26. void progress_changed(wkhtmltoimage_converter * c, int p) {
  27. printf("%3d%%\r",p);
  28. fflush(stdout);
  29. }
  30. /* Print loading phase information */
  31. void phase_changed(wkhtmltoimage_converter * c) {
  32. int phase = wkhtmltoimage_current_phase(c);
  33. printf("%s\n", wkhtmltoimage_phase_description(c, phase));
  34. }
  35. /* Print a message to stderr when an error occurs */
  36. void error(wkhtmltoimage_converter * c, const char * msg) {
  37. fprintf(stderr, "Error: %s\n", msg);
  38. }
  39. /* Print a message to stderr when a warning is issued */
  40. void warning(wkhtmltoimage_converter * c, const char * msg) {
  41. fprintf(stderr, "Warning: %s\n", msg);
  42. }
  43. /* Main method convert image */
  44. int main() {
  45. wkhtmltoimage_global_settings * gs;
  46. wkhtmltoimage_converter * c;
  47. const unsigned char * data;
  48. long len;
  49. /* Init wkhtmltoimage in graphics less mode */
  50. wkhtmltoimage_init(false);
  51. /*
  52. * Create a global settings object used to store options that are not
  53. * related to input objects, note that control of this object is parsed to
  54. * the converter later, which is then responsible for freeing it
  55. */
  56. gs = wkhtmltoimage_create_global_settings();
  57. /* We want to convert the qstring documentation page */
  58. wkhtmltoimage_set_global_setting(gs, "in", "http://www.google.com/");
  59. wkhtmltoimage_set_global_setting(gs, "fmt", "jpeg");
  60. /* Create the actual converter object used to convert the pages */
  61. c = wkhtmltoimage_create_converter(gs, NULL);
  62. /* Call the progress_changed function when progress changes */
  63. wkhtmltoimage_set_progress_changed_callback(c, progress_changed);
  64. /* Call the phase _changed function when the phase changes */
  65. wkhtmltoimage_set_phase_changed_callback(c, phase_changed);
  66. /* Call the error function when an error occurs */
  67. wkhtmltoimage_set_error_callback(c, error);
  68. /* Call the warning function when a warning is issued */
  69. wkhtmltoimage_set_warning_callback(c, warning);
  70. /* Perform the actual conversion */
  71. if (!wkhtmltoimage_convert(c))
  72. fprintf(stderr, "Conversion failed!");
  73. /* Output possible http error code encountered */
  74. printf("httpErrorCode: %d\n", wkhtmltoimage_http_error_code(c));
  75. len = wkhtmltoimage_get_output(c, &data);
  76. printf("%ld len\n", len);
  77. /* Destroy the converter object since we are done with it */
  78. wkhtmltoimage_destroy_converter(c);
  79. /* We will no longer be needing wkhtmltoimage funcionality */
  80. wkhtmltoimage_deinit();
  81. return 0;
  82. }