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;
}
-------------------后面补充-----------------
后面看到了这个帖子:https://dotnetcoretutorials.com/2019/07/02/creating-a-pdf-in-net-core/
中的 这段话
我决定使用包【pugpdf】:https://github.com/pug-pelle-p/pugpdf
当然如果能解决itextsharp.lgplv2.core的中文问题,我觉得他是最好的选择
-------------------后面补充-----------------
来源:oschina
链接:https://my.oschina.net/u/4418004/blog/4260821