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

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

wip

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