ASP.NET Core 5中如何生成PDF文檔
前言
大家用 ASP.NET Core 進(jìn)行項目開發(fā)時,常會有生成 PDF 的需求,那如何生成呢?這篇文章我們就來討論如何通過 DinkToPdf 來生成 PDF 文檔,DinkToPdf 封裝了 C++ 的 wkhtmltopdf 工具包,前者通過 P/Invoke 的方式來調(diào)用后者,而底層的 wkhtmltopdf 利用 Qt WebKit 渲染引擎將 html 轉(zhuǎn)成 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
基礎(chǔ)配置做好之后,接下來我們來寫生成 PDF 的業(yè)務(wù)邏輯,創(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 實例是通過 構(gòu)造函數(shù) 注入的,接下來可以在 GeneratePdfReport() 方法中構(gòu)建生成 pdf 的具體業(yè)務(wù)邏輯。
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è)務(wù)邏輯構(gòu)建好之后,現(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 中并沒有內(nèi)置對 pdf 的支持,所以有這方面的需求只能借助于第三方框架,而 DinkToPdf 就是這么一款非常優(yōu)秀的工具包,DinkToPdf 是一款用 .NET 語言編寫的用于包裝 C++ 的 wkhtmltopdf 的工具包,它可以非常方便的將 Html 轉(zhuǎn)成 PDF ,關(guān)于更多 DinkToPdf 可參考 Github:https://github.com/rdvojmoc/DinkToPdf
譯文鏈接:https://www.infoworld.com/article/3605276/how-to-create-pdf-documents-in-aspnet-core-5.html
總結(jié)
到此這篇關(guān)于ASP.NET Core 5中如何生成PDF文檔的文章就介紹到這了,更多相關(guān)ASP.NET Core5生成PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
- ASP.NET Core自動生成小寫破折號路由的實現(xiàn)方法
- Asp.Net Core使用swagger生成api文檔的完整步驟
- 詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
- Asp.NetCore1.1版本去掉project.json后如何打包生成跨平臺包
- Asp.net core WebApi 使用Swagger生成幫助頁實例
- 基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗證token示例
- asp.net core實現(xiàn)在線生成多個文件將多個文件打包為zip返回的操作
相關(guān)文章
ASP.NET全棧開發(fā)教程之前后臺校驗結(jié)合詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET全棧開發(fā)教程之前后臺校驗結(jié)合的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
asp.net 使用js分頁實現(xiàn)異步加載數(shù)據(jù)
這篇文章主要介紹了asp.net使用js分頁實現(xiàn)異步加載數(shù)據(jù),需要的朋友可以參考下2014-04-04
iis中為每個應(yīng)用程序池單獨設(shè)置aspnet.config配置文件
ASP.NET2.0之后的版本就在各Framework的根目錄下提供了一個aspnet.config文件,這個文件用來配置全局的一些信息,但是一直以來我們都沒有怎么用過2011-12-12
WCF如何綁定netTcpBinding寄宿到控制臺應(yīng)用程序詳解
這篇文章主要給大家介紹了關(guān)于WCF如何綁定netTcpBinding寄宿到控制臺應(yīng)用程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用WCF具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
asp.net導(dǎo)出Excel顯示中文亂碼的解決方法
asp.net導(dǎo)出Excel顯示中文亂碼的解決方法,需要的朋友可以參考一下2013-03-03

