BSharp.Core.RazorToPdf 1.1.0

dotnet add package BSharp.Core.RazorToPdf --version 1.1.0                
NuGet\Install-Package BSharp.Core.RazorToPdf -Version 1.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="BSharp.Core.RazorToPdf" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BSharp.Core.RazorToPdf --version 1.1.0                
#r "nuget: BSharp.Core.RazorToPdf, 1.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 BSharp.Core.RazorToPdf as a Cake Addin
#addin nuget:?package=BSharp.Core.RazorToPdf&version=1.1.0

// Install BSharp.Core.RazorToPdf as a Cake Tool
#tool nuget:?package=BSharp.Core.RazorToPdf&version=1.1.0                

BSharp.Core.RazorToPdf

Convert a Razor Template (cshtml or vbhtml) to a PDF format.

A simple way to generate a PDF using as a template Razor. Alternatives to use:

  1. Using a content template This is an alternative use when the source template can be stored in a Database or any persistence mechanism other than a physical file; or when the physical file has to be pre-processed before making the transformation. This returns the binary representation of the pdf to customize the presentation technique of the pdf, as for example, with MVC or ASP.NET Web Forms.

  2. Using a physical path of template This is an alternative use when the source template is a physical file. This returns the binary representation of the pdf to customize the presentation technique of the pdf, as for example, with MVC or ASP.NET Web Forms.

  3. Save in a physical path using a content template This is an alternative use when the source template can be stored in a Database or any persistence mechanism other than a physical file; or when the physical file has to be pre-processed before making the transformation. This saves the pdf in a physical file.

  4. Save in a physical path using a physical path of template This is an alternative use when the source template is a physical file. This saves the pdf in a physical file. .

Examples:

Using a content template

using BSharp.Core.RazorToPdf;

string content = "<h3>Hi, @Model.Name</h3>";
var model = new { Name = "John Smith" };
ITransformEngine transformEngine = new TransformEngine();
byte[]  bytes = transformEngine.Transform(content, "Example1", model);

Using a physical path of template

using BSharp.Core.RazorToPdf;

string templatePath = @"C:\Template.cshtml";
var model = new { Name = "John Smith" };
ITransformEngine transformEngine = new TransformEngine();
byte[]  bytes = transformEngine.TransformFromFile(templatePath, "Example2", model);

Save in a physical path using a content template

using BSharp.Core.RazorToPdf;

string content = "<h3>Hi, @Model.Name</h3>";
var model = new { Name = "John Smith" };
string targetPath = @"C:\Demo.pdf";
ITransformEngine transformEngine = new TransformEngine();
transformEngine.TransformAndSave(content, "Example3", model, targetPath);

Save in a physical path using a physical path of template

using BSharp.Core.RazorToPdf;

string templatePath = @"C:\Template.cshtml";
var model = new { Name = "John Smith" };
string targetPath = @"C:\Demo.pdf";
ITransformEngine transformEngine = new TransformEngine();
transformEngine.TransformFromFileAndSave(templatePath, "Example4", model, targetPath);

Temporary files

Please review the RazorEngine documentation here, for more details.

Example including embeded images

You can include images embeded in base64 using the following instruction:

using BSharp.Core.RazorToPdf;
string base64 = "<IMAGE CODING IN BASE64 >";
string content = "<h3>Hi, @Model.Name</h3><img src=\"@Model.ImageBase64\" />";
var model = new { Name = "John Smith", ImageBase64 = string.Format(CultureInfo.CurrentCulture, "data:image/png;base64,{0}", base64) };
string targetPath = @"C:\Demo.pdf";
ITransformEngine transformEngine = new TransformEngine(new PdfOptions
            {
                ImageTransform = new Base64ImageTransform()
            });
transformEngine.TransformAndSave(content, "Example3", model, targetPath);
Product Compatible and additional computed target framework versions.
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 is compatible.  net463 was computed.  net47 is compatible.  net471 is compatible.  net472 was computed.  net48 was computed.  net481 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
1.1.0 3,989 11/28/2017
1.0.1 1,143 10/30/2017
1.0.1-beta 809 10/26/2017
1.0.0-beta 832 10/26/2017

A simple way to generate a PDF using as a template Razor.
Options to translate to PDF, the options include transformation of images.