【问题标题】:Generate PDF from ApiController using Rotativa and WebAPI使用 Rotativa 和 WebAPI 从 ApiController 生成 PDF
【发布时间】: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


【解决方案1】:

Rotativa 似乎不适合您的方案。使用像DinkToPdf 这样的替代库会更容易,它既不需要视图也不需要控制器上下文。您应该能够使用它的数据库中的 HTML。

如果您真的需要使用Rotativa,您可以尝试以下方法:

  • 添加一个虚拟视图,您可以使用 ViewModel 将 HTML 从数据库传递到该视图
  • 创建一个虚拟上下文,如this question所示

【讨论】:

    【解决方案2】:

    试试这个: 从 API 控制器称为 MVC 控制器它为我工作

    API 控制器

    public class DocumentsController : ApiController
    {
            public HttpResponseMessage Pdf()
            {
                PDFController controller = new PDFController();
                RouteData route = new RouteData();
                route.Values.Add("action", "getPdf"); // ActionName
                route.Values.Add("controller", "PDF"); // Controller Name
                System.Web.Mvc.ControllerContext newContext = new 
                System.Web.Mvc.ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, controller);
                controller.ControllerContext = newContext;
                var actionPDF = controller.getPdf(); 
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new ByteArrayContent(actionPDF);// new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
                response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = "SamplePDF.PDF";
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                return response;
            }            
    }
    

    MVC 控制器

        public class PDFController : Controller
        {
                public Byte[]  getPDF()
                {
                var actionPDF = new Rotativa.ViewAsPdf("YOUR_VIEW_NAME") )
                {
    
                    PageSize = Rotativa.Options.Size.A4,
                    PageOrientation = Rotativa.Options.Orientation.Landscape,
                   PageMargins = { Left = 1, Right = 1 }
                };
                byte[] applicationPDFData = actionPDF.BuildPdf(this.ControllerContext);
                return applicationPDFData;
            }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      相关资源
      最近更新 更多