pdf_c_api.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/pdf.h>
  25. /* Print out loading progress information */
  26. void progress_changed(wkhtmltopdf_converter * c, int p) {
  27. printf("%3d%%\r",p);
  28. fflush(stdout);
  29. }
  30. /* Print loading phase information */
  31. void phase_changed(wkhtmltopdf_converter * c) {
  32. int phase = wkhtmltopdf_current_phase(c);
  33. printf("%s\n", wkhtmltopdf_phase_description(c, phase));
  34. }
  35. /* Print a message to stderr when an error occurs */
  36. void error(wkhtmltopdf_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(wkhtmltopdf_converter * c, const char * msg) {
  41. fprintf(stderr, "Warning: %s\n", msg);
  42. }
  43. /* Main method convert pdf */
  44. int main() {
  45. wkhtmltopdf_global_settings * gs;
  46. wkhtmltopdf_object_settings * os;
  47. wkhtmltopdf_converter * c;
  48. /* Init wkhtmltopdf in graphics less mode */
  49. wkhtmltopdf_init(false);
  50. /*
  51. * Create a global settings object used to store options that are not
  52. * related to input objects, note that control of this object is parsed to
  53. * the converter later, which is then responsible for freeing it
  54. */
  55. gs = wkhtmltopdf_create_global_settings();
  56. /* We want the result to be storred in the file called test.pdf */
  57. wkhtmltopdf_set_global_setting(gs, "out", "test.pdf");
  58. wkhtmltopdf_set_global_setting(gs, "load.cookieJar", "myjar.jar");
  59. /*
  60. * Create a input object settings object that is used to store settings
  61. * related to a input object, note again that control of this object is parsed to
  62. * the converter later, which is then responsible for freeing it
  63. */
  64. os = wkhtmltopdf_create_object_settings();
  65. /* We want to convert to convert the qstring documentation page */
  66. wkhtmltopdf_set_object_setting(os, "page", "http://doc.qt.io/qt-5/qstring.html");
  67. /* Create the actual converter object used to convert the pages */
  68. c = wkhtmltopdf_create_converter(gs);
  69. /* Call the progress_changed function when progress changes */
  70. wkhtmltopdf_set_progress_changed_callback(c, progress_changed);
  71. /* Call the phase _changed function when the phase changes */
  72. wkhtmltopdf_set_phase_changed_callback(c, phase_changed);
  73. /* Call the error function when an error occurs */
  74. wkhtmltopdf_set_error_callback(c, error);
  75. /* Call the warning function when a warning is issued */
  76. wkhtmltopdf_set_warning_callback(c, warning);
  77. /*
  78. * Add the the settings object describing the qstring documentation page
  79. * to the list of pages to convert. Objects are converted in the order in which
  80. * they are added
  81. */
  82. wkhtmltopdf_add_object(c, os, NULL);
  83. /* Perform the actual conversion */
  84. if (!wkhtmltopdf_convert(c))
  85. fprintf(stderr, "Conversion failed!");
  86. /* Output possible http error code encountered */
  87. printf("httpErrorCode: %d\n", wkhtmltopdf_http_error_code(c));
  88. /* Destroy the converter object since we are done with it */
  89. wkhtmltopdf_destroy_converter(c);
  90. /* We will no longer be needing wkhtmltopdf funcionality */
  91. wkhtmltopdf_deinit();
  92. return 0;
  93. }