Rotativa.AspNetCore
1.4.0
dotnet add package Rotativa.AspNetCore --version 1.4.0
NuGet\Install-Package Rotativa.AspNetCore -Version 1.4.0
<PackageReference Include="Rotativa.AspNetCore" Version="1.4.0" />
paket add Rotativa.AspNetCore --version 1.4.0
#r "nuget: Rotativa.AspNetCore, 1.4.0"
// Install Rotativa.AspNetCore as a Cake Addin #addin nuget:?package=Rotativa.AspNetCore&version=1.4.0 // Install Rotativa.AspNetCore as a Cake Tool #tool nuget:?package=Rotativa.AspNetCore&version=1.4.0
Create PDFs and images with .NET
Use Rotativa to transform a Razor view into a PDF or image. This package is compatible with .NET Core 3.1, .NET 5, .NET 6, .NET 7 and .NET 8.
Install with nuget.org:
https://www.nuget.org/packages/Rotativa.AspNetCore
Please give feedback!
Needs configuration
Basic configuration done in Program.cs (.NET 6 up to 8):
app.UseRotativa();
or, if using .NET Core 3.1 and .NET 5:
app.UseRotativa(env);
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")
Usage
This package should work almost exactly as Rotativa https://github.com/webgio/Rotativa.
Instead of returning a View()
in your .NET controller, use new ViewAsPdf()
to return a PDF or use new ViewAsImage()
to return an image:
public class InvoiceController : Controller
{
private readonly ILogger<InvoiceController> _logger;
public InvoiceController(ILogger<InvoiceController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
// Returns the Index view as HTML.
return View();
}
public IActionResult Invoice()
{
// Returns the Invoice view as PDF.
return new ViewAsPdf();
}
public IActionResult InvoiceImage()
{
// Returns the InvoiceImage view as PDF.
return new ViewAsImage();
}
}
You can specify the View that should be transformed into a PDF or image:
return new ViewAsPdf("NameOfView");
Pass ViewData as an optional property:
ViewData["Message"] = "Thank you for downloading this PDF.";
return new ViewAsPdf(viewData: ViewData);
We support partial views as well:
return new ViewAsImage("MyPartialView", isPartialView: true);
By default Rotativa injects a base url in the head section of the HTML. This can be disabled:
return new ViewAsImage(setBaseUrl: false);
The settings can be combined as well:
ViewData["Message"] = "Thank you for downloading this PDF.";
return new ViewAsImage("MyPartialView", isPartialView: true, viewData: ViewData, setBaseUrl: false);
To change the way the PDF or image is generated, you can pass the settings as parameters:
return new ViewAsImage()
{
Format = ImageFormat.png,
Quality = 90
};
By default the PDF or image is shown to the user in the browser, like HTML. If you want to force the document to be downloaded use the Content-Disposition property:
return new ViewAsPdf()
{
ContentDisposition = ContentDisposition.Attachment,
FileName = "MyDocument.pdf"
};
Each property is documented in the object for easy reference.
Rotativa uses wkhtmltopdf/wkhtmltoimage behind the scenes. If you want to specify custom switches that are unsupported by Rotativa, you can pass them as well:
return new ViewAsPdf()
{
CustomSwitches = "--disable-smart-shrinking"
};
If you need to write the PDF to the server, you can call BuildFile
and use the resulting byte array to save the file:
var pdfFile = new ViewAsPdf().BuildFile(this.ControllerContext);
File.WriteAllBytes("wwwroot/output.pdf", pdfFile);
This is how you save the PDF file to the server before displaying it in the browser:
public IActionResult Invoice()
{
// Generate the PDF.
var pdfFile = new ViewAsPdf();
// Save to the server.
File.WriteAllBytes("wwwroot/output.pdf", pdfFile.BuildFile(this.ControllerContext));
// Show in the browser.
return pdfFile;
}
Issues and Pull Request
Contribution is welcomed. If you would like to provide a PR please add some testing.
rotativa.io
rotativa.io is an API (SaaS) version of Rotativa, hosted on Azure. Works with just a HTTP call, no need to host the process on your server. You can register for free.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETStandard 2.0
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Rotativa.AspNetCore:
Package | Downloads |
---|---|
Our.Umbraco.TemplateToPdf
This Package helps you to dynamically convert html templates to Pdfs! All in Umbraco context! |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Rotativa.AspNetCore:
Repository | Stars |
---|---|
Harsha-Global/AspNetCore-Harsha
Asp.Net Core Ultimate Guide course | Web University by Harsha
|
Version | Downloads | Last updated |
---|---|---|
1.4.0 | 3,932 | 11/6/2024 |
1.3.2 | 155,809 | 11/12/2023 |
1.3.2-beta | 526 | 10/18/2023 |
1.2.0 | 521,609 | 9/19/2021 |
1.2.0-beta | 376,795 | 10/5/2019 |
1.1.1 | 485,603 | 11/17/2018 |
1.1.0 | 1,723 | 11/17/2018 |
1.0.6 | 77,857 | 2/10/2018 |
Multi target enabling .net core 3.1, .net 5, .net 6, .net 7, .net 8