最近业务上需要一个html转pdf的功能,用了一天时间终于找到一个还可以的。找过的插件有
wkhtmltopdf、pechkin、itextsharp等都没能满足我的业务需要。最后终于找到一款比较适合的。
nuget地址:https://www.nuget.org/packages/OpenHtmlToPdf/
OpenHtmlToPdf,这是一款根据wkhtmltopdf二次开发的插件。
项目地址:https://github.com/vilppu/OpenHtmlToPdf
nuget地址:https://www.nuget.org/packages/OpenHtmlToPdf/
using (WebClient wc = new WebClient()) { wc.Encoding = Encoding.UTF8; string html = wc.DownloadString(url); var document = Pdf.From(html) .OfSize(OpenHtmlToPdf.PaperSize.LetterRotated) .WithGlobalSetting("margin.top", "0.4cm"); if (IntPtr.Size == 4) { document = document.WithObjectSetting("load.zoomFactor", "1.5"); } var result = document.Content(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=PDF" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.BinaryWrite(result); HttpContext.Current.Response.End(); }