c# html转pdf

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhb1993829/article/details/80401349
    最近业务上需要一个html转pdf的功能,用了一天时间终于找到一个还可以的。找过的插件有wkhtmltopdf、pechkin、itextsharp等都没能满足我的业务需要。最后终于找到一款比较适合的。

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();
}
展开阅读全文

没有更多推荐了,返回首页