Table of Contents
Using MVCToPDF

MVCToPDF consists of two classes that allow you to convert an ASP.NET MVC page to PDF.

Before You Begin

Before you can use MVCToPDF:

  1. Your project must be an ASP.NET MVC project. MVCToPDF is specifically designed to be used with MVC Web application only. If your application uses Web Form, you should use ASPXToPDF instead. Both MVCToPDF and ASPXToPDF are built-in on top of EO.Pdf. So regardless of your application type, you can always use the more generic HTML to PDF converter interface directly;
  2. You must reference EO.Pdf.dll and EO.Pdf.Mvc.dll. See here for more information about how to reference EO.Pdf.dll. After you add reference to EO.Pdf.dll, follow the same steps to reference EO.Pdf.Mvc.dll;
  3. Follow this step if your application is a traditional ASP.NET MVC application. If your application is ASP.NET Core based, skip to the next step.

    Call EO.Pdf.Mvc.MVCToPDF.RegisterFilter inside your Application_Start method and pass the type of the System.Web.Mvc.GlobalFilters as argument. For example:
    public class MvcApplication : System.Web.HttpApplication
    {
        ....other methods....
    
        protected void Application_Start()
        {
            //Register EO.Pdf MVCToPDF filters
            EO.Pdf.Mvc.MVCToPDF.RegisterFilter(typeof(GlobalFilters));
    
            //Other common MVC startup code
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }
    }

    Alternatively, you can also call RegisterFilter inside RegisterGlobalFilters method of the FilterConfig class.

  4. Follow these steps if your application is based on ASP.NET Core.

    1. Following these general steps;
    2. Call EO.Pdf.Mvc.MVCToPDF.RegisterFilterAspNetCore inside your Setup.ConfigureServices through AddMvc call. The original code may look like this:
      public class Startup
      {
          ....other methods....
      
          public void ConfigureServices(IServiceCollection services)
          {
              ....other code...
              
              //Or services.AddControllersWithViews();
              services.AddMvc();        
          }
      }

      Change it to:

      public class Startup
      {
          ....other methods....
      
          public void ConfigureServices(IServiceCollection services)
          {
              ....other code...
              
              //Or services.AddControllersWithViews();
              services.AddMvc(options =>
                  {
                      //Initializes MVCToPDF
                      EO.Pdf.Mvc.MVCToPDF.RegisterFilterAspNetCore(options);
                  });
          }
      }

Using MVC to PDF

Follow these steps to use MVCToPDF:

  • Location your controller class and the action method that you wish to convert. For example, in the demo application, we want to generate a PDF file when user clicks the "Create Address Labels" button. The corresponding action method for this button is the post back version of the Index method of the DemoController class:

    [HttpPost]
    public ActionResult Input(AddressModel m)
    {
        ....
    }
  • Use EO.Pdf.Mvc namespace by adding the following code at the top of the controller code file:

    using EO.Pdf.Mvc;
  • Apply RenderAsPDFAttribute to the action method. For example:
  • [HttpPost]
    [EO.Pdf.Mvc.RenderAsPDF]
    public ActionResult Input(AddressModel m)
    {
        ....
    }

Run the page and the result of action "Input" will be rendered as PDF.

Trigger PDF Conversion Conditionally

By default, the conversion automatically occurs as soon as attribute RenderAsPDFAttribute is applied. You can set the attribute's AutoConvert to false and then call MVCToPDF.RenderAsPDF method to trigger the conversion when needed. The following code demonstrates this feature:

[HttpPost]
[EO.Pdf.Mvc.RenderAsPDF(AutoConvert=false)]
public ActionResult Input(AddressModel m)
{
    //Trigger the conversion only when m.ExportToPDF is true
    if (m.ExportToPDF)
    {
        EO.Pdf.Mvc.MVCToPDF.ResultFileName = "AddressLabel";
        EO.Pdf.Mvc.MVCToPDF.RenderAsPDF();
    }

    return View("AddressLabel", m);
}

Setting Conversion Options

You can set conversion options immediately before calling MVCToPDF.RenderAsPDF. Note that you should set RenderAsPDFAttribute.AutoConvert to false to use this option.

[HttpPost]
[EO.Pdf.Mvc.RenderAsPDF(AutoConvert=false)]
public ActionResult Input(AddressModel m)
{
    //Set output page size to 8.5 by 11 inches
    EO.Pdf.HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
    
    //Trigger the conversion
    EO.Pdf.Mvc.MVCToPDF.RenderAsPDF();

    return View("AddressLabel", m);
}

Save Conversion Result into a File

By default the conversion result is send to the client browser. Sometimes you may not want this to happen and wish to perform other actions on the conversion result, for example, to save the result as a file, or send it out as an email attachment etc. In order to achieve this, you must:

  1. Set the RenderAsPDFAttribute's SendToClient property to false, or set MVCToPDF.SendToClient to false if you call MVCToPDF.RenderASPDF explicitly;

    //Code inside the action method
    EO.Pdf.Mvc.MVCToPDF.SendToClient = false;
  2. Call RenderAsPDF and supply an afterConvertHandler callback. Inside the callback function you can use the Result property of the MVCToPDF class to access the conversion result and save it to a file. The following code demonstrates how to save the result as a file:

    //Convert with afterConvertHandler
    EO.Pdf.Mvc.MVCToPDF.RenderAsPDF(Post_Hanlder);
    
    protected void Post_Handler(object sender, PdfDocumentEventArgs e)
    {
        EO.Pdf.HtmlToPdfResult result = EO.Pdf.Mvc.MVCToPDF.Result;
        if (result != null)
            result.PdfDocument.Save(the_pdf_file_name);
    }

Change "Save As" File Name

When the conversion result is directly sent to the client as a download (when RenderAsPDFAttribute.ResultAsDownload or MVCToPDF.ResultAsDownload is true), you can use RenderAsPDFAttribute.ResultFileName or MVCToPDF.ResultFileName. The later should be used only if you call MVCToPDF.RenderASPDF explicitly.

If you wish the result to open in the browser directly instead of as a download, you can set RenderAsPDFAttribute.ResultAsDownload or MVCToPDF.ResultAsDownload to false.

Samples Code

A small MVC sample project is included in the Samples\MVC sub folder inside the installer folder.

License

MVCToPDF is covered by EO.Pdf license. You do not need an additional license to use this feature.