(转)Dinktopdf在.net core项目里将Html转成PDF
Dinktopdf : .Net Core对 wkhtmltopdf 库的封装, 使用Webkit引擎将html转换成pdf.
源码地址: https://github.com/rdvojmoc/DinkToPdf
使用比较简单,直接把github里的示例代码放到你的.net core项目里。
注意:要记得把libwkhtmltox库放到项目的根目录里,并在visual studio里设置“如果较新则复制“
dll是window, so是linux, dylib应该是mac os
如果从github下载慢,你也可以从码云网站下载
https://gitee.com/ofri/DinkToPdf
注: linux或docker容器需要安装 libgdiplus 否则会提示找不到libwkhtmltox
apt-get update
apt-get install libgdiplus
在Startup.cs中添加:
1 | services.AddSingleton( typeof (IConverter), new SynchronizedConverter( new PdfTools())); //DinkToPdf注入 |
创建IPDFService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System; namespace HtmlToPdf.Services { /// <summary> /// 与pdf相关 /// </summary> public interface IPDFService { /// <summary> /// 创建PDF /// </summary> /// <param name="htmlContent">传入html字符串</param> /// <returns></returns> byte [] CreatePDF( string htmlContent); } } |
创建PDFService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using System; using DinkToPdf; using DinkToPdf.Contracts; namespace HtmlToPdf.Services { /// <summary> /// 与pdf相关 /// </summary> public class PDFService : IPDFService { private IConverter _converter; public PDFService(IConverter converter) { _converter = converter; } /// <summary> /// 创建PDF /// </summary> /// <param name="htmlContent">传入html字符串</param> /// <returns></returns> public byte [] CreatePDF( string htmlContent) { var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, //Margins = new MarginSettings //{ // Top = 10, // Left = 0, // Right = 0, //}, DocumentTitle = "PDF Report" , }; var objectSettings = new ObjectSettings { PagesCount = true , HtmlContent = htmlContent, // Page = "www.baidu.com", //USE THIS PROPERTY TO GENERATE PDF CONTENT FROM AN HTML PAGE 这里是用现有的网页生成PDF //WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "styles.css") }, WebSettings = { DefaultEncoding = "utf-8" }, //HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }, //FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" } }; var pdf = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings } }; var file = _converter.Convert(pdf); //return File(file, "application/pdf"); return file; } } } |
在Startup.cs中依赖注入:
1 | services.AddTransient<IPDFService, PDFService>(); |
创建TemplateGenerator,生成html字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System; using System.Text; namespace HtmlToPdf { public static class TemplateGenerator { /// <summary> /// 获取HTML字符串 /// </summary> /// <returns></returns> public static string GetPDFHTMLString() { StringBuilder sb = new StringBuilder(); sb.Append( @" <html> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <style> </style> </head> <body> <div> 这是一个网页! </div> </body> </html> " ); return sb.ToString(); } } } |
修改ValuesController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using HtmlToPdf.Services; using Microsoft.AspNetCore.Mvc; namespace HtmlToPdf.Controllers { [Route( "api/[controller]" )] [ApiController] public class ValuesController : ControllerBase { private IPDFService _PDFService; public ValuesController(IPDFService pDFService) { _PDFService = pDFService; } [HttpGet( "pdf" )] public FileResult GetPDF() { //获取html模板 var htmlContent = TemplateGenerator.GetPDFHTMLString(); //生成PDF var pdfBytes = _PDFService.CreatePDF(htmlContent); return File(pdfBytes, "application/pdf" ); } } } |
转载:http://t.zoukankan.com/tangge-p-14582341.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?