wkhtml 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ *.bundle
4
+ *.so
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ bin
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,17 @@
1
+ sudo: false
2
+ os:
3
+ - linux
4
+ #addons:
5
+ # apt:
6
+ # packages:
7
+ # - wkhtmltopdf
8
+ before_install:
9
+ - ./.travis_setup.sh
10
+ language: ruby
11
+ rvm:
12
+ - 1.9.3
13
+ - 2.0
14
+ - 2.1
15
+ - 2.2
16
+ - 2.3.0
17
+ #- ruby-head
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+
3
+ wget -q http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz -O /tmp/wkhtmltox.tar.xz
4
+ tar -xf /tmp/wkhtmltox.tar.xz -C /tmp
5
+ echo "wkhtmltopdf: `/tmp/wkhtmltox/bin/wkhtmltopdf -V`"
6
+ echo "wkhtmltoimage: `/tmp/wkhtmltox/bin/wkhtmltoimage -V`"
7
+
8
+ bundle install --jobs=3 --retry=3
9
+ bundle exec rake compile -- --with-wkhtmltox-dir=/tmp/wkhtmltox --with-wkhtmltox-lib=/tmp/wkhtmltox/lib
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wkhtml.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Carson Reinke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,52 @@
1
+ ## WkHtml
2
+ [![Build Status](https://travis-ci.org/carsonreinke/wkhtml.svg?branch=master)](https://travis-ci.org/carsonreinke/wkhtml)
3
+
4
+ Ruby bindings for [wkhtmltox (wkhtmltopdf)](http://wkhtmltopdf.org/). This gem should be considered experimental (see Todo).
5
+
6
+ ### Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'wkhtml'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install wkhtml
19
+
20
+ And if it can't find wkhtmltox/wkhtmltopdf, you can supply `--with-wkhtmltox-dir`, `--with-wkhtmltox-include`, or `--with-wkhtmltox-lib` for location variations.
21
+
22
+ To use a windowing system for rendering, can be enabled with option `--enable-use-graphics`. For more information, [see `GUIEnabled` portion of Qt QApplication](http://doc.qt.io/qt-4.8/qapplication.html#QApplication-2).
23
+
24
+ ### Usage
25
+
26
+ ```ruby
27
+ #PDF
28
+ WkHtml::Converter.new('http://example.com/').to_pdf()
29
+ #JPEG
30
+ WkHtml::Converter.new('http://example.com/').to_jpg()
31
+ #PNG
32
+ WkHtml::Converter.new('http://example.com/').to_png()
33
+ #SVG
34
+ WkHtml::Converter.new('http://example.com/').to_svg()
35
+ #File
36
+ WkHtml::Converter.new('http://example.com/').to_file('/path', :pdf)
37
+ ```
38
+
39
+ The `#new` will take either a `URI`, HTML content, or a `File`.
40
+
41
+ Secondary argument takes a `Hash` of options, a list of these options can be found [here](http://wkhtmltopdf.org/libwkhtmltox/pagesettings.html).
42
+
43
+ ```ruby
44
+ WkHtml::Converter.new('http://', {:useCompression => false})
45
+ ```
46
+
47
+ ### Todo
48
+
49
+ * Memory leak with any image conversion :disappointed: ([see](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2700))
50
+ * Due to wkhtmltopdf C api limitation, seg fault when try to use library from fork :disappointed:
51
+ * Due to wkhtmltopdf C api limitation, must be used within main Ruby VM thread :disappointed:
52
+ * Some [settings](http://wkhtmltopdf.org/libwkhtmltox/pagesettings.html) for image generation just do not work :sob: ([see](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2802))
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/extensiontask'
3
+ require 'rspec/core/rake_task'
4
+
5
+ Rake::ExtensionTask.new('wkhtml_native', Gem::Specification.load('wkhtml.gemspec')) do |ext|
6
+ ext.ext_dir = 'ext/wkhtml'
7
+ ext.lib_dir = 'lib/wkhtml'
8
+ end
9
+
10
+ task :console => [:compile, :build] do
11
+ exec 'irb -r wkhtml -I ./lib'
12
+ end
13
+
14
+ RSpec::Core::RakeTask.new(:spec)
15
+ task :spec => [:compile, :build]
16
+
17
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ require 'mkmf'
2
+
3
+ #Copied from ruby-filemagic (https://github.com/blackwinter/ruby-filemagic/blob/master/ext/filemagic/extconf.rb)
4
+ HEADER_DIRS = [
5
+ '/opt/local/include', # MacPorts
6
+ '/usr/local/include', # compiled from source and Homebrew
7
+ '/usr/include' # system
8
+ ]
9
+ LIB_DIRS = [
10
+ '/opt/local/lib', # MacPorts
11
+ '/usr/local/lib', # compiled from source and Homebrew
12
+ '/usr/lib' # system
13
+ ]
14
+ dir_config('wkhtmltox', HEADER_DIRS, LIB_DIRS)
15
+
16
+ unless have_library('wkhtmltox')
17
+ abort('Missing wkhtmltox library')
18
+ end
19
+
20
+ unless have_header('wkhtmltox/pdf.h')
21
+ abort('Missing wkhtmltox pdf.h')
22
+ end
23
+
24
+ unless have_header('wkhtmltox/image.h')
25
+ warn('Missing wkhtmltox image.h')
26
+ end
27
+
28
+ #--enable-use-graphics/--disable-use-graphics
29
+ if enable_config('use-graphics')
30
+ $defs.push('-DUSE_GRAPHICS')
31
+ end
32
+
33
+ create_makefile('wkhtml/wkhtml_native')
@@ -0,0 +1,388 @@
1
+ #include "wkhtml.h"
2
+
3
+ #define BUFFER_SIZE 10240
4
+
5
+ //init functions take parameter use_graphics which can be set using --enable-use-graphics
6
+ #ifdef USE_GRAPHICS
7
+ #define USE_GRAPHICS_INT 1
8
+ #else
9
+ #define USE_GRAPHICS_INT 0
10
+ #endif
11
+
12
+ #define INT2BOOL(v) ((int)v) ? Qtrue : Qfalse
13
+ #define BOOL2INT(v) ((VALUE)v) ? 1 : 0
14
+ #define ENCODE_UTF8(v) rb_str_encode(v, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil)
15
+
16
+ void Init_wkhtml_native() {
17
+ //Global initialization of library and when Ruby shuts down
18
+ wkhtmltopdf_init(USE_GRAPHICS_INT);
19
+ wkhtmltoimage_init(USE_GRAPHICS_INT);
20
+ rb_set_end_proc(Deinit_wkhtml_native, Qnil);
21
+
22
+ idReady = rb_intern("ready");
23
+
24
+ mWkHtml = rb_define_module("WkHtml");
25
+ rb_define_const(mWkHtml, "LIBRARY_VERSION", rb_obj_freeze(rb_str_new_cstr(wkhtmltopdf_version())));
26
+ rb_define_const(mWkHtml, "USE_GRAPHICS", INT2BOOL(USE_GRAPHICS_INT));
27
+
28
+ mWkHtmlToPdf = rb_define_module_under(mWkHtml, "ToPdf");
29
+
30
+ cWkHtmlToPdfGlobalSettings = rb_define_class_under(mWkHtmlToPdf, "GlobalSettings", rb_cObject);
31
+ rb_define_alloc_func(cWkHtmlToPdfGlobalSettings, wkhtml_topdf_globalsettings_alloc);
32
+ rb_define_method(cWkHtmlToPdfGlobalSettings, "[]=", wkhtml_topdf_globalsettings_aset, 2);
33
+ rb_define_method(cWkHtmlToPdfGlobalSettings, "[]", wkhtml_topdf_globalsettings_aref, 1);
34
+
35
+ cWkHtmlToPdfObjectSettings = rb_define_class_under(mWkHtmlToPdf, "ObjectSettings", rb_cObject);
36
+ rb_define_alloc_func(cWkHtmlToPdfObjectSettings, wkhtml_topdf_objectsettings_alloc);
37
+ rb_define_method(cWkHtmlToPdfObjectSettings, "[]=", wkhtml_topdf_objectsettings_aset, 2);
38
+ rb_define_method(cWkHtmlToPdfObjectSettings, "[]", wkhtml_topdf_objectsettings_aref, 1);
39
+
40
+ cWkHtmlToPdfConverter = rb_define_class_under(mWkHtmlToPdf, "Converter", rb_cObject);
41
+ /*
42
+ TODO
43
+ rb_define_singleton_method(klass, "new", constructor, 1); //Uses Data_Wrap_Struct -> rb_obj_call_init(t_data, 1, argv);
44
+ rb_define_method(klass, "initialize", initialize, 1);
45
+ */
46
+ rb_define_singleton_method(cWkHtmlToPdfConverter, "create", wkhtml_topdf_converter_create, 1);
47
+ rb_define_method(cWkHtmlToPdfConverter, "add_object", wkhtml_topdf_converter_add_object, 2);
48
+ rb_define_method(cWkHtmlToPdfConverter, "convert", wkhtml_topdf_converter_convert, 0);
49
+ rb_define_method(cWkHtmlToPdfConverter, "http_error_code", wkhtml_topdf_converter_http_error_code, 0);
50
+ rb_define_method(cWkHtmlToPdfConverter, "get_output", wkhtml_topdf_converter_get_output, 0);
51
+ //Force use of factory method
52
+ rb_undef_alloc_func(cWkHtmlToPdfConverter);
53
+ rb_undef_method(rb_singleton_class(cWkHtmlToPdfConverter), "new");
54
+
55
+ mWkHtmlToImage = rb_define_module_under(mWkHtml, "ToImage");
56
+
57
+ cWkHtmlToImageGlobalSettings = rb_define_class_under(mWkHtmlToImage, "GlobalSettings", rb_cObject);
58
+ rb_define_alloc_func(cWkHtmlToImageGlobalSettings, wkhtml_toimage_globalsettings_alloc);
59
+ rb_define_method(cWkHtmlToImageGlobalSettings, "[]=", wkhtml_toimage_globalsettings_aset, 2);
60
+ rb_define_method(cWkHtmlToImageGlobalSettings, "[]", wkhtml_toimage_globalsettings_aref, 1);
61
+
62
+ cWkHtmlToImageConverter = rb_define_class_under(mWkHtmlToImage, "Converter", rb_cObject);
63
+ rb_define_singleton_method(cWkHtmlToImageConverter, "create", wkhtml_toimage_converter_create, 2);
64
+ rb_define_method(cWkHtmlToImageConverter, "convert", wkhtml_toimage_converter_convert, 0);
65
+ rb_define_method(cWkHtmlToImageConverter, "http_error_code", wkhtml_toimage_converter_http_error_code, 0);
66
+ rb_define_method(cWkHtmlToImageConverter, "get_output", wkhtml_toimage_converter_get_output, 0);
67
+ //Force use of factory method
68
+ rb_undef_alloc_func(cWkHtmlToImageConverter);
69
+ rb_undef_method(rb_singleton_class(cWkHtmlToImageConverter), "new");
70
+ }
71
+
72
+ void Deinit_wkhtml_native(VALUE data) {
73
+ wkhtmltopdf_deinit();
74
+ wkhtmltoimage_deinit();
75
+ }
76
+
77
+
78
+ #define _wkhtml_setting_aset(setting_type, setting_func) ({ \
79
+ setting_type* settings;\
80
+ char* key_cstr; \
81
+ \
82
+ rb_check_frozen(self); \
83
+ \
84
+ key = ENCODE_UTF8(rb_obj_as_string(key)); \
85
+ val = ENCODE_UTF8(rb_obj_as_string(val)); \
86
+ \
87
+ Data_Get_Struct(self, setting_type, settings); \
88
+ \
89
+ key_cstr = StringValueCStr(key); \
90
+ if( setting_func(settings, key_cstr, StringValueCStr(val)) ) { \
91
+ return val; \
92
+ } \
93
+ \
94
+ rb_raise(rb_eArgError, "Unable to set: %s", key_cstr); \
95
+ })
96
+
97
+ #define _wkhtml_setting_aref(setting_type, setting_func) ({ \
98
+ char* key_cstr; \
99
+ char* val_cstr; \
100
+ VALUE val; \
101
+ int result; \
102
+ setting_type* settings; \
103
+ \
104
+ key = ENCODE_UTF8(rb_obj_as_string(key)); \
105
+ \
106
+ Data_Get_Struct(self, setting_type, settings); \
107
+ \
108
+ key_cstr = StringValueCStr(key); \
109
+ val_cstr = ALLOC_N(char, BUFFER_SIZE); \
110
+ val = Qnil; \
111
+ result = setting_func(settings, key_cstr, val_cstr, BUFFER_SIZE); \
112
+ \
113
+ if(result) { \
114
+ val = rb_str_new_cstr(val_cstr); \
115
+ } \
116
+ xfree(val_cstr); \
117
+ \
118
+ if(val == Qnil) { \
119
+ rb_raise(rb_eArgError, "Unable to get: %s", key_cstr); \
120
+ } \
121
+ \
122
+ return val; \
123
+ })
124
+
125
+ /*
126
+ * WkHtml::ToPdf::GlobalSettings
127
+ */
128
+ VALUE wkhtml_topdf_globalsettings_alloc(VALUE self) {
129
+ wkhtmltopdf_global_settings* settings = wkhtmltopdf_create_global_settings();
130
+ return Data_Wrap_Struct(self, NULL, wkhtmltopdf_destroy_global_settings, settings);
131
+ }
132
+ VALUE wkhtml_topdf_globalsettings_aset(VALUE self, VALUE key, VALUE val) {
133
+ _wkhtml_setting_aset(wkhtmltopdf_global_settings, wkhtmltopdf_set_global_setting);
134
+ }
135
+ VALUE wkhtml_topdf_globalsettings_aref(VALUE self, VALUE key) {
136
+ _wkhtml_setting_aref(wkhtmltopdf_global_settings, wkhtmltopdf_get_global_setting);
137
+ }
138
+
139
+ /*
140
+ * WkHtml::ToPdf::ObjectSettings
141
+ */
142
+ VALUE wkhtml_topdf_objectsettings_alloc(VALUE self) {
143
+ wkhtmltopdf_object_settings* settings = wkhtmltopdf_create_object_settings();
144
+ return Data_Wrap_Struct(self, NULL, wkhtmltopdf_destroy_object_settings, settings);
145
+ }
146
+ VALUE wkhtml_topdf_objectsettings_aset(VALUE self, VALUE key, VALUE val) {
147
+ _wkhtml_setting_aset(wkhtmltopdf_object_settings, wkhtmltopdf_set_object_setting);
148
+ }
149
+ VALUE wkhtml_topdf_objectsettings_aref(VALUE self, VALUE key) {
150
+ _wkhtml_setting_aref(wkhtmltopdf_object_settings, wkhtmltopdf_get_object_setting);
151
+ }
152
+
153
+ /*
154
+ * WkHtml::ToPdf::Converter
155
+ */
156
+ void wkhtml_topdf_converter_free(wkhtmltopdf_converter* converter) {
157
+ wkhtmltopdf_destroy_converter(converter);
158
+
159
+ //Do this on Ruby exit
160
+ ////Deinitialize library after destroying (will only if last call)
161
+ //wkhtmltopdf_deinit();
162
+ }
163
+
164
+ VALUE wkhtml_topdf_converter_create(VALUE self, VALUE settings) {
165
+ wkhtmltopdf_global_settings* global_settings;
166
+ wkhtmltopdf_converter* converter;
167
+
168
+ if(rb_obj_is_kind_of(settings, cWkHtmlToPdfGlobalSettings) == Qfalse) {
169
+ rb_raise(rb_eArgError, "Wrong argument type, must be a GlobalSettings");
170
+ }
171
+
172
+ Data_Get_Struct(settings, wkhtmltopdf_global_settings, global_settings);
173
+
174
+ //Initialize on library load
175
+ ////Initialize library before creating (will only if first call)
176
+ //wkhtmltopdf_init(USE_GRAPHICS_INT);
177
+
178
+ converter = wkhtmltopdf_create_converter(global_settings);
179
+
180
+ OBJ_FREEZE(settings);
181
+
182
+ //return Data_Wrap_Struct(self, NULL, wkhtmltopdf_destroy_converter, converter);
183
+ return Data_Wrap_Struct(self, NULL, wkhtml_topdf_converter_free, converter);
184
+ }
185
+
186
+ VALUE wkhtml_topdf_converter_add_object(VALUE self, VALUE settings, VALUE data) {
187
+ wkhtmltopdf_converter* converter;
188
+ wkhtmltopdf_object_settings* object_settings;
189
+ char* data_cstr = NULL;
190
+
191
+ if(rb_obj_is_kind_of(settings, cWkHtmlToPdfObjectSettings) == Qfalse) {
192
+ rb_raise(rb_eArgError, "Wrong argument type, must be a ObjectSettings");
193
+ }
194
+
195
+ rb_check_frozen(self);
196
+
197
+ if(!NIL_P(data)) {
198
+ Check_Type(data, T_STRING);
199
+ data_cstr = StringValueCStr(data);
200
+ }
201
+
202
+ Data_Get_Struct(settings, wkhtmltopdf_object_settings, object_settings);
203
+ Data_Get_Struct(self, wkhtmltopdf_converter, converter);
204
+
205
+ wkhtmltopdf_add_object(converter, object_settings, data_cstr);
206
+
207
+ //From wkhtmltox:
208
+ //"Once the object has been added, the supplied settings may no longer be accessed, it Wit eventually be freed by wkhtmltopdf."
209
+ OBJ_FREEZE(settings);
210
+
211
+ rb_ivar_set(self, idReady, Qtrue);
212
+
213
+ return data;
214
+ }
215
+
216
+ VALUE wkhtml_topdf_converter_convert(VALUE self) {
217
+ wkhtmltopdf_converter* converter;
218
+
219
+ //TODO QApplication is initialized on whatever thread `wkhtmltopdf_init` is called, otherwise events are
220
+ //not sent properly
221
+ if(rb_thread_main() != rb_thread_current()) {
222
+ rb_raise(rb_eRuntimeError, "Yuck! You must be on the main thread for wkhtmltopdf to work");
223
+ }
224
+
225
+ //Checks
226
+ if(rb_ivar_get(self, idReady) != Qtrue) rb_raise(rb_eRuntimeError, "Object must be added first");
227
+ rb_check_frozen(self);
228
+
229
+ Data_Get_Struct(self, wkhtmltopdf_converter, converter);
230
+
231
+ if(wkhtmltopdf_convert(converter)) {
232
+ OBJ_FREEZE(self);
233
+
234
+ return Qtrue;
235
+ }
236
+ else {
237
+ return Qfalse;
238
+ }
239
+ }
240
+
241
+ VALUE wkhtml_topdf_converter_http_error_code(VALUE self) {
242
+ wkhtmltopdf_converter* converter;
243
+ int http_error_code;
244
+
245
+ Data_Get_Struct(self, wkhtmltopdf_converter, converter);
246
+
247
+ http_error_code = wkhtmltopdf_http_error_code(converter);
248
+ return http_error_code == 0 ? Qnil : INT2NUM(http_error_code); //0 is treated as success and should be nil
249
+ }
250
+
251
+ VALUE wkhtml_topdf_converter_get_output(VALUE self) {
252
+ wkhtmltopdf_converter* converter;
253
+ const unsigned char* data_cstr;
254
+ long length;
255
+ VALUE data;
256
+
257
+ Data_Get_Struct(self, wkhtmltopdf_converter, converter);
258
+
259
+ length = wkhtmltopdf_get_output(converter, &data_cstr);
260
+
261
+ data = rb_str_new((char*)data_cstr, length);
262
+ rb_enc_associate(data, rb_ascii8bit_encoding());
263
+
264
+ return data;
265
+ }
266
+
267
+ //CAPI(void) wkhtmltopdf_set_warning_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb)
268
+ //CAPI(void) wkhtmltopdf_set_error_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_str_callback cb)
269
+ //CAPI(void) wkhtmltopdf_set_phase_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_void_callback cb)
270
+ //CAPI(void) wkhtmltopdf_set_progress_changed_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb)
271
+ //CAPI(void) wkhtmltopdf_set_finished_callback(wkhtmltopdf_converter * converter, wkhtmltopdf_int_callback cb)
272
+ //CAPI(int) wkhtmltopdf_current_phase(wkhtmltopdf_converter * converter)
273
+ //CAPI(int) wkhtmltopdf_phase_count(wkhtmltopdf_converter * converter)
274
+
275
+ /*
276
+ * WkHtml::ToImage::GlobalSettings
277
+ */
278
+ void wkhtml_toimage_globalsettings_free(wkhtmltoimage_global_settings* settings) {
279
+ //No free function as wkhtmltopdf claims to manage this
280
+ }
281
+ VALUE wkhtml_toimage_globalsettings_alloc(VALUE self) {
282
+ wkhtmltoimage_global_settings* settings = wkhtmltoimage_create_global_settings();
283
+ return Data_Wrap_Struct(self, NULL, wkhtml_toimage_globalsettings_free, settings);
284
+ }
285
+ VALUE wkhtml_toimage_globalsettings_aset(VALUE self, VALUE key, VALUE val) {
286
+ _wkhtml_setting_aset(wkhtmltoimage_global_settings, wkhtmltoimage_set_global_setting);
287
+ }
288
+ VALUE wkhtml_toimage_globalsettings_aref(VALUE self, VALUE key) {
289
+ _wkhtml_setting_aref(wkhtmltoimage_global_settings, wkhtmltoimage_get_global_setting);
290
+ }
291
+
292
+ /*
293
+ * WkHtml::ToImage::Converter
294
+ */
295
+ void wkhtml_toimage_converter_free(wkhtmltoimage_converter* converter) {
296
+ wkhtmltoimage_destroy_converter(converter);
297
+
298
+ //Do this on Ruby exit
299
+ ////Deinitialize library after destroying (will only if last call)
300
+ //wkhtmltoimage_deinit();
301
+ }
302
+
303
+ VALUE wkhtml_toimage_converter_create(VALUE self, VALUE settings, VALUE data) {
304
+ wkhtmltoimage_global_settings* global_settings;
305
+ wkhtmltoimage_converter* converter;
306
+ char* data_cstr = NULL;
307
+
308
+ if(rb_obj_is_kind_of(settings, cWkHtmlToImageGlobalSettings) == Qfalse) {
309
+ rb_raise(rb_eArgError, "Wrong argument type, must be a GlobalSettings");
310
+ }
311
+ rb_check_frozen(self);
312
+
313
+ if(!NIL_P(data)) {
314
+ Check_Type(data, T_STRING);
315
+ data_cstr = StringValueCStr(data);
316
+ }
317
+
318
+ Data_Get_Struct(settings, wkhtmltoimage_global_settings, global_settings);
319
+
320
+ //Initialize on library load
321
+ ////Initialize library before creating (will only if first call)
322
+ //wkhtmltoimage_init(USE_GRAPHICS_INT);
323
+ converter = wkhtmltoimage_create_converter(global_settings, data_cstr);
324
+
325
+ OBJ_FREEZE(settings);
326
+
327
+ return Data_Wrap_Struct(self, NULL, wkhtml_toimage_converter_free, converter);
328
+ }
329
+
330
+ VALUE wkhtml_toimage_converter_convert(VALUE self) {
331
+ wkhtmltoimage_converter* converter;
332
+
333
+ //See wkhtml_topdf_converter_convert
334
+ if(rb_thread_main() != rb_thread_current()) {
335
+ rb_raise(rb_eRuntimeError, "Yuck! You must be on the main thread for wkhtmltopdf to work");
336
+ }
337
+
338
+ //Checks
339
+ rb_check_frozen(self);
340
+
341
+ Data_Get_Struct(self, wkhtmltoimage_converter, converter);
342
+
343
+ if(wkhtmltoimage_convert(converter)) {
344
+ //Freeze converter if successful
345
+ OBJ_FREEZE(self);
346
+
347
+ return Qtrue;
348
+ }
349
+ else {
350
+ return Qfalse;
351
+ }
352
+ }
353
+
354
+ VALUE wkhtml_toimage_converter_http_error_code(VALUE self) {
355
+ wkhtmltoimage_converter* converter;
356
+ int http_error_code;
357
+
358
+ Data_Get_Struct(self, wkhtmltoimage_converter, converter);
359
+
360
+ http_error_code = wkhtmltoimage_http_error_code(converter);
361
+ return http_error_code == 0 ? Qnil : INT2NUM(http_error_code); //0 is treated as success and should be nil
362
+ }
363
+
364
+ VALUE wkhtml_toimage_converter_get_output(VALUE self) {
365
+ wkhtmltoimage_converter* converter;
366
+ const unsigned char* data_cstr;
367
+ long length;
368
+ VALUE data;
369
+
370
+ Data_Get_Struct(self, wkhtmltoimage_converter, converter);
371
+
372
+ length = wkhtmltoimage_get_output(converter, &data_cstr);
373
+
374
+ data = rb_str_new((char*)data_cstr, length);
375
+ rb_enc_associate(data, rb_ascii8bit_encoding());
376
+
377
+ return data;
378
+ }
379
+
380
+ //CAPI(void) wkhtmltoimage_set_warning_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb);
381
+ //CAPI(void) wkhtmltoimage_set_error_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_str_callback cb);
382
+ //CAPI(void) wkhtmltoimage_set_phase_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_void_callback cb);
383
+ //CAPI(void) wkhtmltoimage_set_progress_changed_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb);
384
+ //CAPI(void) wkhtmltoimage_set_finished_callback(wkhtmltoimage_converter * converter, wkhtmltoimage_int_callback cb);
385
+ //CAPI(int) wkhtmltoimage_current_phase(wkhtmltoimage_converter * converter);
386
+ //CAPI(int) wkhtmltoimage_phase_count(wkhtmltoimage_converter * converter);
387
+ //CAPI(const char *) wkhtmltoimage_phase_description(wkhtmltoimage_converter * converter, int phase);
388
+ //CAPI(const char *) wkhtmltoimage_progress_string(wkhtmltoimage_converter * converter);