【发布时间】:2018-04-11 12:36:00
【问题描述】:
我想使用 ApiController 中的 Rotativa 框架创建一个 pdf 文件。 在我的项目中没有任何视图。 pdf (HTML) 的内容在数据库中。
有什么方法可以在没有 View 和 ControllerContext 的情况下生成 pdf 吗?控制器上下文只能在 Controller 中访问,而不能在 APIController 中访问...
我找到了这个https://github.com/JustMaier/Rotativa/tree/ed7678eefdffb3995f5b6a3e9afb18903c648df8 但它需要视图
问题在于方法 BuildFile()...
从这里开始
var a = new Rotativa.ViewAsPdf(pdfResult);
a.FileName = request.ResultFileName;
byte[] f = a.BuildFile();
ControlerContext 可以在这里为空
public byte[] BuildFile(ControllerContext context = null)
{
if (this.WkhtmlPath == string.Empty)
this.WkhtmlPath = context == null ? HttpContext.Current.Server.MapPath("~/Rotativa") : context.HttpContext.Server.MapPath("~/Rotativa");
var fileContent = this.CallTheDriver(context);
if (string.IsNullOrEmpty(this.SaveOnServerPath) == false)
{
File.WriteAllBytes(this.SaveOnServerPath, fileContent);
}
return fileContent;
}
...但是这里不能
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;
}
我做错了什么?
【问题讨论】:
-
您特别想使用 Rotativa 吗?
-
我需要使用可以将 html/razor 转换为 pdf 并且是免费的东西
-
asp.net webapi ?
-
如果您的网站可以提供它,Chrome 可以查看它(从而生成它的 PDF)。
标签: c# asp.net-web-api2 rotativa