HTML 转 PDF

方法1: OpenHtmlToPdf  

复制代码
 1             using (WebClient wc = new WebClient())
 2             {
 3                 wc.Encoding = Encoding.UTF8;
 4                // string html = wc.DownloadString(url);
 5                 string html = File.ReadAllText("C://ssss7.html");
 6 
 7 
 8                 //var pdf = Pdf
 9                 //.From(html)
10                 //.WithGlobalSetting("orientation", "Landscape")
11                 //.WithObjectSetting("web.defaultEncoding", "utf-8")
12                 //.Content();
13 
14                 var pdf = Pdf
15                  .From(html)
16                  .OfSize(PaperSize.A4)
17                  .WithoutOutline()
18                  .WithMargins(2.5.Centimeters())
19                  .Portrait()
20                  .Comressed()
21                  .Content();
22                 File.WriteAllBytes("C://fffff9.pdf", pdf);//保存在本地
23 
24                 //HtmlToPdf toPdf = new HtmlToPdf();
25                 //toPdf.Options.PdfPageSize = PdfPageSize.A4;
26                 //toPdf.Options.MarginRight = 3;
27                 //toPdf.Options.MarginLeft = 3;
28 
29 
30                 //PdfDocument pdf = toPdf.ConvertHtmlString(html); ;
31 
32                 //pdf.Save("C://fffff10.pdf");
33                 //pdf.Close();
34 
35 
36             }
复制代码

 

方法2: SelectPdf
复制代码
 1                 HtmlToPdf toPdf = new HtmlToPdf();
 2                 toPdf.Options.PdfPageSize = PdfPageSize.A4;
 3                 toPdf.Options.MarginRight = 3;
 4                 toPdf.Options.MarginLeft = 3;
 5 
 6 
 7                 PdfDocument pdf = toPdf.ConvertHtmlString(html); ;
 8 
 9                 pdf.Save("C://fffff10.pdf");
10                 pdf.Close();
复制代码

 

 

 
 

posted on   idea飞行家  阅读(388)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示