Пример #1
0
        private byte[] _Convert(string inputHtml)
        {
            var converter = IntPtr.Zero;

            _errorCallback    = OnError;
            _warnCallback     = OnWarning;
            _phaseCallback    = OnPhaseChanged;
            _progressCallback = OnProgressChanged;
            _finishCallback   = OnFinished;

            try
            {
                var gSettings = _BuildGlobalSettings();
                var oSettings = _BuildObjectsettings();

                converter = _BuildConverter(gSettings, oSettings, inputHtml);

                _errorString = new StringBuilder();

                NativeCalls.wkhtmltopdf_set_error_callback(converter, _errorCallback);
                NativeCalls.wkhtmltopdf_set_warning_callback(converter, _warnCallback);
                NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, _phaseCallback);
                NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, _progressCallback);
                NativeCalls.wkhtmltopdf_set_finished_callback(converter, _finishCallback);

                OnBegin(NativeCalls.wkhtmltopdf_phase_count(converter));

                if (NativeCalls.wkhtmltopdf_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.wkhtmltopdf_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.wkhtmltopdf_set_error_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_warning_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_finished_callback(converter, null);
                    NativeCalls.wkhtmltopdf_destroy_converter(converter);
                    converter = IntPtr.Zero;
                }
            }
        }
Пример #2
0
        private byte[] _Convert(string inputHtml)
        {
            var converter  = IntPtr.Zero;
            var errorCb    = new NativeCalls.wkhtmltopdf_str_callback(OnError);
            var warnCb     = new NativeCalls.wkhtmltopdf_str_callback(OnWarning);
            var phaseCb    = new NativeCalls.wkhtmltopdf_void_callback(OnPhaseChanged);
            var progressCb = new NativeCalls.wkhtmltopdf_int_callback(OnProgressChanged);
            var finishCb   = new NativeCalls.wkhtmltopdf_bool_callback(OnFinished);

            try
            {
                var gSettings = _BuildGlobalSettings();
                var oSettings = _BuildObjectsettings();

                converter = _BuildConverter(gSettings, oSettings, inputHtml);

                _errorString = new StringBuilder();

                NativeCalls.wkhtmltopdf_set_error_callback(converter, errorCb);
                NativeCalls.wkhtmltopdf_set_warning_callback(converter, warnCb);
                NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, phaseCb);
                NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, progressCb);
                NativeCalls.wkhtmltopdf_set_finished_callback(converter, finishCb);

                OnBegin(NativeCalls.wkhtmltopdf_phase_count(converter));

                if (NativeCalls.wkhtmltopdf_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.wkhtmltopdf_get_output(converter, out tmp);
                var    output = new byte[ret.ToInt32()];
                Marshal.Copy(tmp, output, 0, output.Length);

                return(output);
            }
            catch
            {
                // Dispose un-managed resources..
                try
                {
                    NativeCalls.wkhtmltopdf_deinit();
                }
                catch (DllNotFoundException)
                {
                    // We may not be initialized yet
                }
                throw;
            }
            finally
            {
                if (converter != IntPtr.Zero)
                {
                    NativeCalls.wkhtmltopdf_set_error_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_warning_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, null);
                    NativeCalls.wkhtmltopdf_set_finished_callback(converter, null);
                    NativeCalls.wkhtmltopdf_destroy_converter(converter);
                    converter = IntPtr.Zero;
                }
            }
        }
		private byte[] _Convert(string inputHtml)
		{
			var converter = IntPtr.Zero;
			var errorCb = new NativeCalls.wkhtmltopdf_str_callback(OnError);
			var warnCb = new NativeCalls.wkhtmltopdf_str_callback(OnWarning);
			var phaseCb = new NativeCalls.wkhtmltopdf_void_callback(OnPhaseChanged);
			var progressCb = new NativeCalls.wkhtmltopdf_int_callback(OnProgressChanged);
			var finishCb = new NativeCalls.wkhtmltopdf_bool_callback(OnFinished);

			try
			{
				var gSettings = _BuildGlobalSettings();
				var oSettings = _BuildObjectsettings();

				converter = _BuildConverter(gSettings, oSettings, inputHtml);

				_errorString = new StringBuilder();

				NativeCalls.wkhtmltopdf_set_error_callback(converter, errorCb);
				NativeCalls.wkhtmltopdf_set_warning_callback(converter, warnCb);
				NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, phaseCb);
				NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, progressCb);
				NativeCalls.wkhtmltopdf_set_finished_callback(converter, finishCb);

				OnBegin(NativeCalls.wkhtmltopdf_phase_count(converter));

				if (NativeCalls.wkhtmltopdf_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.wkhtmltopdf_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.wkhtmltopdf_set_error_callback(converter, null);
					NativeCalls.wkhtmltopdf_set_warning_callback(converter, null);
					NativeCalls.wkhtmltopdf_set_phase_changed_callback(converter, null);
					NativeCalls.wkhtmltopdf_set_progress_changed_callback(converter, null);
					NativeCalls.wkhtmltopdf_set_finished_callback(converter, null);
					NativeCalls.wkhtmltopdf_destroy_converter(converter);
					converter = IntPtr.Zero;
				}
			}
		}