ConvertHtml() public static method

Converts given HTML string to PDF.
public static ConvertHtml ( string wkhtmltopdfPath, string switches, string html ) : byte[]
wkhtmltopdfPath string Path to wkthmltopdf.
switches string Switches that will be passed to wkhtmltopdf binary.
html string String containing HTML code that should be converted to PDF.
return byte[]
Esempio n. 1
0
        protected override byte[] CallTheDriver(ControllerContext context)
        {
            // use action name if the view name was not provided
            string viewName = ViewName;

            if (string.IsNullOrEmpty(viewName))
            {
                viewName = context.RouteData.GetRequiredString("action");
            }

            ViewEngineResult viewResult = GetView(context, viewName, MasterName);
            string           html       = context.GetHtmlFromView(viewResult, viewName, Model);

            byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), html);
            return(fileContent);
        }
Esempio n. 2
0
        protected override byte[] CallTheDriver(ControllerContext context)
        {
            context.Controller.ViewData.Model = Model;

            // use action name if the view name was not provided
            if (string.IsNullOrEmpty(ViewName))
            {
                ViewName = context.RouteData.GetRequiredString("action");
            }

            using (var sw = new StringWriter())
            {
                ViewEngineResult viewResult = GetView(context, ViewName, MasterName);

                // view not found, throw an exception with searched locations
                if (viewResult.View == null)
                {
                    var locations = new StringBuilder();
                    locations.AppendLine();

                    foreach (string location in viewResult.SearchedLocations)
                    {
                        locations.AppendLine(location);
                    }

                    throw new InvalidOperationException(string.Format("The view '{0}' or its master was not found, searched locations: {1}", ViewName, locations));
                }

                var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);

                StringBuilder html = sw.GetStringBuilder();

                // replace href and src attributes with full URLs
                string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
                html.Replace(" href=\"/", string.Format(" href=\"{0}/", baseUrl));
                html.Replace(" src=\"/", string.Format(" src=\"{0}/", baseUrl));

                var fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmltopdfPath, GetConvertOptions(), html.ToString());
                return(fileContent);
            }
        }
Esempio n. 3
0
        public byte[] BuildFile()
        {
            var fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), ViewHtmlString);

            return(fileContent);
        }