如何在 ASP.NET Core 5 中生成 PDF
本文轉載自微信公眾號「碼農讀書」,作者碼農讀書。轉載本文請聯(lián)系碼農讀書公眾號。
大家用 ASP.NET Core 進行項目開發(fā)時,常會有生成 PDF 的需求,那如何生成呢?這篇文章我們就來討論如何通過 DinkToPdf 來生成 PDF 文檔,DinkToPdf 封裝了 C++ 的 wkhtmltopdf 工具包,前者通過 P/Invoke 的方式來調用后者,而底層的 wkhtmltopdf 利用 Qt WebKit 渲染引擎將 html 轉成 pdf。
安裝 DinkToPdf
要想安裝 DinkToPdf,可以通過 Nuget 可視化界面或者通過 NuGet Package Manager Console 命令行工具輸入以下命令:
- 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 上下載,然后根據(jù)你的需要選擇 32bit 還是 64bit 。
注冊 DinkToPdf
要想在 ASP.NET Core 中使用,需要在 ConfigureServices() 方法下將 DinkToPdf 注入到 IOC 容器中,下面的代碼展示了如何去實現(xiàn)。
- public void ConfigureServices(IServiceCollection services)
 - {
 - services.AddSingleton(typeof(IConverter),new SynchronizedConverter(new PdfTools()));
 - services.AddControllers();
 - }
 
創(chuàng)建 ReportService
基礎配置做好之后,接下來我們來寫生成 PDF 的業(yè)務邏輯,創(chuàng)建一個 IReportService 和 ReportService 實現(xiàn)類,代碼如下:
- public interface IReportService
 - {
 - public byte[] GeneratePdfReport();
 - }
 - public class ReportService : IReportService
 - {
 - private readonly IConverter _converter;
 - public ReportService(IConverter converter)
 - {
 - _converter = converter;
 - }
 - public byte[] GeneratePdfReport()
 - {
 - throw new NotImplementedException();
 - }
 - }
 
從上面的代碼可以看出,IConverter 實例是通過 構造函數(shù) 注入的,接下來可以在 GeneratePdfReport() 方法中構建生成 pdf 的具體業(yè)務邏輯。
- public byte[] GeneratePdfReport()
 - {
 - var html = $@"
 - <!DOCTYPE html>
 - <html lang=""en"">
 - <head>
 - This is the header of this document.
 - </head>
 - <body>
 - <h1>This is the heading for demonstration purposes only.</h1>
 - <p>This is a line of text for demonstration purposes only.</p>
 - </body>
 - </html>
 - ";
 - GlobalSettings globalSettings = new GlobalSettings();
 - globalSettings.ColorMode = ColorMode.Color;
 - globalSettings.Orientation = Orientation.Portrait;
 - globalSettings.PaperSize = PaperKind.A4;
 - globalSettings.Margins = new MarginSettings { Top = 25, Bottom = 25 };
 - ObjectSettings objectSettings = new ObjectSettings();
 - objectSettings.PagesCount = true;
 - objectSettings.HtmlContent = html;
 - WebSettings webSettings = new WebSettings();
 - webSettings.DefaultEncoding = "utf-8";
 - HeaderSettings headerSettings = new HeaderSettings();
 - headerSettings.FontSize = 15;
 - headerSettings.FontName = "Ariel";
 - headerSettings.Right = "Page [page] of [toPage]";
 - headerSettings.Line = true;
 - FooterSettings footerSettings = new FooterSettings();
 - footerSettings.FontSize = 12;
 - footerSettings.FontName = "Ariel";
 - footerSettings.Center = "This is for demonstration purposes only.";
 - footerSettings.Line = true;
 - objectSettings.HeaderSettings = headerSettings;
 - objectSettings.FooterSettings = footerSettings;
 - objectSettings.WebSettings = webSettings;
 - HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument()
 - {
 - GlobalSettings = globalSettings,
 - Objects = { objectSettings },
 - };
 - return _converter.Convert(htmlToPdfDocument);
 - }
 
然后再將 IReportService 和 ReportService 注入到 IOC 容器中,如下代碼所示:
- services.AddSingleton<IReportService, ReportService>();
 
創(chuàng)建 ReportController
GeneratePdfReport() 方法的業(yè)務邏輯構建好之后,現(xiàn)在可以將 IReportService 實例注入到 ReportController 中來最終渲染 pdf,下面的代碼展示了如何去實現(xiàn)。
- [Route("api/[controller]")]
 - [ApiController]
 - public class ReportController : ControllerBase
 - {
 - private readonly IReportService _reportService;
 - public ReportController(IReportService reportService)
 - {
 - _reportService = reportService;
 - }
 - [HttpGet]
 - public IActionResult Get()
 - {
 - var pdfFile = _reportService.GeneratePdfReport();
 - return File(pdfFile,"application/octet-stream", "SimplePdf.pdf");
 - }
 - }
 
在 ASP.NET Core 中并沒有內置對 pdf 的支持,所以有這方面的需求只能借助于第三方框架,而 DinkToPdf 就是這么一款非常優(yōu)秀的工具包,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


















 
 
 
 
 
 
 