前言

在本文中,我们将学习如何使用rotativa.aspnetcore工具从asp.net core中的视图创建pdf。如果您使用asp.net mvc,那么rot​​ativa工具已经可用,我们可以使用它来生成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 = 20, bottom = 20, right = 20, top = 20 },
   };
  }

 就这样,我们再次启动,可见已经有了外边距!

横向与纵向

它还给我们提供了横向还是竖向的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 = 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<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> 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对www.887551.com的支持。