protected void Page_Load(object sender, EventArgs e)
{
string RootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
string url = RootDir + "hh.html";
HtmlToPdf(url, "c:/ddd.pdf");
}
/// <summary>
/// HTML生成PDF
/// </summary>
/// <param name="url">地址</param>
/// <param name="path">PDF存放路径</param>
public static bool HtmlToPdf(string url, string path)
{
try
{
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
return false;
Process p = new Process();
string str = System.Web.HttpContext.Current.Server.MapPath("wkhtmltopdf.exe");
if (!System.IO.File.Exists(str))
return false;
p.StartInfo.FileName = str;
p.StartInfo.Arguments = " \"" + url + "\" " + path;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
System.Threading.Thread.Sleep(500);
return true;
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex);
}
return false;
}
需要运行exe文件
参考:
http://www.jingzhengli.cn/blog/cj/1030.html