使用Rotativa在ASP.NET Core MVC中创建PDF
在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF。如果您使用ASP.NET MVC,那么Rotativa工具已经可用,我们可以使用它来生成pdf。
创建一个MVC项目,无论您是core或不core,都可以nuget下包.命令如下:
Install-Package Rotativa
#或者
Install-Package Rotativa.AspNetCore
这个工具由意大利人Giorgio Bozio创建。他需要在ASP.NET MVC中生成pdf,并且重复的任务是设置一种方法来创建PDF文档,用于业务流程或报告,下面废话不多说,我们开始吧。
在startup.cs类中配置Rotativa.AspNetCore设置
我们在Configure方法内的startup.cs类中添加此设置,以设置要访问的wkhtmltopdf.exe文件的相对路径。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
RotativaConfiguration.Setup(env);
}
我们需要在wwwroot中添加Rotativa文件夹,然后放入这两个exe,我把这两个文件已经放到了百度云盘。
然后我们添加一个Demo控制器,定义一个Get方法,其定义如下,通过ViewAsPdf方法,就可以通过pdf的形式去套住cshtml,也就达到了pdf的效果。
public class DemoController : Controller
{
[HttpGet]
public IActionResult DemoViewAsPdf()
{
return new ViewAsPdf("DemoViewAsPdf");
}
}
就现在,我们需要通过控制器去创建一个视图,然后在视图中有如下定义:
@{
ViewData["Title"] = "DemoViewAsPdf";
}
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<p>Hello AspNetCore!!</p>
</body>
</html>
现在,我们把页面重定与
http://localhost:55999/Demo/DemoViewAsPdf
边距
除了普通的展示pdf,我们还可以进行操作,例如下载,打印。当然如果宽和高不太满意,你可以对视图进行设置,其中有一个类是对视图进行配置的,其定义如下,有四大配置值。
public class Margins
{
[OptionFlag("-B")]
public int? Bottom;
[OptionFlag("-L")]
public int? Left;
[OptionFlag("-R")]
public int? Right;
[OptionFlag("-T")]
public int? Top; public Margins();
public Margins(int top, int right, int bottom, int left); public override string ToString();
}
在控制器中直接new出它,然后直接return,和上面类似,现在你可以将html中的p标签添加一些内容,然后看一下效果。
[HttpGet]
public IActionResult DemoViewAsPdf()
{
return new ViewAsPdf("DemoPageMarginsPDF")
{
PageMargins = { Left = , Bottom = , Right = , Top = },
};
}
就这样,我们再次启动,可见已经有了外边距!
横向与纵向
它还给我们提供了横向还是竖向的pdf效果,如以下定义:
[HttpGet]
public IActionResult DemoViewAsPdf(string Orientation)
{
if (Orientation == "Portrait")
{
var demoViewPortrait = new ViewAsPdf("DemoViewAsPDF")
{
FileName = "Invoice.pdf",
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,
};
return demoViewPortrait;
}
else
{
var demoViewLandscape = new ViewAsPdf("DemoViewAsPDF")
{
FileName = "Invoice.pdf",
PageOrientation = Rotativa.AspNetCore.Options.Orientation.Landscape,
};
return demoViewLandscape;
}
}
通过 http//localhost:60042/demo/DemoOrientationPDF?Orientation=Portrait 或者其它路由进行访问,你对比以下就可以看到效果。
设置PDF大小
基本上都是A4,枚举里很多值,自己看~
[HttpGet]
public IActionResult DemoViewAsPdf(string Orientation)
{
return new ViewAsPdf("DemoPageSizePDF")
{
PageSize = Rotativa.AspNetCore.Options.Size.A4
};
}
小案例
创建一个模型,这是一个非常简单的模型,定义如下:
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Phoneno { get; set; }
}
在控制器中new几个对象,然后返回pdf。
[HttpGet]
public IActionResult DemoViewAsPdf()
{
List<Customer> customerList = new List<Customer>() {
new Customer { CustomerID = , Address = "Taj Lands Ends 1", City = "Mumbai" , Country ="India", Name ="Sai", Phoneno =""},
new Customer { CustomerID = , Address = "Taj Lands Ends 2", City = "Mumbai" , Country ="India", Name ="Ram", Phoneno =""},
new Customer { CustomerID = , Address = "Taj Lands Ends 3", City = "Mumbai" , Country ="India", Name ="Sainesh", Phoneno =""},
new Customer { CustomerID = , Address = "Taj Lands Ends 4", City = "Mumbai" , Country ="India", Name ="Saineshwar", Phoneno =""},
new Customer { CustomerID = , Address = "Taj Lands Ends 5", City = "Mumbai" , Country ="India", Name ="Saibags", Phoneno =""}
};
return new ViewAsPdf("DemoModelPDF", customerList);
}
在视图中,我们只是迭代集合,渲染页面。
@model List<MvcHtmlToPdf.Models.Customer>
@{
Layout = null;
} <!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<h2>Customer</h2>
<p>Customer Details</p>
<table class="table table-bordered">
<thead>
<tr>
<th>CustomerID</th>
<th>Name</th>
<th>Address</th>
<th>Country</th>
<th>City</th>
<th>Phoneno</th>
</tr>
</thead>
<tbody> @foreach (var item in Model)
{
<tr>
<td>@item.CustomerID</td>
<td>@item.Name</td>
<td>@item.Address</td>
<td>@item.Country</td>
<td>@item.City</td>
<td>@item.Phoneno</td>
</tr>
} </tbody>
</table>
</div>
</body>
</html>
使用Rotativa在ASP.NET Core MVC中创建PDF的更多相关文章
- NET Core MVC中创建PDF
使用Rotativa在ASP.NET Core MVC中创建PDF 在本文中,我们将学习如何使用Rotativa.AspNetCore工具从ASP.NET Core中的视图创建PDF.如果您使用ASP ...
- ASP.NET Core MVC 中的 [Controller] 和 [NonController]
前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...
- ASP.NET Core MVC 中设置全局异常处理方式
在asp.net core mvc中,如果有未处理的异常发生后,会返回http500错误,对于最终用户来说,显然不是特别友好.那如何对于这些未处理的异常显示统一的错误提示页面呢? 在asp.net c ...
- 006.Adding a controller to a ASP.NET Core MVC app with Visual Studio -- 【在asp.net core mvc 中添加一个控制器】
Adding a controller to a ASP.NET Core MVC app with Visual Studio 在asp.net core mvc 中添加一个控制器 2017-2-2 ...
- 008.Adding a model to an ASP.NET Core MVC app --【在 asp.net core mvc 中添加一个model (模型)】
Adding a model to an ASP.NET Core MVC app在 asp.net core mvc 中添加一个model (模型)2017-3-30 8 分钟阅读时长 本文内容1. ...
- ASP.NET Core MVC中的 [Required]与[BindRequired]
在开发ASP.NET Core MVC应用程序时,需要对控制器中的模型校验数据有效性,元数据注释(Data Annotations)是一个完美的解决方案. 元数据注释最典型例子是确保API的调用者提供 ...
- ASP.NET Core MVC中构建Web API
在ASP.NET CORE MVC中,Web API是其中一个功能子集,可以直接使用MVC的特性及路由等功能. 在成功构建 ASP.NET CORE MVC项目之后,选中解决方案,先填加一个API的文 ...
- 007.Adding a view to an ASP.NET Core MVC app -- 【在asp.net core mvc中添加视图】
Adding a view to an ASP.NET Core MVC app 在asp.net core mvc中添加视图 2017-3-4 7 分钟阅读时长 本文内容 1.Changing vi ...
- 在ASP.NET Core MVC中子类Controller拦截器要先于父类Controller拦截器执行
我们知道在ASP.NET Core MVC中Controller上的Filter拦截器是有执行顺序的,那么如果我们在有继承关系的两个Controller类上,声明同一种类型的Filter拦截器,那么是 ...
随机推荐
- linux上nginx新建站点
遇到一个要将后台部分模块剥离出来,重新放到一个新的后台上的问题: 这样一来,就要在服务器上新建站点,but,服务器是linux系统的,不是很熟,经过多方努力,搞定了 在这记录一下,用到的linux命令 ...
- 微信小程序-框架详解(1)
配置 -app.json文件对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.tab等 { "pages": [ //决定页面文件的路径 "pag ...
- Eclipse常用插件 + Eclipse快捷键
J2EE开发IDE,常用的有Eclipse.Myeclipse.Intellij IDEA 版本(Luna):http://www.eclipse.org/downloads/ 版本(2015 ...
- nohup在linux中的挂起
笔者也是一个linux新手,最近在学习linux相关的东西,本人是一个node爱好者,想在linux上写一个linux服务,我的环境是centeros7,用putty链接远端的服务器,要想让服务在服务 ...
- Tomcat配置与优化(内存、并发、管理)与性能监控
原文链接:http://blog.csdn.net/xyang81/article/details/51530979 一.JVM内存配置优化 在开发当中,当一个项目比较大时,依赖的jar包通常比较多, ...
- Pat1108: Finding Average
1108. Finding Average (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The b ...
- 影响 MySQL Server 性能的相关因素
MySQL 最多的使用场景是WEB 应用,那么我们就以一个WEB 应用系统为例,逐个分析其系统构成,进行经验总结,分析出数据库应用系统中各个环境对性能的影响. 商业需求对性能的影响 这里我们就拿一个看 ...
- sqlserver聚合索引(clustered index) / 非聚合索引(nonclustered index)的理解
1. 什么是聚合索引(clustered index) / 什么是非聚合索引(nonclustered index)? 可以把索引理解为一种特殊的目录.微软的SQL SERVER提供了两种索引:聚集索 ...
- Linux时间子系统之(六):POSIX timer
专题文档汇总目录 Notes:首先讲解了POSIX timer的标识(唯一识别).POSIX Timer的组织(管理POSIX Timer).内核中如何抽象POSIX Timer:然后分析了POSIX ...
- ASP.NET Core & Docker 实战经验分享
一.前言 最近一直在研究和实践ASP.NET Core.Docker.持续集成.在ASP.NET Core 和 Dcoker结合下遇到了一些坑,在此记录和分享,希望对大家有一些帮助. 二.中间镜像 我 ...