分享一个快速将url页面转成pdf文件的工具wkhtmltopdf
我们需要回答三个问题
什么是wkhtmltopdf?
官网地址:https://wkhtmltopdf.org/
根据官网上的描述,wkhtmltopdf是开放源代码(LGPLv3)命令行工具,可使用Qt WebKit渲染引擎将HTML渲染为PDF。它们完全“无头”运行,不需要显示或显示服务。
有什么用?
可以将HTML渲染为pdf或者经url指向的html页面转为pdf
怎么使用?
1. 安装wkhtmltopdf
- 官网下载,进行安装。这里不做过多说明。我安装在D盘。
- 配置环境变量:在环境变量”Path”的后面添加:;D:\wkhtmltopdf\bin 也就是你安装的目录。安装好以后重启电脑。
- 测试效果:打开cmd。输入:wkhtmltopdf http://www.baidu.com/ D:website1.pdf 测试D盘下是否出现百度内容的pdf
2. C#操作wkhtmltopdf将一个url页面转为pdf
软件:vs2015
运行环境:win10
我们直接看代码:
/// <summary>
/// wkhtmltopdf 将url页面转为pdf文件
/// </summary>
/// <param name="htmlPath">url路径</param>
/// <param name="savePath">pdf文件保存的地址</param>
/// <returns></returns>
public static bool HtmlConvertToPdf(string htmlPath, string savePath)
{
bool flag = false;
///这个路径为程序集的目录,因为我把应用程序 wkhtmltopdf.exe 放在了程序集同一个目录下
string exePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf.exe";
if (!File.Exists(exePath))
{
throw new Exception("No application wkhtmltopdf.exe was found.");
}
try
{
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = exePath;//应用程序的位置
processStartInfo.WorkingDirectory = Path.GetDirectoryName(exePath);
processStartInfo.UseShellExecute = false;
processStartInfo.CreateNoWindow = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.Arguments = GetArguments(htmlPath, savePath);//关键:要执行的命令行参数
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
process.WaitForExit();
//用于查看是否返回错误信息
StreamReader srone = process.StandardError;
StreamReader srtwo = process.StandardOutput;
string ss1 = srone.ReadToEnd();
string ss2 = srtwo.ReadToEnd();
srone.Close();
srone.Dispose();
srtwo.Close();
srtwo.Dispose();
process.Close();
process.Dispose();
flag = true;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
flag = false;
}
return flag;
}
/// <summary>
/// 获取命令行参数
/// </summary>
/// <param name="htmlPath"></param>
/// <param name="savePath"></param>
/// <returns></returns>
private static string GetArguments(string htmlPath, string savePath)
{
if (string.IsNullOrEmpty(htmlPath))
{
throw new Exception("HTML local path or network address can not be empty.");
}
if (string.IsNullOrEmpty(savePath))
{
throw new Exception("The path saved by the PDF document can not be empty.");
}
StringBuilder stringBuilder = new StringBuilder();
//stringBuilder.Append(" --page-height 100 "); //页面高度100mm
//stringBuilder.Append(" --page-width 100 "); //页面宽度100mm
stringBuilder.Append(" --header-center 我是页眉 "); //设置居中显示页眉
stringBuilder.Append(" --header-line "); //页眉和内容之间显示一条直线
stringBuilder.Append(" --footer-center \"Page [page] of [topage]\" "); //设置居中显示页脚
stringBuilder.Append(" --footer-line "); //页脚和内容之间显示一条直线
//stringBuilder.Append(" --javascript-delay 20000 ");//增加延迟20秒,用于等待页面js加载完成
stringBuilder.Append(" " + htmlPath + " "); //本地 HTML 的文件路径或网页 HTML 的URL地址
stringBuilder.Append(" " + savePath + " "); //生成的 PDF 文档的保存路径
//stringBuilder.Append(" --javascript-delay <msec> 2000 ");
return stringBuilder.ToString();
}
【参考文档】
完整内容参考:https://www.cnblogs.com/xiangxiong/p/7645679.html
关于在使用wkhtml中遇到的坑及其解决方法:
https://blog.csdn.net/qq_39541254/article/details/107541497