示例#1
0
        private bool GetUrlPdfResponse(GeneratePdfRequest 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.GeneratePdfRequestQueue, serialisedObject);
            sub.Publish(RequestSubcriberChannelNames.GeneratePdfRequestChannel, "");

            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(string sourcePath, string destinationPath, string pdfConverterPath = "", string fileName = "")
        {
            _sourcePath      = sourcePath;
            _destinationPath = destinationPath;
            _fileName        = string.IsNullOrWhiteSpace(_fileName) ? Guid.NewGuid() + ".pdf" : fileName + ".pdf";

            var outputpath = _destinationPath + _fileName;

            var url = new Uri(_sourcePath, UriKind.Absolute);

            if (url.Scheme != Uri.UriSchemeHttp && url.Scheme != Uri.UriSchemeHttps)
            {
                return(null);
            }

            /*
             * if (_sourcePath.IndexOfAny(Path.GetInvalidPathChars()) != -1)
             *  return null;
             * if (outputpath.IndexOfAny(Path.GetInvalidPathChars()) != -1)
             *  return null;
             * if (_destinationPath.IndexOfAny(Path.GetInvalidPathChars()) != -1)
             *  return null;
             * if (pdfConverterPath.IndexOfAny(Path.GetInvalidPathChars()) != -1)
             *  return null;
             *
             * _process = new Process();
             *
             * var startInfo = new ProcessStartInfo
             *              {
             *                  FileName = string.IsNullOrWhiteSpace(pdfConverterPath) ? @"wkhtmltopdf.exe" : pdfConverterPath + @"/wkhtmltopdf.exe",
             *                  Arguments = Switches + " \"" + _sourcePath + "\" " + outputpath,
             *                  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();
             *
             * // read the output here...
             * var output = _process.StandardOutput.ReadToEnd();
             * //var error = _process.StandardError.ReadToEnd();
             *
             * _process.WaitForExit(30000);
             *
             * int returnCode = _process.ExitCode;
             *
             * _process.Close();
             */
            var fName = string.IsNullOrWhiteSpace(pdfConverterPath) ? @"wkhtmltopdf.exe" : pdfConverterPath + @"/wkhtmltopdf.exe";
            var args  = Switches + " \"" + _sourcePath + "\" " + outputpath;
            var pub   = new GeneratePdfRequest {
                FileName = fName, Arguments = args, DestinationPath = _destinationPath
            };

            var success = GetUrlPdfResponse(pub);

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