Home > Back-end >  ASP.NET Core 6 Export PDF
ASP.NET Core 6 Export PDF

Time:08-24

I want to make PDF export from my razor page in ASP.NET Core 6. I used 'Rotativa.AspNetCore'.

In my controller, I use this code:

public IActionResult PdfReport()
{
    return View();
}

And call action with this:

public ActionResult Export()
{
    return new ViewAsPdf("PdfReport")
               {
                   FileName = "test.pdf",
                   PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
               };
}  

After used this code in Program.cs:

IWebHostEnvironment env = app.Environment;
RotativaConfiguration.Setup((Microsoft.AspNetCore.Hosting.IHostingEnvironment)env);

I get this error:

Win32Exception: An error occurred trying to start process 'D:\Projects\WebApp\AvatWebApp\AvatWebApp\wwwroot\Rotativa\wkhtmltopdf.exe' with working directory 'D:\Projects\WebApp\AvatWebApp\AvatWebApp\wwwroot\Rotativa'. The requested operation requires elevation.

Please help me - what do I need to do?

Thanks.

CodePudding user response:

In documentation written:

Make sure you have a folder with the wkhtmltopdf.exe file accessible by the process running the web app. By default it searches in a folder named "Rotativa" in the root of the web app. If you need to change that use the optional parameter to the Setup call RotativaConfiguration.Setup(env, "path/relative/to/root")

You may copy it in your wwwroot folder from link below

https://github.com/webgio/Rotativa.AspNetCore/tree/master/Rotativa.AspNetCore.Demo/wwwroot/Rotativa

  • Related