美文网首页
C# html 转 pdf

C# html 转 pdf

作者: Rinaloving | 来源:发表于2020-07-21 16:59 被阅读0次

    方法一:

    OpenHtmlToPdf
    openhtmltopdf.png
      using (WebClient wc = new WebClient())
                {
                    wc.Encoding = Encoding.UTF8;
                   // string html = wc.DownloadString(url);
                    string html = File.ReadAllText("C://ssss7.html");
    
    
                    //var pdf = Pdf
                    //.From(html)
                    //.WithGlobalSetting("orientation", "Landscape")
                    //.WithObjectSetting("web.defaultEncoding", "utf-8")
                    //.Content();
    
                    var pdf = Pdf
                     .From(html)
                     .OfSize(PaperSize.A4)
                     .WithoutOutline()
                     .WithMargins(2.5.Centimeters())
                     .Portrait()
                     .Comressed()
                     .Content();
                    File.WriteAllBytes("C://fffff9.pdf", pdf);//保存在本地
    
                    //HtmlToPdf toPdf = new HtmlToPdf();
                    //toPdf.Options.PdfPageSize = PdfPageSize.A4;
                    //toPdf.Options.MarginRight = 3;
                    //toPdf.Options.MarginLeft = 3;
    
    
                    //PdfDocument pdf = toPdf.ConvertHtmlString(html); ;
    
                    //pdf.Save("C://fffff10.pdf");
                    //pdf.Close();
    
    
                }
    
    

    方法二: SelectPdf

                    HtmlToPdf toPdf = new HtmlToPdf();
                    toPdf.Options.PdfPageSize = PdfPageSize.A4;
                    toPdf.Options.MarginRight = 3;
                    toPdf.Options.MarginLeft = 3;
    
    
                    PdfDocument pdf = toPdf.ConvertHtmlString(html); ;
    
                    pdf.Save("C://fffff10.pdf");
                    pdf.Close();
    

    结果:

    pdf.png

    相关文章

      网友评论

          本文标题:C# html 转 pdf

          本文链接:https://www.haomeiwen.com/subject/hofwkktx.html