将页面转化为pdf的实现方法

1.实现代码把html转化为pdf主要是使用wkhtmltopdf.exe工具生成,在获取转化的地址,创建一个进程,把地址传递到进程参数中进行调用wkhtmltopdf.exe工具打印

2.代码片段//调用工具部分

/// <summary>         /// 启动Wkhtmltopdf         /// </summary>         /// <param name="parms">启动参数</param>         /// <returns></returns>         public void StartWK(string parms, bool IsNoHead = true)         {             try             {                 Process proc = new Process();                 string resource = HttpContext.Current.Server.MapPath("~/Content/wkhtmltopdf/bin");                 string dllstr = string.Format(resource + "\\wkhtmltopdf.exe");

if (System.IO.File.Exists(dllstr))                 {                     proc.StartInfo.FileName = dllstr;                     string strArguments = IsNoHead ? string.Empty : " --margin-top 20 --margin-bottom 20 --header-spacing 2 --footer-spacing 1 --footer-font-size 7 --footer-center \"第[page]页/共[topage]页\" ";                     strArguments += parms;

proc.StartInfo.Arguments = strArguments;                     proc.StartInfo.UseShellExecute = false;                     proc.StartInfo.RedirectStandardInput = true;                     proc.StartInfo.RedirectStandardOutput = true;                     proc.StartInfo.RedirectStandardError = true;                     proc.StartInfo.CreateNoWindow = true;                     proc.Start();                     proc.WaitForExit();                 }                 else                 {                     throw new Exception("wkhtmltopdf.exe启动异常!");                 }             }             catch (Exception)             {                 throw;             }         }

上一篇:data语意学


下一篇:将caj文件转化为pdf文件进行全文下载脚本(ubuntu下亲测有用)