之前实现了html直接转换为word文档的功能,那么是否也同样可以直接转换为pdf文档呢,网上搜了下html to pdf 的开源插件有很多 如:wkhtmltopdf,pdfsharp,itextsharp等
本文使用itextsharp实现如何将html文件转换为pdf文档
首先使用Nuget安装itextsharp插件
-
Install-Package itextsharp.xmlworker
创建FileContentResult文件继承自ActionResult,方法HtmlToPdf中实现了如何将一段html转换为pdf文档逻辑,itextsharp.xmlworker能够支持丰富的css和html标签,但是有一个很大的缺点就是不支持中文,网上的一些解决中文字体的逻辑,在新版里面已经不支持了,在以下的示例代码中已经解决此问题,重点是以下两部代码:
FontFactory.RegisterDirectories();//注册当前系统中所支持的字体
worker.ParseXHtml(pdfWriter, document, new MemoryStream(Encoding.UTF8.GetBytes(sbHtml.ToString())), null, Encoding.UTF8, new UnicodeFontFactory()); //指定要使用的字体
-
public class PdfContentResult : ActionResult
-
{
-
public PdfContentResult() : this(null, null) { }
-
-
public PdfContentResult(string viewName) : this(null, viewName) { }
-
-
public PdfContentResult(object model) : this(model, null) { }
-
-
public PdfContentResult(object model, string viewName)
-
{
-
this.ViewName = viewName;
-
ViewData = null != model ? new ViewDataDictionary(model) : null;
-
}
-
-
public ViewDataDictionary ViewData { get; set; } = new ViewDataDictionary();
-
-
public string ViewName { get; set; }
-
-
public IView View { get; set; }
-
-
public override void ExecuteResult(ControllerContext context)
-
{
-
if (String.IsNullOrEmpty(ViewName))
-
{
-
ViewName = context.RouteData.GetRequiredString("action");
-
}
-
if (ViewData == null)
-
{
-
ViewData = context.Controller.ViewData;
-
}
-
ViewEngineResult result = ViewEngines.Engines.FindView(context, ViewName, null);
-
View = result.View;
-
-
StringBuilder sbHtml = new StringBuilder();
-
TextWriter txtWriter = new StringWriter(sbHtml);
-
ViewContext viewContext = new ViewContext(context, View, ViewData, context.Controller.TempData, txtWriter);
-
result.View.Render(viewContext, txtWriter);
-
-
HttpResponseBase httpResponse = context.HttpContext.Response;
-
httpResponse.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
-
-
//加入此头部文件会直接下载pdf文件,而不是在浏览器中预览呈现
-
//context.HttpContext.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pdf", ViewName));
-
-
HtmlToPdf(sbHtml, httpResponse);
-
-
result.ViewEngine.ReleaseView(context, View);
-
}
-
-
private static void HtmlToPdf(StringBuilder sbHtml, HttpResponseBase httpResponse)
-
{
-
using (Document document = new Document(PageSize.A4, 4, 4, 4, 4))
-
{
-
using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, httpResponse.OutputStream))
-
{
-
document.Open();
-
FontFactory.RegisterDirectories();//注册系统中所支持的字体
-
XMLWorkerHelper worker = XMLWorkerHelper.GetInstance();
-
//UnicodeFontFactory 自定义实现解决itextsharp.xmlworker 不支持中文的问题
-
worker.ParseXHtml(pdfWriter, document, new MemoryStream(Encoding.UTF8.GetBytes(sbHtml.ToString())), null, Encoding.UTF8, new UnicodeFontFactory());
-
document.Close();
-
}
-
}
-
}
-
}
UnicodeFontFactory完整代码
-
public class UnicodeFontFactory : FontFactoryImp
-
{
-
static UnicodeFontFactory()
-
{
-
-
}
-
public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
-
{
-
return FontFactory.GetFont("arial unicode ms", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
-
}
-
}
如何确定哪些字体在itextsharp中是支持中文的呢,可以通过下面这个小程序验证输出所有的字体名称,及是否支持中文
通过控制台应用程序执行完成后,打开生成的pdf文件,查看 字体名称是否有中文 " 我支持中文" ,如果存在则表示支持中文,否则不支持中文
-
Document document = new Document();
-
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"c:\pdf\pdf.pdf", FileMode.Create));
-
document.Open();
-
-
FontFactory.RegisterDirectories();
-
-
foreach (var item in FontFactory.RegisteredFonts)
-
{
-
Font font = FontFactory.GetFont(item, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
-
document.Add(new Paragraph(item + "<p>我支持中文</p>", font));
-
}
-
document.Close();
上面说了如何转换html为pdf及怎么解决中文字体的问题,那么怎么使用定义的PdfContentResult呢,
使用方式一:直接在控制器的Action方法中返回PdfContentResult实例
-
public class PdfController : Controller
-
{
-
// GET: Pdf
-
public ActionResult Index()
-
{
-
return new PdfContentResult(null,"index");
-
}
-
}
使用方式二:添加Controller类的拓展方法,然后在控制器的Action方法中返回对应的拓展方法
-
public static class ControllerExtensions
-
{
-
public static PdfContentResult Pdf(this Controller controller, object model)
-
{
-
return new PdfContentResult(model);
-
}
-
-
public static PdfContentResult Pdf(this Controller controller, object model, string fileName)
-
{
-
return new PdfContentResult(model, fileName);
-
}
-
-
public static PdfContentResult Pdf(this Controller controller, string fileName)
-
{
-
return new PdfContentResult(fileName);
-
}
-
}
这种感觉用起来是不是与return view();一样
-
public class PdfController : Controller
-
{
-
// GET: Pdf
-
public ActionResult Index()
-
{
-
return this.Pdf(null, "index");
-
}
-
}
可能有人会问pdf文档的内容在哪里维护,直接打开Action对应的View视图,像写mvc页面一样布局pdf内容就可以了
至于itextsharp更多功能支持,请参考此文档:http://developers.itextpdf.com/