Plain wrapper around wkhtmltox API library.
WARNING: Due to underlaying's API restrictions all calls to instances of this class should be made from within the same thread!! See MultiplexingConverter for an interim & transparent solution.
Inheritance: IHtmlToPdfConverter
Exemple #1
0
        public MultiplexingConverter()
        {
            lock (_worker)
            {
                // XXX: We need to keep a converter instance alive during the whole application
                //		lifetime due to some underlying's library bug by which re-initializing
                //		the API after having deinitiaized it, causes all newlly rendered pdf
                //		file to be corrupted. So we will keep this converter alive to avoid
                //		de-initialization until app's shutdown. (pruiz)
                // See: http://code.google.com/p/wkhtmltopdf/issues/detail?id=511
                if (_initiWorkAround == null)
                {
                    //_Log.InfoFormat("Initializing converter infrastructure..");
                    _worker.Invoke((Action)(() => _initiWorkAround = new WkHtmlToPdfConverter()));
                    //_Log.InfoFormat("Initialized converter infrastructure.. (workaround: {0})", _initiWorkAround != null);

                    AppDomain.CurrentDomain.ProcessExit += (o, e) =>
                                                           _worker.Invoke((Action)(() => {
                        //_Log.InfoFormat("Disposing converter infraestructure..");
                        _initiWorkAround.Dispose();
                        _initiWorkAround = null;
                        //_Log.InfoFormat("Disposed converter infraestructure..");
                    }));
                }
            }

            _worker.Invoke((Action)(() => _converter = new WkHtmlToPdfConverter()));
        }
        public MultiplexingPdfConverter()
        {
            lock (_worker)
            {
                // XXX: We need to keep a converter instance alive during the whole application
                //		lifetime due to some underlying's library bug by which re-initializing
                //		the API after having deinitiaized it, causes all newlly rendered pdf
                //		file to be corrupted. So we will keep this converter alive to avoid
                //		de-initialization until app's shutdown. (pruiz)
                // See: http://code.google.com/p/wkhtmltopdf/issues/detail?id=511
                if (_initiWorkAround == null)
                {
                    _Log.InfoFormat("Initializing converter infrastructure..");
                    _worker.Invoke((Action)(() => _initiWorkAround = new WkHtmlToPdfConverter()));
                    _Log.InfoFormat("Initialized converter infrastructure..");

                    AppDomain.CurrentDomain.ProcessExit += (o, e) =>
                        _worker.Invoke((Action)(() => {
                            _Log.InfoFormat("Disposing converter infraestructure..");
                            _initiWorkAround.Dispose();
                            _initiWorkAround = null;
                            _Log.InfoFormat("Disposed converter infraestructure..");
                        }));
                }
            }

            _worker.Invoke((Action)(() => _converter = new WkHtmlToPdfConverter()));
        }
Exemple #3
0
        public void Dispose()
        {
            if (_converter != null)
            {
                _worker.Invoke((Action)(() => _converter.Dispose()));
            }

            _converter = null;
        }
Exemple #4
0
        private void btnGo_Click(object sender, EventArgs e)
        {
            BackgroundWorker bgw = new BackgroundWorker();
            bgw.WorkerReportsProgress = true;

            //string address = tbHttpAddress.Text;
            string outputFile = tbOutputFile.Text;

            bgw.ProgressChanged += (o, args) => {
                progressBar1.Value = args.ProgressPercentage;
                lblProgress.Text = args.UserState as string;
            };

            if (outputFile.EndsWith(".pdf")) {
                bgw.DoWork += (o, args) => {
                    Tuple<string, string> t = args.Argument as Tuple<string, string>;

                    string address = t.Item1;
                    string outFile = t.Item2;

                    WkHtmlToPdfConverter conv = new WkHtmlToPdfConverter();
                    conv.ObjectSettings.Page = address;
                    conv.GlobalSettings.Out = outFile;
                    conv.ProgressChanged += (sender1, eventArgs) => bgw.ReportProgress(eventArgs.Value, eventArgs.Value2);
                    conv.PhaseChanged += (sender1, eventArgs) => this.Invoke(new Action(() => { lblPhase.Text = string.Format("Phase {0}: {1}", eventArgs.Value, eventArgs.Value2); }));

                    conv.Convert();

                    conv.Dispose();
                };
            } else if (outputFile.EndsWith(".jpg") || outputFile.EndsWith(".png")) {
                bgw.DoWork += (o, args) => {
                    Tuple<string, string> t = args.Argument as Tuple<string, string>;

                    string address = t.Item1;
                    string outFile = t.Item2;

                    WkHtmlToImageConverter conv = new WkHtmlToImageConverter();
                    conv.GlobalSettings.In = address;
                    conv.GlobalSettings.Out = outFile;
                    conv.ProgressChanged += (sender1, eventArgs) => bgw.ReportProgress(eventArgs.Value, eventArgs.Value2);
                    conv.PhaseChanged += (sender1, eventArgs) => this.Invoke(new Action(() => { lblPhase.Text = string.Format("Phase {0}: {1}", eventArgs.Value, eventArgs.Value2); }));

                    conv.Convert();

                    conv.Dispose();
                };
            }

            bgw.RunWorkerCompleted += (o, args) => bgw.Dispose();

            bgw.RunWorkerAsync(new Tuple<string, string>(tbHttpAddress.Text, outputFile));
        }
Exemple #5
0
        static void Main(string[] args)
        {
            if (args.Length != 2) {
                PrintInstructions();
                return;
            }

            if (args[1].EndsWith(".pdf")) {
                WkHtmlToPdfConverter conv = new WkHtmlToPdfConverter();
                conv.GlobalSettings.Out = args[1];
                conv.ObjectSettings.Page = args[0];

                int phaseLine = Console.CursorTop + 1;
                int progressLine = phaseLine + 1;

                conv.PhaseChanged += (sender, eventArgs) => { Console.CursorTop = phaseLine; Console.CursorLeft = 0; Console.Write("Phase: {0} - {1}", eventArgs.Value, eventArgs.Value2); };
                conv.ProgressChanged += (sender, eventArgs) => { Console.CursorTop = progressLine; Console.CursorLeft = 0; Console.Write("Progress: {0} - {1}", eventArgs.Value, eventArgs.Value2); };
                conv.Finished += (sender, eventArgs) => { Console.CursorTop = progressLine + 2; Console.CursorLeft = 0; };

                conv.Convert();

                conv.Dispose();
            } else {
                WkHtmlToImageConverter conv = new WkHtmlToImageConverter();
                conv.GlobalSettings.Out = args[1];
                conv.GlobalSettings.In = args[0];

                int phaseLine = Console.CursorTop + 1;
                int progressLine = phaseLine + 1;

                conv.PhaseChanged += (sender, eventArgs) => { Console.CursorTop = phaseLine; Console.CursorLeft = 0; Console.Write("Phase: {0} - {1}", eventArgs.Value, eventArgs.Value2); };
                conv.ProgressChanged += (sender, eventArgs) => { Console.CursorTop = progressLine; Console.CursorLeft = 0; Console.Write("Progress: {0} - {1}", eventArgs.Value, eventArgs.Value2); };
                conv.Finished += (sender, eventArgs) => { Console.CursorTop = progressLine + 2; Console.CursorLeft = 0; };

                conv.Convert();

                conv.Dispose();
            }
        }
        public void Dispose()
        {
            if (_converter != null)
                _worker.Invoke((Action)(() => _converter.Dispose()));

            _converter = null;
        }