Ejemplo n.º 1
0
        private static byte[] Convert(string wkhtmltopdfPath, string switches, string html)
        {
            switches = "-q " + switches + " -";
            if (!string.IsNullOrEmpty(html))
            {
                switches += " -";
                html      = WkhtmltopdfDriver.SpecialCharsEncode(html);
            }
            Process process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.exe"),
                    Arguments              = switches,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    RedirectStandardInput  = true,
                    WorkingDirectory       = wkhtmltopdfPath,
                    CreateNoWindow         = true
                }
            };

            process.Start();
            if (!string.IsNullOrEmpty(html))
            {
                using (StreamWriter standardInput = process.StandardInput)
                {
                    standardInput.WriteLine(html);
                }
            }
            MemoryStream memoryStream = new MemoryStream();

            using (Stream baseStream = process.StandardOutput.BaseStream)
            {
                byte[] array = new byte[4096];
                int    count;
                while ((count = baseStream.Read(array, 0, array.Length)) > 0)
                {
                    memoryStream.Write(array, 0, count);
                }
            }
            string message = process.StandardError.ReadToEnd();

            if (memoryStream.Length == 0L)
            {
                throw new Exception(message);
            }
            process.WaitForExit();
            return(memoryStream.ToArray());
        }
Ejemplo n.º 2
0
 protected override byte[] CallTheDriver(ActionContext context)
 {
     _tempDataProvider = (ITempDataProvider)context.HttpContext.RequestServices.GetService(typeof(ITempDataProvider));
     _viewEngine       = (IRazorViewEngine)context.HttpContext.RequestServices.GetService(typeof(IRazorViewEngine));
     if (string.IsNullOrEmpty(this.ViewName))
     {
         this.ViewName = context.RouteData.Values["action"].ToString();
     }
     byte[] result;
     using (StringWriter stringWriter = new StringWriter())
     {
         ViewEngineResult view = this.GetView(context, this.ViewName, this.MasterName);
         if (view.View == null)
         {
             StringBuilder stringBuilder = new StringBuilder();
             stringBuilder.AppendLine();
             foreach (string current in view.SearchedLocations)
             {
                 stringBuilder.AppendLine(current);
             }
             throw new InvalidOperationException(string.Format("The view '{0}' or its master was not found, searched locations: {1}", this.ViewName, stringBuilder));
         }
         var viewContext = new ViewContext(
             context,
             view.View,
             new ViewDataDictionary(
                 metadataProvider: new EmptyModelMetadataProvider(),
                 modelState: new ModelStateDictionary())
         {
             Model = this.Model
         },
             new TempDataDictionary(
                 context.HttpContext,
                 this._tempDataProvider),
             stringWriter,
             new HtmlHelperOptions());
         view.View.RenderAsync(viewContext).GetAwaiter().GetResult();
         string text = stringWriter.GetStringBuilder().ToString();
         string arg  = string.Format("{0}://{1}", context.HttpContext.Request.Scheme, context.HttpContext.Request.Host.Value);
         text = Regex.Replace(text, "<head>", string.Format("<head><base href=\"{0}\" />", arg), RegexOptions.IgnoreCase);
         byte[] array = WkhtmltopdfDriver.ConvertHtml(base.WkhtmltopdfPath, base.GetConvertOptions(), text);
         result = array;
     }
     return(result);
 }
Ejemplo n.º 3
0
 public static byte[] ConvertHtml(string wkhtmltopdfPath, string switches, string html)
 {
     return(WkhtmltopdfDriver.Convert(wkhtmltopdfPath, switches, html));
 }
Ejemplo n.º 4
0
        protected virtual byte[] CallTheDriver(ActionContext context)
        {
            string wkParams = this.GetWkParams(context);

            return(WkhtmltopdfDriver.Convert(this.WkhtmltopdfPath, wkParams));
        }