/// <summary>
/// HTML转换为PDF
/// </summary>
/// <param name="htmlPath">可以是本地路径,也可以是网络地址</param>
/// <param name="savePath">PDF文件保存的路径</param>
/// <returns></returns>
public bool HtmlConvertToPdf(string htmlPath, string savePath)
{
bool flag = false;
CheckFilePath(savePath);
///这个路径为程序集的目录,因为我把应用程序 wkhtmltopdf.exe 放在了程序集同一个目录下
string exePath = Server.MapPath("/wkhtmltopdf/bin/wkhtmltopdf.exe"); //AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf/bin/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
{
flag = false;
}
return flag;
}
/// <summary>
/// 验证保存路径
/// </summary>
/// <param name="savePath"></param>
private void CheckFilePath(string savePath)
{
string ext = string.Empty;
string path = string.Empty;
string fileName = string.Empty;
ext = Path.GetExtension(savePath);
if (string.IsNullOrEmpty(ext) || ext.ToLower() != ".pdf")
{
throw new Exception("Extension error:This method is used to generate PDF files.");
}
fileName = Path.GetFileName(savePath);
if (string.IsNullOrEmpty(fileName))
{
throw new Exception("File name is empty.");
}
try
{
path = savePath.Substring(0, savePath.IndexOf(fileName));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch
{
throw new Exception("The file path does not exist.");
}
}
/// <summary>
/// 获取命令行参数
/// </summary>
/// <param name="htmlPath"></param>
/// <param name="savePath"></param>
/// <returns></returns>
private 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 500 "); //页面高度100mm
//stringBuilder.Append(" --page-width 650 "); //页面宽度100mm
//stringBuilder.Append(" --header-center 我是页眉 "); //设置居中显示页眉
//stringBuilder.Append(" --header-line "); //页眉和内容之间显示一条直线
//stringBuilder.Append(" --footer-center \"Page [page] of [topage]\" "); //设置居中显示页脚
//stringBuilder.Append(" --footer-line "); //页脚和内容之间显示一条直线
//stringBuilder.Append(" --page-break-inside avoid "); //分页
stringBuilder.Append(" " + htmlPath + " "); //本地 HTML 的文件路径或网页 HTML 的URL地址
stringBuilder.Append(" " + savePath + " "); //生成的 PDF 文档的保存路径
return stringBuilder.ToString();
}
/// HTML转换为PDF
/// </summary>
/// <param name="htmlPath">可以是本地路径,也可以是网络地址</param>
/// <param name="savePath">PDF文件保存的路径</param>
/// <returns></returns>
public bool HtmlConvertToPdf(string htmlPath, string savePath)
{
bool flag = false;
CheckFilePath(savePath);
///这个路径为程序集的目录,因为我把应用程序 wkhtmltopdf.exe 放在了程序集同一个目录下
string exePath = Server.MapPath("/wkhtmltopdf/bin/wkhtmltopdf.exe"); //AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf/bin/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
{
flag = false;
}
return flag;
}
/// <summary>
/// 验证保存路径
/// </summary>
/// <param name="savePath"></param>
private void CheckFilePath(string savePath)
{
string ext = string.Empty;
string path = string.Empty;
string fileName = string.Empty;
ext = Path.GetExtension(savePath);
if (string.IsNullOrEmpty(ext) || ext.ToLower() != ".pdf")
{
throw new Exception("Extension error:This method is used to generate PDF files.");
}
fileName = Path.GetFileName(savePath);
if (string.IsNullOrEmpty(fileName))
{
throw new Exception("File name is empty.");
}
try
{
path = savePath.Substring(0, savePath.IndexOf(fileName));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch
{
throw new Exception("The file path does not exist.");
}
}
/// <summary>
/// 获取命令行参数
/// </summary>
/// <param name="htmlPath"></param>
/// <param name="savePath"></param>
/// <returns></returns>
private 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 500 "); //页面高度100mm
//stringBuilder.Append(" --page-width 650 "); //页面宽度100mm
//stringBuilder.Append(" --header-center 我是页眉 "); //设置居中显示页眉
//stringBuilder.Append(" --header-line "); //页眉和内容之间显示一条直线
//stringBuilder.Append(" --footer-center \"Page [page] of [topage]\" "); //设置居中显示页脚
//stringBuilder.Append(" --footer-line "); //页脚和内容之间显示一条直线
//stringBuilder.Append(" --page-break-inside avoid "); //分页
stringBuilder.Append(" " + htmlPath + " "); //本地 HTML 的文件路径或网页 HTML 的URL地址
stringBuilder.Append(" " + savePath + " "); //生成的 PDF 文档的保存路径
return stringBuilder.ToString();
}