如何以 PDF 格式呈现 ASP.NET MVC 视图 [英] How to render an ASP.NET MVC View in PDF format

查看:31
本文介绍了如何以 PDF 格式呈现 ASP.NET MVC 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ExpertPDF 的 Html-to-PDF 转换实用程序来解决这个问题(尽管如果有足够的文档,我对其他库持开放态度).

I'm working with ExpertPDF's Html-to-PDF conversion utility for this question (although I'm open to other libraries if there's sufficient documentation).

简而言之,我有一个以特定方式格式化的视图,我想将其呈现为用户可以保存到磁盘的 PDF 文档.

In short, I have a view that is formatted a specific way and I would like to render it as a PDF document the user can save to disk.

到目前为止我有一个 PrintService(它实现了一个 IPrintService 接口),这个实现有两个 PrintToPDF() 重载,一个只需要一个 URL,另一个需要一个 HTML 字符串,两者都返回一个字节[].我只计算了需要 HTML 字符串的第二个重载的细节.

What I have so far is a PrintService (which implements an IPrintService interface) and this implementation has two overloads for PrintToPDF(), one that takes just a URL and another that takes an HTML string, and both of which return a byte[]. I've only worked out the details of the second overload which requires the HTML string.

我想从我的控制器做的事情是这样的:

What I would like to do from my controller is something like:

public FileStreamResult Print(int id)
{
    var model = _CustomRepository.Get(id);
    string renderedView = SomethingThatRendersMyViewAsAString(model);
    Stream byteStream = _PrintService.PrintToPdf(renderedView);
    HttpContext.Response.AddHeader("content-disposition", 
        "attachment; filename=report.pdf");
    return new FileStreamResult(byteStream, "application/pdf");  
}

理论上会将 PDF 渲染到页面.这是我正在寻求帮助的SomethingThatRendersMyViewAsAString".有没有一种快速的方法来获取视图的字符串表示?或者也许我应该坚持使用 URL 重载并将 URL 传递给视图......还有其他想法吗?

which in theory would render a PDF to the page. It's the "SomethingThatRendersMyViewAsAString" that I'm looking for help with. Is there a quick way to get the string representation of a View? Or perhaps I should just stick with the URL overload and pass in a URL to the view... Any other thoughts?

谢谢!

推荐答案

您可能能够在 OnResultExecuting 期间利用 Response 并将 Filter 属性替换为将结果 HTML 存储在 MemoryStream 中的内容.然后您可以在 OnResultExecuted 期间清除 Response 并将其替换为您的 PDF 转换结果.不过,我不确定这是否比仅从 URL 获取 HTML 更好.

You might be able to tap into the Response during OnResultExecuting and replace the Filter property with something that stores the resultant HTML in a MemoryStream. Then you could clear the Response during OnResultExecuted and replace it with the results of your PDF conversion. I'm not sure that this would be better than just getting the HTML from the URL, though.

 public FileStreamResult Print(int id)
 {
     var model = _CustomRepository.Get(id);
     this.ConvertToPDF = true;
     return View( "HtmlView" );
 }

 public override OnResultExecuting( ResultExecutingContext context )
 {
      if (this.ConvertToPDF)
      {
          this.PDFStream = new MemoryStream();
          context.HttpContext.Response.Filter = new PDFStreamFilter( this.PDFStream );
      }
 }

 public override OnResultExecuted( ResultExecutedContext context )
 {
      if (this.ConvertToPDF)
      {
          context.HttpContext.Response.Clear();
          this.PDFStream.Seek( 0, SeekOrigin.Begin );
          Stream byteStream = _PrintService.PrintToPDF( this.PDFStream );
          StreamReader reader = new StreamReader( byteStream );
          context.HttpContext.Response.AddHeader( "content-disposition",
                 "attachment; filename=report.pdf" );
          context.HttpContext.Response.AddHeader( "content-type",
                 "application/pdf" );
          context.HttpContext.Response.Write( reader.ReadToEnd() );
      }
}

PDFStreamFilter 需要重写Write"方法并将数据发送到内存流.

The PDFStreamFilter would need to override the "Write" method(s) and send the data to the memory stream instead.

这篇关于如何以 PDF 格式呈现 ASP.NET MVC 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆