C# Net Core 使用 itextsharp.lgplv2.core 把Html转PDF
只支持英文(中文我不知道怎么弄,懂的朋友帮我看一下)!!!!!
引入包itextsharp.lgplv2.core
代码
public static MemoryStream Pdf(string html) { StringReader sr = new StringReader(html); //步骤1 Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f); MemoryStream stream = new MemoryStream(); //步骤2 PdfWriter.GetInstance(document, stream); //步骤3 document.Open(); //创建一个样式表 StyleSheet styles = new StyleSheet(); ////设置默认字体的属性 //styles.LoadTagStyle(HtmlTags.BODY, "encoding", "Identity-H"); //styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "Tahoma"); //styles.LoadTagStyle(HtmlTags.BODY, "size", "16pt"); //FontFactory.Register(@"C:\Windows\Fonts\tahoma.ttf"); var unicodeFontProvider = FontFactoryImp.Instance; unicodeFontProvider.DefaultEmbedding = BaseFont.EMBEDDED; unicodeFontProvider.DefaultEncoding = BaseFont.IDENTITY_H; var props = new Hashtable { // { "img_provider", new MyImageFactory() }, { "font_factory", unicodeFontProvider } //始终使用Unicode字体 }; //步骤4 //var objects = HtmlWorker.ParseToList(sr, styles, props); var objects = HtmlWorker.ParseToList(sr, styles); foreach (IElement element in objects) { document.Add(element); } document.Close(); return stream; }