示例#1
0
        private bool GetHtmlStreamPdfResponse(HtmlStreamPdfRequest pub)
        {
            IDatabase   db               = RedisConnection.ConnectionMultiplexer.GetDatabase(_settings.RedisDatabaseKey);
            ISubscriber sub              = RedisConnection.ConnectionMultiplexer.GetSubscriber();
            var         elapsedSeconds   = 0;
            var         serialisedObject = Newtonsoft.Json.JsonConvert.SerializeObject(pub);

            db.ListLeftPush(RequestSubcriberChannelNames.HtmlStreamPdfRequestQueue, serialisedObject);
            sub.Publish(RequestSubcriberChannelNames.HtmlStreamPdfRequestChannel, "");

            var value = string.Empty;

            while (string.IsNullOrEmpty(value))
            {
                elapsedSeconds++;
                value = db.StringGet(pub.Guid);
                if (!string.IsNullOrEmpty(value))
                {
                    db.KeyDelete(pub.Guid, CommandFlags.FireAndForget);
                    return(value == "Completed");
                }

                Thread.Sleep(1000);
                if (elapsedSeconds == WaitForSeconds)
                {
                    db.KeyDelete(pub.Guid, CommandFlags.FireAndForget);
                    return(false);
                }
            }
            return(true);
        }
示例#2
0
        public string Generate(StringBuilder htmlStream, string destinationPath, string pdfConverterPath = "", string fileName = "")
        {
            _destinationPath = destinationPath;
            _fileName        = string.IsNullOrWhiteSpace(fileName) ? Guid.NewGuid() + ".pdf" : fileName + ".pdf";

            /*
             * _process = new Process();
             * var startInfo = new ProcessStartInfo
             * {
             *  FileName = string.IsNullOrWhiteSpace(pdfConverterPath) ? @"wkhtmltopdf.exe" : pdfConverterPath + @"\wkhtmltopdf.exe",
             *  Arguments = Switches + " " + "-" + " " + _destinationPath + _fileName,
             *  UseShellExecute = false, // needs to be false in order to redirect output
             *  RedirectStandardOutput = true,
             *  RedirectStandardError = true,
             *  RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
             *  WorkingDirectory = destinationPath
             * };
             *
             *
             * _process.StartInfo = startInfo;
             * _process.Start();
             *
             * var streamWriter = _process.StandardInput;
             * streamWriter.Write(htmlStream.ToString());
             * streamWriter.Close();
             *
             * // read the output here...
             * string output = _process.StandardOutput.ReadToEnd();
             *
             * // ...then wait n milliseconds for exit (as after exit, it can't read the output)
             * _process.WaitForExit(30000);
             *
             * int returnCode = _process.ExitCode;
             *
             * _process.Close();
             *
             * */
            var fName = string.IsNullOrWhiteSpace(pdfConverterPath) ? @"wkhtmltopdf.exe" : pdfConverterPath + @"\wkhtmltopdf.exe";
            var args  = Switches + " " + "-" + " " + _destinationPath + _fileName;
            var pub   = new HtmlStreamPdfRequest {
                FileName = fName, Arguments = args, DestinationPath = _destinationPath, HtmlStream = htmlStream.ToString()
            };

            var success = GetHtmlStreamPdfResponse(pub);

            // if 0, it worked
            return((success) ? _fileName : null);
        }