BSharp.Core.RazorToPdf
1.1.0
Convert a Razor Template (cshtml or vbhtml) to a PDF format
Install-Package BSharp.Core.RazorToPdf -Version 1.1.0
dotnet add 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
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);
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);
Release Notes
A simple way to generate a PDF using as a template Razor.
Options to translate to PDF, the options include transformation of images.
Dependencies
-
.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)
Used By
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version History
Version | Downloads | Last updated |
---|---|---|
1.1.0 | 1,447 | 11/28/2017 |
1.0.1 | 520 | 10/30/2017 |
1.0.1-beta | 397 | 10/26/2017 |
1.0.0-beta | 423 | 10/26/2017 |