コード例 #1
0
        private byte[] _Convert()
        {
            var converter  = IntPtr.Zero;
            var errorCb    = new NativeCalls.wkhtmltoimage_str_callback(OnError);
            var warnCb     = new NativeCalls.wkhtmltoimage_str_callback(OnWarning);
            var phaseCb    = new NativeCalls.wkhtmltoimage_void_callback(OnPhaseChanged);
            var progressCb = new NativeCalls.wkhtmltoimage_int_callback(OnProgressChanged);
            var finishCb   = new NativeCalls.wkhtmltoimage_bool_callback(OnFinished);

            try
            {
                var gSettings = _BuildGlobalSettings();

                converter = _BuildConverter(gSettings);

                _errorString = new StringBuilder();

                NativeCalls.wkhtmltoimage_set_error_callback(converter, errorCb);
                NativeCalls.wkhtmltoimage_set_warning_callback(converter, warnCb);
                NativeCalls.wkhtmltoimage_set_phase_changed_callback(converter, phaseCb);
                NativeCalls.wkhtmltoimage_set_progress_changed_callback(converter, progressCb);
                NativeCalls.wkhtmltoimage_set_finished_callback(converter, finishCb);

                OnBegin(NativeCalls.wkhtmltoimage_phase_count(converter));

                if (NativeCalls.wkhtmltoimage_convert(converter) == 0)
                {
                    var msg = string.Format("HtmlToPdf conversion failed: {0}", _errorString.ToString());
                    throw new ConverterException(msg);
                }

                if (!string.IsNullOrEmpty(GlobalSettings.Out))
                {
                    return(null);
                }

                _Log.Debug("CONVERSION DONE.. getting output.");

                // Get output from internal buffer..

                IntPtr tmp    = IntPtr.Zero;
                var    ret    = NativeCalls.wkhtmltoimage_get_output(converter, out tmp);
                var    output = new byte[ret.ToInt32()];
                Marshal.Copy(tmp, output, 0, output.Length);

                return(output);
            }
            finally
            {
                if (converter != IntPtr.Zero)
                {
                    NativeCalls.wkhtmltoimage_set_error_callback(converter, null);
                    NativeCalls.wkhtmltoimage_set_warning_callback(converter, null);
                    NativeCalls.wkhtmltoimage_set_phase_changed_callback(converter, null);
                    NativeCalls.wkhtmltoimage_set_progress_changed_callback(converter, null);
                    NativeCalls.wkhtmltoimage_set_finished_callback(converter, null);
                    NativeCalls.wkhtmltoimage_destroy_converter(converter);
                    converter = IntPtr.Zero;
                }
            }
        }