/// <summary> /// Setup Rotativa library /// </summary> /// <param name="env">The IHostingEnvironment object</param> /// <param name="wkhtmltopdfRelativePath">Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html</param> public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, string wkhtmltopdfRelativePath = "Rotativa") { IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (IsWindows) { RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath); if (!Directory.Exists(RotativaPath)) { throw new Exception("Folder containing wkhtmltopdf.exe not found, searched for " + RotativaPath); } } else { RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath); if (!Directory.Exists(RotativaPath)) { throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath); } } var updateableFileProvider = new UpdateableFileProvider(); var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); services.TryAddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>(); services.TryAddSingleton <DiagnosticSource>(diagnosticSource); services.TryAddTransient <ITempDataProvider, SessionStateTempDataProvider>(); services.TryAddTransient <IRazorViewToStringRenderer, RazorViewToStringRenderer>(); services.TryAddTransient <IGeneratePdf, GeneratePdf>(); services.TryAddSingleton(updateableFileProvider); return(services); }
/// <summary> /// Setup Rotativa library /// </summary> /// <param name="services">The IServiceCollection object</param> /// <param name="wkhtmltopdfRelativePath">Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html</param> public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, params string[] wkhtmltopdfRelativePath) { foreach (var path in wkhtmltopdfRelativePath) { if (Directory.Exists(path) || File.Exists(path)) { RotativaPath = path; break; } } if (RotativaPath == "") { throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath); } var fileProvider = new UpdateableFileProvider(); services.TryAddTransient <ITempDataProvider, SessionStateTempDataProvider>(); services.TryAddSingleton(fileProvider); services.TryAddSingleton <IRazorViewEngine, RazorViewEngine>(); services.AddMvc(); // services.AddMvc().AddRazorRuntimeCompilation(options => options.FileProviders.Add(fileProvider)) // .SetCompatibilityVersion(CompatibilityVersion.Latest); services.Configure <MvcRazorRuntimeCompilationOptions>(options => { options.FileProviders.Add(fileProvider); }); services.TryAddTransient <IRazorViewToStringRenderer, RazorViewToStringRenderer>(); services.TryAddTransient <IGeneratePdf, GeneratePdf>(); return(services); }
public void UpdateView(string path, string viewHTML) { if (ExistsView(path)) { UpdateableFileProvider.UpdateContent(viewHTML, $"/Views/{path}.cshtml"); } else { throw new Exception($"View {path} does not exists"); } }
/// <summary> /// Setup Rotativa library /// </summary> /// <param name="services">The IServiceCollection object</param> /// <param name="wkhtmltopdfRelativePath">Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html</param> public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, string wkhtmltopdfRelativePath = "Rotativa") { RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath); if (!Directory.Exists(RotativaPath)) { throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath); } var fileProvider = new UpdateableFileProvider(); services.TryAddTransient <ITempDataProvider, SessionStateTempDataProvider>(); services.TryAddSingleton(fileProvider); services.TryAddSingleton <IRazorViewEngine, RazorViewEngine>(); services.AddMvc().AddRazorRuntimeCompilation(options => options.FileProviders.Add(fileProvider)) .SetCompatibilityVersion(CompatibilityVersion.Latest); services.TryAddTransient <IRazorViewToStringRenderer, RazorViewToStringRenderer>(); services.TryAddTransient <IGeneratePdf, GeneratePdf>(); return(services); }
/// <summary> /// Setup Rotativa library /// </summary> /// <param name="env">The IHostingEnvironment object</param> /// <param name="wkhtmltopdfRelativePath">Optional. Relative path to the directory containing wkhtmltopdf. Default is "Rotativa". Download at https://wkhtmltopdf.org/downloads.html</param> public static IServiceCollection AddWkhtmltopdf(this IServiceCollection services, string wkhtmltopdfRelativePath = "Rotativa") { RotativaPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, wkhtmltopdfRelativePath); if (!Directory.Exists(RotativaPath)) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { throw new Exception("Folder containing wkhtmltopdf.exe not found, searched for " + RotativaPath); } throw new Exception("Folder containing wkhtmltopdf not found, searched for " + RotativaPath); } var updateableFileProvider = new UpdateableFileProvider(); var diagnosticSource = new DiagnosticListener("Microsoft.AspNetCore"); services.TryAddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>(); services.TryAddSingleton <DiagnosticSource>(diagnosticSource); services.TryAddTransient <ITempDataProvider, SessionStateTempDataProvider>(); services.TryAddTransient <IRazorViewToStringRenderer, RazorViewToStringRenderer>(); services.TryAddTransient <IGeneratePdf, GeneratePdf>(); services.TryAddSingleton(updateableFileProvider); if (services.BuildServiceProvider().GetService <IControllerFactory>() == null) { var _hostingEnvironment = new HostingEnvironment(); services.TryAddSingleton <IHostingEnvironment>(_hostingEnvironment); services.AddLogging(); services.AddMvc(); } services.Configure <RazorViewEngineOptions>(options => { options.FileProviders.Add(updateableFileProvider); }); return(services); }
public async Task <string> RenderHtmlToStringAsync <TModel>(string html, TModel model) { UpdateableFileProvider.UpdateContent(html); return(await RenderViewToStringAsync("/Views/FakeView.cshtml", model)); }