本篇文章给大家分享的是有关怎么在ASP.NET项目中生成一个PDF文档,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
要想安装 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 上下载,然后根据你的需要选择 32bit 还是 64bit 。
要想在 ASP.NET Core 中使用,需要在 ConfigureServices() 方法下将 DinkToPdf 注入到 IOC 容器中,下面的代码展示了如何去实现。
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(IConverter),new SynchronizedConverter(new PdfTools()));
services.AddControllers();
}
基础配置做好之后,接下来我们来写生成 PDF 的业务逻辑,创建一个 IReportService 和 ReportService 实现类,代码如下:
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 实例是通过 构造函数 注入的,接下来可以在 GeneratePdfReport() 方法中构建生成 pdf 的具体业务逻辑。
public byte[] GeneratePdfReport()
{
var html = $@"<!DOCTYPE html>
<html lang=""en"">
<head>
This is the header of this document.
</head>
<body>
<h2>This is the heading for demonstration purposes only.</h2>
<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>();
GeneratePdfReport() 方法的业务逻辑构建好之后,现在可以将 IReportService 实例注入到 ReportController 中来最终渲染 pdf,下面的代码展示了如何去实现。
[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项目中生成一个PDF文档,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。