.Net 把网页Html转PDF文件
此篇主要利用 wkhtmltopdf
进行转换。
一、控制台直接转换
首先到官网http://wkhtmltopdf.org/下载wkhtmltopdf
,下后主要有3个文件,
wkhtmltoimage.exe 主要是把URL转成图片。
wkhtmltopdf.exe 主要是把 URL 转成PDF
我们只要打CMD. 输入 wkhtmltopdf http://oschina.net cc.pdf 就会在当前目前产生一个cc.pdf 文件。
二、asp.net 包装 wkhtmltopdf
项目结构
主要代码:
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace HtmltoX.Controllers { public class HomeController : Controller { public ActionResult index(string url, string type) { return View(); } [HttpPost] public ActionResult Gen(string url, string type) { if (string.IsNullOrWhiteSpace(url)) { return Content("参数异常..."); } string contentType = "application/pdf"; string ext = ".pdf"; string folder = "Pdfs"; string genExe = "wkhtmltopdf.exe "; if ("image".Equals(type, StringComparison.InvariantCultureIgnoreCase)) { contentType = "image/jpeg"; ext = ".jpg"; folder = "Images"; genExe = "wkhtmltoimage.exe "; } var rootUrl = Server.MapPath("/"); var file = rootUrl + @"AutoGen\wkhtmltoX\" + genExe; string fName = Guid.NewGuid().ToString(); string flileName = rootUrl + "AutoGen/" + folder + "/" + fName + ext; try { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = file; startInfo.Arguments = string.Format(" {0} {1}", url, flileName); startInfo.CreateNoWindow = true; startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; var cc = Process.Start(startInfo); cc.WaitForExit(); cc.Close(); } catch { return Content("生成失败. 请多试几次..."); } if (System.IO.File.Exists(flileName)) { var f=new FileStreamResult(new FileStream(flileName, FileMode.Open), contentType); f.FileDownloadName = fName+ext; return f; } return Content("生成失败. 请多试几次..."); } } }
三、 Pechkin
Pechkin主要是把 wkhtmltopdf
所用的DLL文件作了C#的封装。
Nuget地址:
https://www.nuget.org/packages/Pechkin.Synchronized/
https://www.nuget.org/packages/Pechkin/
安装:
Install-Package Pechkin
Install-Package Pechkin.Synchronized
using System; using System.Collections.Generic; using System.Drawing.Printing; using System.Linq; using System.Web; using System.Web.Mvc; using Pechkin; using Pechkin.Synchronized; namespace MvcApplication1.Controllers { public class DefaultController : Controller { // // GET: /Default/ public ActionResult Index() { var config = new GlobalConfig(); var pechkin = new SimplePechkin(config); ObjectConfig oc = new ObjectConfig(); oc.SetPrintBackground(true).SetRunJavascript(true).SetScreenMediaType(true) .SetLoadImages(true) .SetPageUri("http://oschina.net"); byte[] pdf = pechkin.Convert(oc);
return File(pdf, "application/pdf", "download.pdf"); } public ActionResult c() { SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig() .SetMargins(new Margins() {Left = 0, Right = 0, Top = 0, Bottom = 0})); //设置边距 ObjectConfig oc = new ObjectConfig(); oc.SetPrintBackground(true).SetRunJavascript(true).SetScreenMediaType(true) .SetLoadImages(true) .SetPageUri("http://oschina.net"); byte[] buf = sc.Convert(oc); return File(buf, "application/pdf", "download.pdf"); } } }