RotativaCore 4.1.0

dotnet add package RotativaCore --version 4.1.0
NuGet\Install-Package RotativaCore -Version 4.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RotativaCore" Version="4.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RotativaCore --version 4.1.0
#r "nuget: RotativaCore, 4.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RotativaCore as a Cake Addin
#addin nuget:?package=RotativaCore&version=4.1.0

// Install RotativaCore as a Cake Tool
#tool nuget:?package=RotativaCore&version=4.1.0

Extremely easy way to create Pdf files from ASP.NET Core

New Features

4.1.0

  • Support new delegate. OnNoContentAsync()
    • Specifies an alternative process in the case of an empty content response from the conversion source or an error response (error response to standard output) by wkhtmltopdf in converting an empty content.
    • Within this delegate, direct operations (e.g., redirection instructions) must be performed on the ActionContext.
    • When this process is called, TryCustomizeAsync and OnBuildFileSuccess are not executed.
    • To use this feature effectively, it is essential to return wkhtmltoX with NoContent, i.e., an empty rendering element (empty Body response). This is because wkhtmltoX will respond with an error condition on its standard output and exit.
    • To take advantage of this, I modified the internal processing so that wkhtnmltoX returns WkhtmlDriverStandardErrorException if wkhtnmltoX terminates abnormally and the return data is empty. If OnNoContentAsync() is defined in this case, this process is called because the converted data is always empty when this error is detected.
    • If OnNoContentAsync() is not defined, there is no difference in behavior except that the thrown Exception is more detailed.
        public async Task<IActionResult> TestEmptyContentActionAsPdfInline()
        {
            return new ActionAsPdf("EmptyContent")
            {
                ContentDisposition = ContentDisposition.Inline,
                OnNoContentAsync = async (ex, context) =>
                {
                    // e.g.
                    context.HttpContext.Response.Redirect("https://example.com/");
                }
            };
        }

4.0.0

  • Property SaveOnServerPath is discontinued.
  • Support new delegate. TryCustomizeAsync()
    • If you want to customize the generated binary file before OnBuildFileSuccess(), use this.
    • Please return true to continue processing, false to drop with error.
        public ActionResult TestInlie()
        {
            return new ActionAsPdf("Index")
            {
                ContentDisposition = ContentDisposition.Inline,
                TryCustomizeAsync = async (stream, context, fineName) =>
                {
                    // some code done.
                    return true;

                    // e.g.
                    var customizeStream = new MemoryStream();
                    await stream.CopyToAsync(customizeStream);

                    // ...
                    stream.SetLength(0);
                    await customizeStream.CopyToAsync(stream);

                    return true;
                },
            };
        }

3.0.0

2.2.0

  • Support new event. OnBuildFileSuccess()
        public ActionResult TestInlie()
        {
            return new ActionAsPdf("Index", new { name = "Friends" })
            {
                //FileName = "Test.pdf",
                ContentDisposition = ContentDisposition.Inline,
                OnBuildFileSuccess = async (bytes, context, fileName) =>
                {
                    // some code done.
                    return true;

                    // example.
                    if (string.IsNullOrEmpty(fileName))
                        fileName = $"{Guid.NewGuid()}.pdf";

                    var container = CloudStorageAccount
                         // Please set your value.
                         // If it's null, it will result in an ArgumentNullException().
                        .Parse(connectionString:null)
                        .CreateCloudBlobClient()
                        // Please set your value.
                        // If it's null, it will result in an ArgumentNullException().
                        .GetContainerReference(containerName:null);

                    try
                    {
                        var blockBlob = container.GetBlockBlobReference(fileName);
                        blockBlob.Properties.ContentType = "application/pdf";
                        await blockBlob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);
                    }
                    catch (Exception e)
                    {
                        // logging.
                        return false;  // fire InvalidOperationException()
                    }

                    return true;
                },
            };
        }

Usage

public ActionResult PrintIndex()
{
    return new ActionAsPdf("Index", new { name = "Giorgio" }) { FileName = "Test.pdf" };
}

public ActionResult Index(string name)
{
    ViewBag.Message = string.Format("Hello {0} to ASP.NET MVC!", name);

    return View();
}

ViewAsPdf now available. It enables you to render a view as pdf in just one move, thanks to scoorf

public ActionResult TestViewWithModel(string id)
{
    var model = new TestViewModel {DocTitle = id, DocContent = "This is a test"};
    return new ViewAsPdf(model);
}

Also available a RouteAsPdf, UrlAsPdf and ViewAsPdf ActionResult.

It generates Pdf also from authorized actions (web forms authentication).

You can also output images from MVC with ActionAsImage, ViewAsImage, RouteAsImage, UrlAsImage.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0 8,853 6/22/2022
4.0.0 6,043 4/22/2022
3.0.0 29,268 4/25/2019
2.2.0 15,634 5/22/2018
2.1.1 3,772 12/20/2017
2.0.0 1,194 12/19/2017

This is beta software always.  It is not fully tested.