How to use Rotativa to create PDF details in ASP.NET Core MVC

Time:2019-9-30

Preface

In this article, we will learn how to use the Rotativa. AspNetCore tool to create PDF from views in ASP. NET Core. If you use ASP.NET MVC, the Rot ativa tool is already available, and we can use it to generate pdf.

Create an MVC project, whether you are core or not, you can nuget the package. The commands are as follows:

Install-Package Rotativa
Or
Install-Package Rotativa.AspNetCore

This tool was created by Giorgio Bozio, an Italian. He needs to generate PDF in ASP. NET MVC, and the repetitive task is to set up a method to create PDF documents for business processes or reports. Let’s get started.

Configure Rotativa. AspNetCore settings in the startup. CS class

We add this setting to the startup. CS class in the Configure method to set the relative path of the wkhtmltopdf. EXE file to be accessed.


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

We need to add the Rotativa folder to the wwwroot, and then put these two exes, which I have put on Baidu cloud disk.

Then we add a Demo controller and define a Get method, which is defined as follows. Through the ViewAsPdf method, we can use the form of PDF to trap cshtml, which achieves the effect of pdf.


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

Now, we need to create a view through the controller, and then define it as follows:


@{
 ViewData["Title"] = "DemoViewAsPdf";
}
<html>
<head>
 <meta charset="utf-8">
 <title>Demo</title>
</head>
<body>
 <p>Hello AspNetCore!!</p>
</body>
</html> 

Now, let’s redirect the page

http://localhost:55999/Demo/DemoViewAsPdf

Margin

In addition to the ordinary display pdf, we can also operate, such as download, print. Of course, if the width and height are not satisfactory, you can set the view, one of which is to configure the view, which is defined as follows, there are four configuration values.


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();
 }

Now you can add some content to the P tag in HTML and see the effect.


[HttpGet]
  public IActionResult DemoViewAsPdf()
  {
   return new ViewAsPdf("DemoPageMarginsPDF")
   {
    PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
   };
  }

In this way, we start again, we can see that there is already an outer margin!

Horizontal and Longitudinal

It also provides us with horizontal or vertical PDF effects, such as the following definitions:


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

By accessing http//localhost:60042/demo/DemoOrientation PDF? Orientation=Portrait or other routes, you can see the effect by comparing the following.

Set the PDF size

Basically, it’s A4. There are many values in the enumeration. Look at them for yourself.~


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

Small case

Create a model, which is a very simple model, defined as follows:


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 objects in the controller, and then return to 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);
  }

In the view, we just iterate over the set and render the page.


@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>
  <h2>Customer</h2>
  <p>Customer Details</p>
  <table>
   <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> 

summary

Above is the whole content of this article. I hope that the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message and exchange it. Thank you for your support to developpaer.