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 2022-04-22 11:09  idea飞行家  阅读(416)  评论(0)    收藏  举报

导航