Press "Enter" to skip to content

PDF Creation Using Rotativa in ASP.NET MVC

In the .Net Framework, creating PDFs for reporting or other purposes often opens a business. However, Rotativa, which we can download from Nuget, makes our job very easy. It is great convenience that Rotativa can convert View or Partial Views to PDF.

Installing Rotativa in MVC

We can install with the “Install-Package Rotativa” command using the Package Manager console. The second way to install is Nuget. When the installation is complete, a folder called Rotativa is created in the root of your project.

Creating PDF with Rotativa

ViewAsPdf: The name of the class to create PDF based on View. Sample code is:

public ActionResult DownloadViewPDF()
{
    List<User> uList = GetUsers();    //get user list from db, json or etc.
    ViewAsPdf pdf = new ViewAsPdf("PdfView", uList);
    return pdf;
}

PdfView” is the name of View to be converted to PDF. uList is Model to be sent to View. So there is no difference from the return View () method. There are also ActionAsPdf, PartialViewAsPdf and UrlAsPdf classes available like ViewAsPdf. Their names explain all their functions.

In the example above, we can interfere with the many features of the PDF which will be created by the object named “pdf”. For example :

pdf.PageMargins.Left = 0;    //padding from left
pdf.FileName = "Deneme";     // name of the file
pdf.PageHeight = 200;    //height of page

Let me remind you that if there is a problem with PDF creation, the first permissions to be checked. An error can occur if the IIS user does not have access to the “Rotativa” directory in the root directory.

Do not forget to allow all users access to the folder in the root directory. Rotativa is really helpful for creating pdf files.

I think you should try!

Do not forget to visit our programming memes category! Click here

Comments are closed.