C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
最近欣赏文章的时候发明DinkToPdf框架,可以操作HTML转换成PDF,与我早期利用ITextSharp 框架构建PDF的方法不太一样。DinkToPdf直接将HTML转成PDF,HTML的结构直观且简朴。这种方法大概可以成为不错的选择!
下面为各人简朴先容DinkToPdf的利用方法。
Github https://github.com/rdvojmoc/DinkToPdf
2.焦点内容结构HtmlToPdfDocument工具Html文本形式
string html = @"<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>W3Cschool</title> </head> <body> <h4>Html文本</h4> </body> </html> var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A4, }, Objects = { new ObjectSettings() { PagesCount = true, HtmlContent = htmlContent, WebSettings = { DefaultEncoding = "utf-8" }, HeaderSettings = { FontSize = 9, Right = "Page [ page ] of [ toPage ]", Line = true }, FooterSettings = { FontSize = 9, Right = "Page [ page ] of [ toPage ]" } } } };
URL方法
string url = ""https://www.baidu.com/"; var doc = new HtmlToPdfDocument() { GlobalSettings = { PaperSize = PaperKind.A4Small }, Objects = { new ObjectSettings() { Page = url, } } };
HtmlToPdfDocument 转成 PDF
var converter = new BasicConverter(new PdfTools()); byte[] pdf = converter.Convert(doc);
生成PDF
if (!Directory.Exists("Files")) { Directory.CreateDirectory("Files"); } using (FileStream stream = new FileStream(@"Files\" + DateTime.UtcNow.Ticks.ToString() + ".pdf", FileMode.Create)) { stream.Write(pdf, 0, pdf.Length); }
留意点
框架依赖于wkhtmltopdf,在本身陈设的时候假如提示缺少dll,记得去官方Git上下载(样例源码已经复制了64位的dll)
https://github.com/rdvojmoc/DinkToPdf/tree/master/v0.12.4
3.结果图展示Html文本形式w3c表格截图
pdf生成功效图
URL方法百度网址生成pdf结果图
4.样例源码地点https://github.com/Impartsoft/Bins/tree/main/DinkToPdfDemo
到此这篇关于.NET利用DinkToPdf将HTML转成PDF的示例代码的文章就先容到这了,更多相关.NET HTML转成PDF内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!
您大概感乐趣的文章: