IDC

如何在 ASP.NET Core 5 中生成 PDF

作者:admin 2021-04-24 我要评论

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。 大家用 ASP.NET Core 进行项目开发时,常会有生成 PDF 的需求,那如何生成呢...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。

大家用 ASP.NET Core 进行项目开发时,常会有生成 PDF 的需求,那如何生成呢?这篇文章我们就来讨论如何通过 DinkToPdf 来生成 PDF 文档,DinkToPdf 封装了 C++ 的 wkhtmltopdf 工具包,前者通过 P/Invoke 的方式来调用后者,而底层的 wkhtmltopdf 利用 Qt WebKit 渲染引擎将 html 转成 pdf。

安装 DinkToPdf

要想安装 DinkToPdf,可以通过 Nuget 可视化界面或者通过 NuGet Package Manager Console 命令行工具输入以下命令:

  1. Install-Package DinkToPdf 

安装完毕之后可以验证下 DinkToPdf.dll 是否已成功引用到项目中。

既然是封装了 C++ 的 wkhtmltopdf,肯定要拿到原生的 wkhtmltopdf 工具包, 官方下载地址:https://wkhtmltopdf.org/downloads.html ,也可以在 DinkToPdf 的官方Github:https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4 上下载,然后根据你的需要选择 32bit 还是 64bit 。

注册 DinkToPdf

要想在 ASP.NET Core 中使用,需要在 ConfigureServices() 方法下将 DinkToPdf 注入到 IOC 容器中,下面的代码展示了如何去实现。

  1. public void ConfigureServices(IServiceCollection services) 
  2.         { 
  3.             services.AddSingleton(typeof(IConverter),new SynchronizedConverter(new PdfTools())); 
  4.  
  5.             services.AddControllers(); 
  6.         } 

创建 ReportService

基础配置做好之后,接下来我们来写生成 PDF 的业务逻辑,创建一个 IReportService 和 ReportService 实现类,代码如下:

  1. public interface IReportService 
  2.     { 
  3.         public byte[] GeneratePdfReport(); 
  4.     } 
  5.  
  6.     public class ReportService : IReportService 
  7.     { 
  8.         private readonly IConverter _converter; 
  9.         public ReportService(IConverter converter) 
  10.         { 
  11.             _converter = converter; 
  12.         } 
  13.         public byte[] GeneratePdfReport() 
  14.         { 
  15.             throw new NotImplementedException(); 
  16.         } 
  17.     } 

从上面的代码可以看出,IConverter 实例是通过 构造函数 注入的,接下来可以在 GeneratePdfReport() 方法中构建生成 pdf 的具体业务逻辑。

  1. public byte[] GeneratePdfReport() 
  2.     var html = $@" 
  3.                    <!DOCTYPE html> 
  4.                    <html lang=""en""
  5.                    <head> 
  6.                        This is the header of this document. 
  7.                    </head> 
  8.                   <body> 
  9.                   <h1>This is the heading for demonstration purposes only.</h1> 
  10.                   <p>This is a line of text for demonstration purposes only.</p> 
  11.                   </body> 
  12.                   </html> 
  13.                   "; 
  14.     GlobalSettings globalSettings = new GlobalSettings(); 
  15.     globalSettings.ColorMode = ColorMode.Color; 
  16.     globalSettings.Orientation = Orientation.Portrait; 
  17.     globalSettings.PaperSize = PaperKind.A4; 
  18.     globalSettings.Margins = new MarginSettings { Top = 25, Bottom = 25 }; 
  19.     ObjectSettings objectSettings = new ObjectSettings(); 
  20.     objectSettings.PagesCount = true
  21.     objectSettings.HtmlContent = html; 
  22.     WebSettings webSettings = new WebSettings(); 
  23.     webSettings.DefaultEncoding = "utf-8"
  24.     HeaderSettings headerSettings = new HeaderSettings(); 
  25.     headerSettings.FontSize = 15; 
  26.     headerSettings.FontName = "Ariel"
  27.     headerSettings.Right = "Page [page] of [toPage]"
  28.     headerSettings.Line = true
  29.     FooterSettings footerSettings = new FooterSettings(); 
  30.     footerSettings.FontSize = 12; 
  31.     footerSettings.FontName = "Ariel"
  32.     footerSettings.Center = "This is for demonstration purposes only."
  33.     footerSettings.Line = true
  34.     objectSettings.HeaderSettings = headerSettings; 
  35.     objectSettings.FooterSettings = footerSettings; 
  36.     objectSettings.WebSettings = webSettings; 
  37.     HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument() 
  38.     { 
  39.         GlobalSettings = globalSettings, 
  40.         Objects = { objectSettings }, 
  41.     }; 
  42.     return _converter.Convert(htmlToPdfDocument); 

然后再将 IReportService 和 ReportService 注入到 IOC 容器中,如下代码所示:

  1. services.AddSingleton<IReportService, ReportService>(); 

创建 ReportController

GeneratePdfReport() 方法的业务逻辑构建好之后,现在可以将 IReportService 实例注入到 ReportController 中来最终渲染 pdf,下面的代码展示了如何去实现。

  1. [Route("api/[controller]")] 
  2.  [ApiController] 
  3.  public class ReportController : ControllerBase 
  4.  { 
  5.      private readonly IReportService _reportService; 
  6.      public ReportController(IReportService reportService) 
  7.      { 
  8.          _reportService = reportService; 
  9.      } 
  10.      [HttpGet] 
  11.      public IActionResult Get() 
  12.      { 
  13.          var pdfFile = _reportService.GeneratePdfReport(); 
  14.          return File(pdfFile,"application/octet-stream""SimplePdf.pdf"); 
  15.      } 
  16.  } 

在 ASP.NET Core 中并没有内置对 pdf 的支持,所以有这方面的需求只能借助于第三方框架,而 DinkToPdf 就是这么一款非常优秀的工具包,DinkToPdf 是一款用 .NET 语言编写的用于包装 C++ 的 wkhtmltopdf 的工具包,它可以非常方便的将 Html 转成 PDF ,关于更多 DinkToPdf 可参考 Github:https://github.com/rdvojmoc/DinkToPdf

译文链接:https://www.infoworld.com/article/3605276/how-to-create-pdf-documents-in-aspnet-core-5.html


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/8PFYJUaEDPk0tqidZKqSrg

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 如何在 ASP.NET Core 5 中生成 PDF

    如何在 ASP.NET Core 5 中生成 PDF

  • 这些提高你代码性能的技巧你值得拥有

    这些提高你代码性能的技巧你值得拥有

  • React 受控组件,Hooks 方式!

    React 受控组件,Hooks 方式!

  • Steam 增加了对 Mesa 着色器单文件缓存

    Steam 增加了对 Mesa 着色器单文件缓存

腾讯云代理商
海外云服务器