如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
前言
在本文中,我們將學(xué)習(xí)如何使用Rotativa.AspNetCore工具從ASP.NET Core中的視圖創(chuàng)建PDF。如果您使用ASP.NET MVC,那么Rotativa工具已經(jīng)可用,我們可以使用它來(lái)生成pdf。
創(chuàng)建一個(gè)MVC項(xiàng)目,無(wú)論您是core或不core,都可以nuget下包.命令如下:
Install-Package Rotativa #或者 Install-Package Rotativa.AspNetCore
這個(gè)工具由意大利人Giorgio Bozio創(chuàng)建。他需要在ASP.NET MVC中生成pdf,并且重復(fù)的任務(wù)是設(shè)置一種方法來(lái)創(chuàng)建PDF文檔,用于業(yè)務(wù)流程或報(bào)告,下面廢話不多說(shuō),我們開(kāi)始吧。
在startup.cs類中配置Rotativa.AspNetCore設(shè)置
我們?cè)贑onfigure方法內(nèi)的startup.cs類中添加此設(shè)置,以設(shè)置要訪問(wèn)的wkhtmltopdf.exe文件的相對(duì)路徑。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
RotativaConfiguration.Setup(env);
}
我們需要在wwwroot中添加Rotativa文件夾,然后放入這兩個(gè)exe,我把這兩個(gè)文件已經(jīng)放到了百度云盤。

然后我們添加一個(gè)Demo控制器,定義一個(gè)Get方法,其定義如下,通過(guò)ViewAsPdf方法,就可以通過(guò)pdf的形式去套住cshtml,也就達(dá)到了pdf的效果。
public class DemoController : Controller
{
[HttpGet]
public IActionResult DemoViewAsPdf()
{
return new ViewAsPdf("DemoViewAsPdf");
}
}
就現(xiàn)在,我們需要通過(guò)控制器去創(chuàng)建一個(gè)視圖,然后在視圖中有如下定義:
@{
ViewData["Title"] = "DemoViewAsPdf";
}
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<p>Hello AspNetCore??!</p>
</body>
</html>
現(xiàn)在,我們把頁(yè)面重定與

邊距
除了普通的展示pdf,我們還可以進(jìn)行操作,例如下載,打印。當(dāng)然如果寬和高不太滿意,你可以對(duì)視圖進(jìn)行設(shè)置,其中有一個(gè)類是對(duì)視圖進(jìn)行配置的,其定義如下,有四大配置值。
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,和上面類似,現(xiàn)在你可以將html中的p標(biāo)簽添加一些內(nèi)容,然后看一下效果。
[HttpGet]
public IActionResult DemoViewAsPdf()
{
return new ViewAsPdf("DemoPageMarginsPDF")
{
PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
};
}
就這樣,我們?cè)俅螁?dòng),可見(jiàn)已經(jīng)有了外邊距!

橫向與縱向
它還給我們提供了橫向還是豎向的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;
}
}
通過(guò) http//localhost:60042/demo/DemoOrientationPDF?Orientation=Portrait 或者其它路由進(jìn)行訪問(wèn),你對(duì)比以下就可以看到效果。
設(shè)置PDF大小
基本上都是A4,枚舉里很多值,自己看~
[HttpGet]
public IActionResult DemoViewAsPdf(string Orientation)
{
return new ViewAsPdf("DemoPageSizePDF")
{
PageSize = Rotativa.AspNetCore.Options.Size.A4
};
}
小案例
創(chuàng)建一個(gè)模型,這是一個(gè)非常簡(jiǎn)單的模型,定義如下:
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幾個(gè)對(duì)象,然后返回pdf。
[HttpGet]
public IActionResult DemoViewAsPdf()
{
List<Customer> customerList = new List<Customer>() {
new Customer { CustomerID = 1, Address = "Taj Lands Ends 1", City = "Mumbai" , Country ="India", Name ="Sai", Phoneno ="9000000000"},
new Customer { CustomerID = 2, Address = "Taj Lands Ends 2", City = "Mumbai" , Country ="India", Name ="Ram", Phoneno ="9000000000"},
new Customer { CustomerID = 3, Address = "Taj Lands Ends 3", City = "Mumbai" , Country ="India", Name ="Sainesh", Phoneno ="9000000000"},
new Customer { CustomerID = 4, Address = "Taj Lands Ends 4", City = "Mumbai" , Country ="India", Name ="Saineshwar", Phoneno ="9000000000"},
new Customer { CustomerID = 5, Address = "Taj Lands Ends 5", City = "Mumbai" , Country ="India", Name ="Saibags", Phoneno ="9000000000"}
};
return new ViewAsPdf("DemoModelPDF", customerList);
}
在視圖中,我們只是迭代集合,渲染頁(yè)面。
@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>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
ASP.NET開(kāi)發(fā)者使用jQuery應(yīng)該了解的幾件事情
如果你是有著APS.NET開(kāi)發(fā)背景的人員,那么jQuery的幾個(gè)概念建議你應(yīng)該忘掉。像使用其它的framework一樣,你應(yīng)該學(xué)習(xí)一下jQuery的所有語(yǔ)法等約定來(lái)讓它更好的為你服務(wù)。2009-09-09
.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法
這篇文章介紹了.NET程序性能監(jiān)控系統(tǒng)Elastic?AMP的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11
詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由
本篇文章主要介紹了詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
VS2005 水晶報(bào)表在時(shí)部署時(shí)遇到的問(wèn)題
前幾天在服務(wù)器上部署一個(gè)B/S程序的時(shí)候,程序中的水晶報(bào)表部分出了些問(wèn)題,報(bào)錯(cuò):Server Error in '/' Application.2010-02-02
Repeater的FooterTemplate顯示某列總計(jì)思路與代碼
在Repeater的FooterTemplate顯示某列總計(jì),接下來(lái)與大家分享詳細(xì)的實(shí)現(xiàn)方案,感興趣的各位可以參考下哈2013-03-03
MAUI中實(shí)現(xiàn)構(gòu)建跨平臺(tái)原生控件
這篇文章介紹了MAUI中實(shí)現(xiàn)構(gòu)建跨平臺(tái)原生控件的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
asp.net?core?configuration配置讀取的實(shí)現(xiàn)
本文主要介紹了asp.net?core?configuration配置讀取,configuration可以從命令行、環(huán)境變量、配置文件讀取配置,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
ASP.NET core Web中使用appsettings.json配置文件的方法
這篇文章主要給大家介紹了在ASP.NET core Web中使用appsettings.json配置文件的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-04-04
.NET實(shí)現(xiàn)倉(cāng)儲(chǔ)Repository(AI)的操作方法
倉(cāng)儲(chǔ)模式是一種在應(yīng)用程序中使用的設(shè)計(jì)模式,它將數(shù)據(jù)訪問(wèn)邏輯與業(yè)務(wù)邏輯分離,通過(guò)倉(cāng)儲(chǔ)接口和倉(cāng)儲(chǔ)實(shí)現(xiàn)類,您可以定義和實(shí)現(xiàn)數(shù)據(jù)的增刪改查操作,這篇文章主要介紹了.NET?實(shí)現(xiàn)倉(cāng)儲(chǔ)Repository(AI),需要的朋友可以參考下2023-09-09

