随笔 - 13,  文章 - 0,  评论 - 4,  阅读 - 26911

1、nuget引入包 Haukcode.WkHtmlToPdfDotNet

using WkHtmlToPdfDotNet;
using WkHtmlToPdfDotNet.Contracts;
复制代码
private readonly IConverter converter;

//控制器注入
public TestController( IConverter converter)
{
      this.converter = converter;
}


//将百度页面生成pdf
 public IActionResult CreatePDF()
        {
            try
            {
                var doc = new HtmlToPdfDocument()
                {
                    GlobalSettings = {
                    PaperSize = PaperKind.A3,
                    Orientation = Orientation.Landscape,
                },

                    Objects = {
                    new ObjectSettings()
                    {
                        Page = "http:/www.baidu.com",
                    }
                }
                };

                byte[] pdf = this.converter.Convert(doc);

                var base64 = Convert.ToBase64String(pdf);
                return Content(ResponseResult.Success(base64));
            }
            catch (Exception)
            {
                return Content(ResponseResult.Exception("文件生产错误"));
            }
           
        }
复制代码

 

posted on   一禅·小和尚  阅读(1192)  评论(0编辑  收藏  举报

< 2025年1月 >
29 30 31 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 6 7 8
点击右上角即可分享
微信分享提示