C#将html转pdf
2016-04-01 09:51
459 查看
publicstringHtmlToPdf(stringurl) { boolsuccess=true; //stringdwbh=url.Split('?')[1].Split('=')[1]; //CommonBllHelper.CreateUserDir(dwbh); //url=Request.Url.Host+"/html/"+url; stringguid=DateTime.Now.ToString("yyyyMMddhhmmss"); stringpdfName="1.pdf"; //stringpath=Server.MapPath("~/kehu/"+dwbh+"/pdf/")+pdfName; stringpath="D:\\"+pdfName; try { if(string.IsNullOrEmpty(url)||string.IsNullOrEmpty(path)) success=false; stringstr=Server.MapPath("~\\bin\\wkhtmltopdf.exe"); Processp=System.Diagnostics.Process.Start(str,url+""+path); p.WaitForExit(); if(!System.IO.File.Exists(str)) success=false; if(System.IO.File.Exists(path)) { FileStreamfs=newFileStream(path,FileMode.Open); byte[]bytes=newbyte[(int)fs.Length]; fs.Read(bytes,0,bytes.Length); fs.Close(); if(Request.UserAgent!=null) { stringuserAgent=Request.UserAgent.ToUpper(); if(userAgent.IndexOf("FIREFOX",StringComparison.Ordinal)<=0) { Response.AddHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlEncode(pdfName,Encoding.UTF8)); } else { Response.AddHeader("Content-Disposition","attachment;filename="+pdfName); } } Response.ContentEncoding=Encoding.UTF8; Response.ContentType="application/octet-stream"; //通知浏览器下载文件而不是打开 Response.BinaryWrite(bytes); Response.Flush(); Response.End(); fs.Close(); System.IO.File.Delete(path); } else { Response.Write("文件未找到,可能已经被删除"); Response.Flush(); Response.End(); } } catch(Exceptionex) { success=false; } return""; }
{
HtmlToPdf("http://www.deriva.cn");
}
相关文章推荐
- 动态HTML和W3C文档对象模型
- 在HTML中URL、src、href分别代表什么?如何使用?
- HTML学习笔记(七)<div>与<span>
- HTML简介
- HTML <td> 标签的 nowrap 属性
- HTML中cellSpace 和 cellPadding
- html学习笔记3
- HTML基础语法(2)
- HTML 作业
- PHP代码为什么不能直接保存HTML文件——>PHP生成静态页面教程
- html基础
- html中dispaly 与 visibility的区别
- html 输入框显示“小叉叉”的清空图标
- 函数放到onload里面,在html里面执行函数会报错-----作用域和闭包相关问题
- html canvas 简单体验
- HTML学习笔记(六)列表
- xhtml与html的区别
- htm、html、shtml区别
- [Web开发] 在HTML代码里面如何判断IE版本
- 【原创】C# 多线程采集工具(使用 HtmlAgilityPack 工具)