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
<PackageReference Include="BSharp.Core.RazorToPdf" Version="1.1.0" />
paket add BSharp.Core.RazorToPdf --version 1.1.0
#r "nuget: BSharp.Core.RazorToPdf, 1.1.0"
// 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:
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.
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.
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.
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 | Versions 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. |
-
.NETFramework 4.0
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 2.0.30506)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.5
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.5.1
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.5.2
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.6
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.6.1
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.6.2
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.7
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
-
.NETFramework 4.7.1
- iTextSharp (>= 5.5.12)
- itextsharp.xmlworker (>= 5.5.12)
- Microsoft.AspNet.Razor (>= 3.2.3)
- RazorEngine (>= 3.10.0)
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,994 | 11/28/2017 |
1.0.1 | 1,150 | 10/30/2017 |
1.0.1-beta | 815 | 10/26/2017 |
1.0.0-beta | 838 | 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.