-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
191 lines (184 loc) · 4.94 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib.h>
#include "wkgtkprinter.h"
void print_help()
{
printf("usage: wkgtkprinter [-i <input_uri>] [-b <base_uri>] [-k <key_file>] [-s <default_css>] -o <out_uri>\n");
printf("html string should be provided through stdin. (if input_uri was not provided.)\n");
printf("Pdf result must be written to file, no option for outputing to stdout. (but you may write to a pipe file.)\n");
printf("Options:\n"
"-i <uri> [Input URI] Supply input uri for the html source (instead of stdin)\n"
" eg. file:///home/user/in.html or https://webkit.org ...\n"
"-o <uri> [Output URI] Supply output uri for the pdf output (mandatory) \n"
" eg. file:///home/user/x.pdf\n"
"-b <uri> [Base URI] When you supply html string, you must provide\n"
" a base uri where all your external resources (images, css, etc) sits in\n"
" (similar to the <base> tag in html). You must provide it (and in uri format)\n"
" even if your html string do not contain any external reference.\n"
"-k <file> [Key file] key file contains settings for the pdf printer.\n"
" a typical key file should look like:\n"
"\n"
" ~~~~~~~~~~~~~~~~~~~\\n"
" [Print Settings]\n"
" quality=high\n"
" resolution-x=320\n"
" resolution-y=320\n"
" resolution=320\n"
" output-file-format=pdf <--- mandatory\n"
" printer=Print to File <--- mandatory\n"
" page-set=all\n"
"\n"
" [Page Setup]\n"
" PPDName=A4\n"
" DisplayName=A4\n"
" Width=210\n"
" Height=297\n"
" MarginTop=6.35\n"
" MarginBottom=14.224\n"
" MarginLeft=6.35\n"
" MarginRight=6.35\n"
" Orientation=landscape|portrait\n"
" ~~~~~~~~~~~~~~~~~~~\n"
"-s <file> [Default stylesheet file] in css format, optional.\n"
"-h print this help and exits.\n");
}
int read_file(char **bufptr, FILE *stream)
{
#define CHUNK_SIZE 4096
*bufptr = malloc(CHUNK_SIZE+1);
ssize_t n;
size_t buflen = 0;
size_t bufoff = 0;
while ((n = fread(*bufptr+buflen,1,CHUNK_SIZE,stdin)))
{
buflen += n;
if (n == CHUNK_SIZE)
{
*bufptr=realloc(*bufptr,buflen+CHUNK_SIZE+1);
}
else
break;
}
(*bufptr)[buflen] = '\0';
if (ferror(stdin))
{
return -1;
}
*bufptr=realloc(*bufptr,buflen+1);
return buflen;
}
int main(int argc, char ** argv)
{
const char *in_uri = NULL;
const char *base_uri = NULL; // base path for html resources
const char *out_uri = NULL;
const char *key_file = NULL;
const char *stylesheet_file = NULL;
char *key_string = NULL;
char *stylesheet_string = NULL;
char *html_txt = NULL;
int index;
int c;
while ((c = getopt (argc, argv, "i:o:k:b:s:h")) != -1)
{
switch (c)
{
case 'i':
in_uri = optarg;
break;
case 'b':
base_uri = optarg;
break;
case 'o':
out_uri = optarg;
break;
case 'k':
key_file = optarg;
break;
case 's':
stylesheet_file = optarg;
break;
case 'h':
print_help();
return 0;
case '?':
if (optopt == 'i' || optopt == 'o' ||optopt == 'k' ||optopt == 'b' || optopt == 's' )
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
return -1;
}
}
if (key_file != NULL)
{
if (!g_file_get_contents (
key_file,
&key_string,
NULL,
NULL))
{
fprintf (stderr,
"Could not read key file successfully (%s)\n", key_file);
return -1;
}
}
if (stylesheet_file != NULL)
{
if (!g_file_get_contents (
stylesheet_file,
&stylesheet_string,
NULL,
NULL))
{
fprintf (stderr,
"Could not read stylesheet file successfully\n");
return -1;
}
}
if (out_uri == NULL)
{
fprintf (stderr,
"Missing [Output URI] argument. Add '-o <output-uri>' to the arguments\n");
return -1;
}
if (in_uri == NULL)
{
if (base_uri == NULL)
{
fprintf (stderr,
"Missing [Base URI] argument\n");
return -1;
}
size_t len = 0;
ssize_t bytes_read = read_file( &html_txt, stdin);
if ( bytes_read < 0) {
fprintf (stderr,
"Could not read html content from stdin\n");
return -1;
}
}
wkgtkprinter_gtk_init();
wkgtkprinter_html2pdf(in_uri, html_txt, base_uri, out_uri, key_string, stylesheet_string);
if (key_string != NULL)
{
g_free(key_string);
}
if (stylesheet_string != NULL)
{
g_free(stylesheet_string);
}
if (html_txt != NULL)
{
free(html_txt);
}
return 0;
}