将 html 转换为 pdf 是一种常见的需求。这种转换方式能够帮助用户轻松保存、共享和打印网页内容,同时确保文档的格式和布局始终保持一致。通过将网页、报告或文章等内容转换为 pdf 格式,用户可以创建便携式文档,方便在不同设备和平台上查看和传播,有效提升信息的可读性和实用性。
spire.pdf for .net 提供了一种新的方法,支持通过 chromehtmlconverter 将 html 转换为 pdf。本文将对该方法进行详细的介绍。
安装 spire.pdf for .net
首先,您需要添加 spire.pdf for .net 包中包含的 dll 文件作为 .net 项目中的引用。dll 文件可以从此链接下载或通过 安装。
pm> install-package spire.pdf
安装 chrome
此方法需要用到 chrome.exe 进行转换。如果您的系统尚未安装 chrome,可以通过下载安装。
使用 chromehtmlconverter 将 html 转换为 pdf
你可以使用 chromehtmlconverter.converttopdf() 方法通过 chrome 插件将 html 文件转换为 pdf。此方法接受三个参数:输入 html 地址,输出 pdf 文件路径,以及用于自定义转换设置(例如转换超时时间、pdf 纸张大小和页边距)的 convertoptions 对象。具体步骤如下:
- 创建 chromehtmlconverter 类的实例,并在构造函数中指定 chrome 插件(chrome.exe)的路径。
- 创建 convertoptions 类的实例。
- 通过 convertoptions 类的属性自定义转换设置,例如超时时间、纸张大小和页边距。
- 使用 chromehtmlconverter.converttopdf() 方法将 html 转换为 pdf。
- c#
using spire.additions.chrome;
namespace converthtmltopdfusingchrome
{
internal class program
{
static void main(string[] args)
{
// 指定输入 url 和输出 pdf 文件路径
string inputurl = @"https://www.e-iceblue.cn/conversion/c-convert-pdf-to-images-jpg-png-bmp.html";
string outputfile = @"html转pdf.pdf";
// 指定 chrome 插件路径
string chromelocation = @"c:\program files\google\chrome\application\chrome.exe";
// 创建 chromehtmlconverter 类的实例
chromehtmlconverter converter = new chromehtmlconverter(chromelocation);
// 创建 convertoptions 类的实例
convertoptions options = new convertoptions();
// 设置转换超时时间
options.timeout = 10 * 3000;
// 设置 pdf 的纸张大小和页边距
options.pagesettings = new pagesettings()
{
paperwidth = 8.27,
paperheight = 11.69,
margintop = 0,
marginleft = 0,
marginright = 0,
marginbottom = 0
};
// 将 html 转换为 pdf
converter.converttopdf(inputurl, outputfile, options);
}
}
}
在 html 到 pdf 转换过程中生成日志文件
spire.pdf for .net 提供了生成日志的功能,方便用户在 html 到 pdf 转换过程中记录相关信息。具体步骤如下:
- 创建 chromehtmlconverter 类的实例,并在构造函数中指定 chrome 插件(chrome.exe)的路径。
- 创建 logger 类的实例,然后将其赋值给 chromehtmlconverter.logger 属性来启用日志功能。
- 创建 convertoptions 类的实例。
- 通过 convertoptions 类的属性自定义转换设置,例如超时时间、纸张大小和页边距。
- 使用 chromehtmlconverter.converttopdf() 方法将 html 转换为 pdf。
- c#
using spire.additions.chrome;
namespace converthtmltopdfusingchrome
{
internal class program
{
static void main(string[] args)
{
// 指定输入 url 和输出 pdf 文件路径
string inputurl = @"https://www.e-iceblue.cn/conversion/c-convert-pdf-to-images-jpg-png-bmp.html";
string outputfile = @"html转pdf.pdf";
// 指定日志文件路径
string logfilepath = @"日志.txt";
// 指定 chrome 插件路径
string chromelocation = @"c:\program files\google\chrome\application\chrome.exe";
// 创建 chromehtmlconverter 类的实例
chromehtmlconverter converter = new chromehtmlconverter(chromelocation);
// 启用日志记录
converter.logger = new logger(logfilepath);
// 创建 convertoptions 类的实例
convertoptions options = new convertoptions();
// 设置转换超时时间
options.timeout = 10 * 3000;
// 设置 pdf 的纸张大小和页边距
options.pagesettings = new pagesettings()
{
paperwidth = 8.27,
paperheight = 11.69,
margintop = 0,
marginleft = 0,
marginright = 0,
marginbottom = 0
};
// 将 html 转换为 pdf
converter.converttopdf(inputurl, outputfile, options);
}
}
}
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。