source: wkhtmltox/trunk/fuentes/examples/image_c_api.c @ 51

Last change on this file since 51 was 51, checked in by mabarracus, 4 years ago

wip

File size: 3.4 KB
Line 
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
22/* This is a simple example program showing how to use the wkhtmltopdf c bindings */
23#include <stdbool.h>
24#include <stdio.h>
25#include <wkhtmltox/image.h>
26
27/* Print out loading progress information */
28void progress_changed(wkhtmltoimage_converter * c, int p) {
29        printf("%3d%%\r",p);
30        fflush(stdout);
31}
32
33/* Print loading phase information */
34void phase_changed(wkhtmltoimage_converter * c) {
35        int phase = wkhtmltoimage_current_phase(c);
36        printf("%s\n", wkhtmltoimage_phase_description(c, phase));
37}
38
39/* Print a message to stderr when an error occures */
40void error(wkhtmltoimage_converter * c, const char * msg) {
41        fprintf(stderr, "Error: %s\n", msg);
42}
43
44/* Print a message to stderr when a warning is issued */
45void warning(wkhtmltoimage_converter * c, const char * msg) {
46        fprintf(stderr, "Warning: %s\n", msg);
47}
48
49/* Main method convert image */
50int main() {
51        wkhtmltoimage_global_settings * gs;
52        wkhtmltoimage_converter * c;
53        const unsigned char * data;
54        long len;
55
56        /* Init wkhtmltoimage in graphics less mode */
57        wkhtmltoimage_init(false);
58
59        /*
60         * Create a global settings object used to store options that are not
61         * related to input objects, note that control of this object is parsed to
62         * the converter later, which is then responsible for freeing it
63         */
64        gs = wkhtmltoimage_create_global_settings();
65
66        /* We want to convert to convert the qstring documentation page */
67        wkhtmltoimage_set_global_setting(gs, "in", "http://www.google.com/");
68        wkhtmltoimage_set_global_setting(gs, "fmt", "jpeg");
69
70        /* Create the actual converter object used to convert the pages */
71        c = wkhtmltoimage_create_converter(gs, NULL);
72
73        /* Call the progress_changed function when progress changes */
74        wkhtmltoimage_set_progress_changed_callback(c, progress_changed);
75
76        /* Call the phase _changed function when the phase changes */
77        wkhtmltoimage_set_phase_changed_callback(c, phase_changed);
78
79        /* Call the error function when an error occures */
80        wkhtmltoimage_set_error_callback(c, error);
81
82        /* Call the waring function when a warning is issued */
83        wkhtmltoimage_set_warning_callback(c, warning);
84
85        /* Perform the actual convertion */
86        if (!wkhtmltoimage_convert(c))
87                fprintf(stderr, "Convertion failed!");
88
89        /* Output possible http error code encountered */
90        printf("httpErrorCode: %d\n", wkhtmltoimage_http_error_code(c));
91
92        len = wkhtmltoimage_get_output(c, &data);
93        printf("%ld len\n", len);
94
95
96        /* Destroy the converter object since we are done with it */
97        wkhtmltoimage_destroy_converter(c);
98
99        /* We will no longer be needing wkhtmltoimage funcionality */
100        wkhtmltoimage_deinit();
101
102        return 0;
103}
Note: See TracBrowser for help on using the repository browser.