【.NET CORE】使用Rotativa.AspNetCore将网页转换为PDF

文章介绍了如何在Asp.NetCore项目中使用Rotativa.AspNetCore插件将在线网页转换为PDF。首先,通过NuGet安装插件,然后将示例项目的Rotativa文件夹添加到工程。接着,在Startup.cs中配置插件。最后,展示了如何创建返回PDF视图的方法,包括设置页面尺寸、页边距和保存路径等选项。
摘要由CSDN通过智能技术生成

插件功能:将在线网页转换为PDF显示,文件保存

组件配置:

1、在NuGet管理中搜索Rotativa.AspNetCore并安装稳定版,项目github地址:GitHub - webgio/Rotativa.AspNetCore: Rotativa for Asp.Net Core

2、github下载项目,将示例项目中的Rotativa.AspNetCore-master\Rotativa.AspNetCore.Demo\wwwroot下的Rotativa文件夹放入工程项目并包括进项目,如图:

3、StartUp.cs文件Configure方法中增加以下内容

 RotativaConfiguration.Setup((Microsoft.AspNetCore.Hosting.IHostingEnvironment)env);//RotativaConfiguration 转PDF 功能

 注:RotativaConfiguration.Setup方法参数类型为:Microsoft.AspNetCore.Hosting.IHostingEnvironment

如图:

至此 Rotativa项目配置完成

使用方法:

//返回在线预览模式的PDF页面
public ActionResult PDF()
        {
            
            PDFViewModel model = new PDFViewModel();//创建PDF页面参数model

            return new ViewAsPdf(model)
            {
                //PageSize 设置页面尺寸
                PageSize = Size.A4,
                //PageOrientation 设置页面横/纵向
                PageOrientation = Orientation.Portrait,
                //设置页边距
                PageMargins = { Left = 10, Right = 10, Top = 10 }
            }; 
        }
//指定页面路由并生成PDF文件
public ActionResult CreatePDF()
        {
            
            PDFViewModel model = new PDFViewModel();//创建PDF页面参数model

            return new ViewAsPdf("ExamineDetail", model)
            {
                //FileName 设置文件名称
                FileName = fileName,
                PageSize = Size.A4,
                PageOrientation = Orientation.Portrait,
                PageMargins = { Left = 0, Right = 0 },
                //SaveOnServerPath 设置保存在服务器的文件路径
                SaveOnServerPath = filePath
            };
        }

附:object转实体类泛型方法

public T ConvertObject<T>(object asObject) where T : new()
        {
            //创建实体对象实例
            var t = Activator.CreateInstance<T>();
            if (asObject != null)
            {
                Type type = asObject.GetType();
                //遍历实体对象属性
                foreach (var info in typeof(T).GetProperties())
                {
                    object obj = null;
                    //取得object对象中此属性的值
                    var val = type.GetProperty(info.Name)?.GetValue(asObject);
                    if (val != null)
                    {
                        //非泛型
                        if (!info.PropertyType.IsGenericType)
                            obj = Convert.ChangeType(val, info.PropertyType);
                        else//泛型Nullable<>
                        {
                            Type genericTypeDefinition = info.PropertyType.GetGenericTypeDefinition();
                            if (genericTypeDefinition == typeof(Nullable<>))
                            {
                                obj = Convert.ChangeType(val, Nullable.GetUnderlyingType(info.PropertyType));
                            }
                            else
                            {
                                obj = Convert.ChangeType(val, info.PropertyType);
                            }
                        }
                        info.SetValue(t, obj, null);
                    }
                }
            }
            return t;
        }
    }

其他组件使用方法参见插件DEMO源码

备注:

组件需要运行环境安装运行库Microsoft Visual C++ Redistributable ,版本暂时未遇到有要求的,但需要安装x64,和x86两个版本,资源网上搜索下载安装即可,资源链接:Microsoft Visual C++ 2015-2022 Redistributable 14.36.32531.0 - 阿酷杂货铺

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值