如何使一个ASP.NET MVC查看PDF格式格式、NET、ASP、PDF

2023-09-02 21:32:49 作者:夢想的年代

我正与ExpertPDF的HTML到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我正在寻找帮助。是否有一个快速的方式来获得的视图的字符串再presentation?也许我应该坚持的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在响应,并与一些存储所产生的HTML在一个MemoryStream更换过滤器属性。然后,你可以清除OnResultExecuted在响应,并与您的PDF转换的结果取代。我不知道,这将是比刚刚起步的HTML从URL更好,虽然。

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将需要重写写入方法(S)和数据发送到所述存储器流代替

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