Пример #1
0
        public RtvMsg RotativaFun()
        {
            //欲轉換PDF的URL位置
            string pdfurl = "https://www.google.com/";
            //指定檔案名稱
            string fileName = "FromApi.pdf";
            //選擇儲存Server端路徑
            string pdfTemp = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa") + @"\" + fileName;
            //wkhtmltopdf.exe 路徑
            string wkhtmltopdfPath = System.Web.HttpContext.Current.Server.MapPath("~/Rotativa");
            //wkhtmltopdf.exe 欲執行的指令
            string Command = "--zoom 2 --margin-top 15mm --margin-bottom 15mm --ma  rgin-right 15mm --margin-left 15mm --page-size A4  ";

            //透過Rotativa所提供的靜態類別中的Convert執行轉換HTML TO PDF
            byte[] GenPdfByte = WkhtmltopdfDriver.Convert(wkhtmltopdfPath, Command + pdfurl);
            System.IO.File.WriteAllBytes(pdfTemp, GenPdfByte);
            RtvMsg Rtv = new RtvMsg();

            if (System.IO.File.Exists(pdfTemp))
            {
                string UrlPath = @"http://" + this.Request.RequestUri.Authority + "/Rotativa/" + fileName;
                Rtv.Path    = UrlPath;
                Rtv.Message = "已產生檔案";
            }
            else
            {
                Rtv.Path    = "";
                Rtv.Message = "檔案未產生";
            }

            return(Rtv);
        }
        protected override async Task <byte[]> CallTheDriver(ActionContext actionContext)
        {
            var html = await RenderPageAsString(actionContext);

            // copied from https://github.com/webgio/Rotativa.AspNetCore/blob/c907afa8c7dd6a565d307901741c336c429fc698/Rotativa.AspNetCore/ViewAsPdf.cs#L147-L151
            string baseUrl       = string.Format("{0}://{1}", actionContext.HttpContext.Request.Scheme, actionContext.HttpContext.Request.Host);
            var    htmlForWkhtml = Regex.Replace(html.ToString(), "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl), RegexOptions.IgnoreCase);

            byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(WkhtmlPath, GetConvertOptions(), htmlForWkhtml);
            return(fileContent);
        }
Пример #3
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);
        }
Пример #4
0
        protected override async Task <byte[]> CallTheDriver(ActionContext context)
        {
            // use action name if the view name was not provided
            string viewName = ViewName;

            if (string.IsNullOrEmpty(ViewName))
            {
                viewName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName;
            }
            ViewEngineResult viewResult = GetView(context, viewName, MasterName);
            var html = new StringBuilder();
            ITempDataProvider tempDataProvider = context.HttpContext.RequestServices.GetService(typeof(ITempDataProvider)) as ITempDataProvider;
            var viewDataDictionary             = new ViewDataDictionary(
                metadataProvider: new EmptyModelMetadataProvider(),
                modelState: new ModelStateDictionary())
            {
                Model = this.Model
            };

            if (this.ViewData != null)
            {
                foreach (var item in this.ViewData)
                {
                    viewDataDictionary.Add(item);
                }
            }
            using (var output = new StringWriter())
            {
                var view = viewResult.View;
                var tempDataDictionary = new TempDataDictionary(context.HttpContext, tempDataProvider);
                var viewContext        = new ViewContext(
                    context,
                    viewResult.View,
                    viewDataDictionary,
                    tempDataDictionary,
                    output,
                    new HtmlHelperOptions());
                await view.RenderAsync(viewContext);

                html = output.GetStringBuilder();
            }
            string baseUrl       = string.Format("{0}://{1}", context.HttpContext.Request.Scheme, context.HttpContext.Request.Host);
            var    htmlForWkhtml = Regex.Replace(html.ToString(), "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl), RegexOptions.IgnoreCase);

            byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), htmlForWkhtml);
            return(fileContent);
        }
Пример #5
0
 protected override byte[] WkhtmlConvert(string switches)
 {
     return(WkhtmltopdfDriver.Convert(this.WkhtmlPath, switches));
 }