一、WkHtmlToPdfDotNet介绍
WkHtmlToPdfDotNet是基于本地 wkhtmltopdf封装的.NET Core类库,主要通过webkit引擎实现html页面转换为pdf文件。并且支持在Windows、Docker、Linux、MacOSX运行。
主要功能实现在线URL转pdf文件或者html代码直接转换为pdf文件,并对css样式、图片等很好的支持,导出的pdf文件和网站相差不大。
今天给大家通过实际的案例给大家介绍一下如何使用它,感兴趣的朋友可以一起来学习一下!
官网:https://github.com/HakanL/WkHtmlToPdf-DotNet
二、安装
1、使用Visual Studio 2019及以上版本创建一个.NET Core控制台程序。
2、安装WkHtmlToPdfDotNet依赖包
命令方式安装
工具→NuGet包管理器→程序包管理器控制包
安装命令如下:
Install-Package Haukcode.WkHtmlToPdfDotNet
- 1.
直接通过Nuget包搜索安装
工具→NuGet包管理器→管理解决方案的NuGet程序包
3、实现代码
using System;using System.IO;using WkHtmlToPdfDotNet;namespace HtmlToPdf{ class Program { // https://github.com/HakanL/WkHtmlToPdf-DotNet // 安装依赖包 Install-Package Haukcode.WkHtmlToPdfDotNet
static void Main(string[] args) { //ConvertHtmlToPdf(); ConvertUrlToPdf(); } /// <summary> /// 基于html导出pdf /// </summary>private static void ConvertHtmlToPdf() { var converter = new BasicConverter(new PdfTools()); // 绑定转换过程中处理事件日志 converter.PhaseChanged += Converter_PhaseChanged; converter.ProgressChanged += Converter_ProgressChanged; converter.Finished += Converter_Finished; converter.Warning += Converter_Warning; converter.Error += Converter_Error; // pdf 样式设置 var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A4, }, Objects = { new ObjectSettings() { PagesCount = true, HtmlContent = @" <p style='color:red'>hello</p><h2>测试</h2><img src='https://www.baidu.com/img/pc_675fe66eab33abff35a2669768c43d95.png' alt=''>", WebSettings = { DefaultEncoding = "utf-8" }, HeaderSettings = { FontSize = 9, Right = "Page [page] of [toPage]", Line = false }, FooterSettings = { FontSize = 9, Right = "Page [page] of [toPage]" } } } }; // 转换为二进制 byte[] pdf = converter.Convert(doc); // 判断目录是否存在,不存在则创建 if (!Directory.Exists("Files")) { Directory.CreateDirectory("Files"); } // 文件保存 using (var stream = new FileStream(Path.Combine("Files", DateTime.UtcNow.Ticks.ToString() + ".pdf"), FileMode.Create)) { stream.Write(pdf, 0, pdf.Length); } }
/// <summary> /// 基于url导出pdf /// </summary>private static void ConvertUrlToPdf() {
var converter = new BasicConverter(new PdfTools()); // 绑定转换过程中处理事件日志 converter.PhaseChanged += Converter_PhaseChanged; converter.ProgressChanged += Converter_ProgressChanged; converter.Finished += Converter_Finished; converter.Warning += Converter_Warning; converter.Error += Converter_Error; // pdf 样式设置 var doc = new HtmlToPdfDocument() { GlobalSettings = { PaperSize = PaperKind.A3, // 纸张类型 Orientation = Orientation.Landscape, }, // 支持多个网址 Objects = { new ObjectSettings() { Page = "http://baidu.com/", }, new ObjectSettings() { Page = "https://www.163.com/dy/article/HJVSIG920511DTU9.html?clickfrom=w_yw_zgzz",
} } };
// 转换为二进制 byte[] pdf = converter.Convert(doc); // 判断目录是否存在,不存在则创建 if (!Directory.Exists("FilesHtml")) { Directory.CreateDirectory("FilesHtml"); } // 文件保存 using (var stream = new FileStream(Path.Combine("FilesHtml", DateTime.UtcNow.Ticks.ToString() + ".pdf"), FileMode.Create)) { stream.Write(pdf, 0, pdf.Length); } }
/// <summary> /// 转换产生错误日志输出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>private static void Converter_Error(object sender, WkHtmlToPdfDotNet.EventDefinitions.ErrorArgs e) { Console.WriteLine("[转换错误] {0}", e.Message); } /// <summary> /// 转换产生警告日志输出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>
private static void Converter_Warning(object sender, WkHtmlToPdfDotNet.EventDefinitions.WarningArgs e) { Console.WriteLine("[警告] {0}", e.Message); } /// <summary> /// 转转完成日志输出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>private static void Converter_Finished(object sender, WkHtmlToPdfDotNet.EventDefinitions.FinishedArgs e) { Console.WriteLine("转换 {0} ", e.Success ? "成功" : "失败"); } /// <summary> /// 转换进度日志输出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>private static void Converter_ProgressChanged(object sender, WkHtmlToPdfDotNet.EventDefinitions.ProgressChangedArgs e) { Console.WriteLine("转换进度 {0}", e.Description); } /// <summary> /// 转换阶段日志输入 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>private static void Converter_PhaseChanged(object sender, WkHtmlToPdfDotNet.EventDefinitions.PhaseChangedArgs e) { Console.WriteLine("阶段进度 {0} - {1}", e.CurrentPhase, e.Description); }
}}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
三、运行效果
基于在线url方式导出效果:
直接拼接html方式导出效果: