使用C#代码 HTML 转为 PDF 是常见需求,生成可打印文档或离线保存网页内容。Spire.PDF for .NET 的出现,使得开发者可以通过多种方式实现网页(HTML/URL)到PDF格式的转换。用户可以选择简单调用 QT 插件实现快速转换,或者使用 ChromeHtmlConverter 获取高保真输出。不管用户选择哪种方式,Spire.PDF 都能提供灵活便捷的解决方案。
下表是关于本文两种方案的对比,你可以根据自己的实际情况直接做出选择并跳转到对应文章板块:
方法 | 适合对象 | 特点 |
---|---|---|
使用 QT 插件将 HTML/URL 转为 PDF | 追求快速、简单、高效率代码转换的开发者 | 支持登录会话,代码精简,转换速度快 |
使用 ChromeHtmlConverter转换并输出日志 | 需要高保真转换并输出转换日志的用户 | 像浏览器一样渲染动态内容,提供详细日志 |
方法一:使用 QT 插件进行转换
在正式引导你使用代码实现HTML文件PDF格式转换之前,你需要先了解我们今天使用的工具——Spire.PDF for .NET。这是一个功能强大的C#代码库,能轻松协助用户在PDF文件上添加数字签名、合并或拆分PDF文件,以及从PDF文件中提取文本/图片等功能。
推荐理由:
- 可将 PDF 压缩至原来的 1/10–1/100 大小
- 与大多数 C# 环境兼容
- 提供加密机制保护文件隐私,用户可通过设置密码等方式来保护PDF文档
- 更多精彩功能等待探索……
操作步骤:
步骤 1. 从官方下载页下载 Spire.PDF for .NET 并添加至对应的C#项目当中。
小贴士:您可该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。申请30天的免费试用权限以解锁该代码库的全部功能。
步骤 2.从以下对应链接中下载适合操作系统的 QT 插件(Windows x86/x64、Linux、macOS)。
步骤 3.解压插件,可看到 “plugin” 文件夹。在该演示中,我们将其放在 “F:\Libraries\Plugin\plugins-windows-x64\plugins” 文件夹下。
同时,建议将项目的“目标平台”设置为对应的 x86 或 x64。
步骤 4.在代码中设置插件路径,并调用 API 进行转换:
如果您存有网页原HTML文件,复制以下代码到对应的C#项目中并运行即可:
using System.IO;using Spire.Additions.Qt;using System.Drawing;using Spire.Pdf.Graphics;namespace ConvertHtmlStringToPdfWithPlugin{class Program{static void Main(string[] args){//Get the HTML string from a .html file string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");//Specify the output file path string fileName = "HtmlStringToPdf.pdf";//Specify the plugin path string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";//Set plugin path HtmlConverter.PluginPath = pluginPath;//Convert HTML string to PDF HtmlConverter.Convert(htmlString,fileName,true,100000,new Size(1080,1000),new PdfMargins(0),LoadHtmlType.SourceCode);}}}
如果您只有对应的URL链接,可复制以下代码到对应的C#项目中运行并实现网页到PDF格式的转换:
using Spire.Pdf.Graphics;using Spire.Additions.Qt;using System.Drawing;namespace ConvertUrlToPdf{class Program{static void Main(string[] args){//Specify the URL path string url = "https://www.wikipedia.org/";//Specify the output file path string fileName = "UrlToPdf.pdf";//Specify the plugin path string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";//Set the plugin path HtmlConverter.PluginPath = pluginPath;//Convert URL to PDF HtmlConverter.Convert(url,fileName,true,100000,new Size(1080,1000),new PdfMargins(0));}}}
效果图:
方法二、使用 ChromeHtmlConverter 并输出日志
与仅依赖引擎默认设置或基础库的标准转换方法不同,ChromeHtmlConverter 利用无头 Chrome 浏览器的强大功能,将 HTML 内容呈现得与真实浏览器环境中一致。它还提供输出日志,对于诊断渲染问题、监控转换性能或在生产环境中保持合规性至关重要。
操作示例代码如下:
using Spire.Additions.Chrome;namespace ConvertHtmlToPdfUsingChrome{internal class Program{static void Main(string[] args){//Specify the input URL and output PDF file path string inputUrl = @"https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/C-/VB.NET-Convert-Image-to-PDF.html";string outputFile = @"HtmlToPDF.pdf";// Specify the log file path string logFilePath = @"Logs.txt";//Specify the path to the Chrome plugin string chromeLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe";//Create an instance of the ChromeHtmlConverter class ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation);//Enable logging converter.Logger = new Logger(logFilePath);//Create an instance of the ConvertOptions class ConvertOptions options = new ConvertOptions();//Set conversion timeout options.Timeout = 10 * 3000;//Set paper size and page margins of the converted PDF options.PageSettings = new PageSettings(){PaperWidth = 8.27,PaperHeight = 11.69,MarginTop = 0,MarginLeft = 0,MarginRight = 0,MarginBottom = 0};//Convert the URL to PDF converter.ConvertToPdf(inputUrl,outputFile,options);}}}
转换效果及日志效果如图所示:
如您想获取更多细节内容,可阅读下文:
C# 使用 ChromeHtmlConverter 将 HTML 转换为 PDF
总结
Spire.PDF for .NET提供多种工具,可在 C# 中将 HTML 或 URL 转换为 PDF,无论是处理本地 HTML 文件还是远程网页。根据项目需求(如渲染复杂度、输出质量或依赖偏好),您可以选择最合适的转换方式。