日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Rotativa在ASP.NETCoreMVC中創(chuàng)建PDF的案例-創(chuàng)新互聯(lián)

這篇文章主要介紹使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊虛擬主機、營銷軟件、網(wǎng)站建設(shè)、平度網(wǎng)站維護、網(wǎng)站推廣。

前言

在本文中,我們將學(xué)習(xí)如何使用Rotativa.AspNetCore工具從ASP.NET Core中的視圖創(chuàng)建PDF。如果您使用ASP.NET MVC,那么Rotativa工具已經(jīng)可用,我們可以使用它來生成pdf。

創(chuàng)建一個MVC項目,無論您是core或不core,都可以nuget下包.命令如下:

Install-Package Rotativa
#或者
Install-Package Rotativa.AspNetCore

這個工具由意大利人Giorgio Bozio創(chuàng)建。他需要在ASP.NET MVC中生成pdf,并且重復(fù)的任務(wù)是設(shè)置一種方法來創(chuàng)建PDF文檔,用于業(yè)務(wù)流程或報告,下面廢話不多說,我們開始吧。

在startup.cs類中配置Rotativa.AspNetCore設(shè)置

我們在Configure方法內(nèi)的startup.cs類中添加此設(shè)置,以設(shè)置要訪問的wkhtmltopdf.exe文件的相對路徑。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
   RotativaConfiguration.Setup(env);
  }

 我們需要在wwwroot中添加Rotativa文件夾,然后放入這兩個exe,我把這兩個文件已經(jīng)放到了百度云盤。

使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的案例

然后我們添加一個Demo控制器,定義一個Get方法,其定義如下,通過ViewAsPdf方法,就可以通過pdf的形式去套住cshtml,也就達到了pdf的效果。

public class DemoController : Controller
 {
  [HttpGet]
  public IActionResult DemoViewAsPdf()
  {
   return new ViewAsPdf("DemoViewAsPdf");
  }
 }

 就現(xiàn)在,我們需要通過控制器去創(chuàng)建一個視圖,然后在視圖中有如下定義:

@{
 ViewData["Title"] = "DemoViewAsPdf";
}


 
 Demo


 

Hello AspNetCore??!

現(xiàn)在,我們把頁面重定與

http://localhost:55999/Demo/DemoViewAsPdf

使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的案例


邊距


除了普通的展示pdf,我們還可以進行操作,例如下載,打印。當(dāng)然如果寬和高不太滿意,你可以對視圖進行設(shè)置,其中有一個類是對視圖進行配置的,其定義如下,有四大配置值。

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 },
   };
  }

 就這樣,我們再次啟動,可見已經(jīng)有了外邊距!

使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的案例

橫向與縱向


它還給我們提供了橫向還是豎向的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 或者其它路由進行訪問,你對比以下就可以看到效果。

設(shè)置PDF大小


基本上都是A4,枚舉里很多值,自己看~

[HttpGet]
  public IActionResult DemoViewAsPdf(string Orientation)
  {
   return new ViewAsPdf("DemoPageSizePDF")
   {
    PageSize = Rotativa.AspNetCore.Options.Size.A4
   };
  }

小案例


創(chuàng)建一個模型,這是一個非常簡單的模型,定義如下:

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 customerList = new List() {
     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);
  }

在視圖中,我們只是迭代集合,渲染頁面。

@model List
@{
 Layout = null;
}




 Bootstrap Example
 
 


 
  

Customer

  

Customer Details

                 CustomerID      Name      Address      Country      City      Phoneno                  @foreach (var item in Model)     {             @item.CustomerID       @item.Name       @item.Address       @item.Country       @item.City       @item.Phoneno           }         

以上是“使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的案例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


文章題目:使用Rotativa在ASP.NETCoreMVC中創(chuàng)建PDF的案例-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://www.dlmjj.cn/article/ddigde.html