TemplateExport.Pdf 0.0.1

dotnet add package TemplateExport.Pdf --version 0.0.1
                    
NuGet\Install-Package TemplateExport.Pdf -Version 0.0.1
                    
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="TemplateExport.Pdf" Version="0.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TemplateExport.Pdf" Version="0.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TemplateExport.Pdf" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TemplateExport.Pdf --version 0.0.1
                    
#r "nuget: TemplateExport.Pdf, 0.0.1"
                    
#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.
#:package TemplateExport.Pdf@0.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TemplateExport.Pdf&version=0.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TemplateExport.Pdf&version=0.0.1
                    
Install as a Cake Tool

TemplateExport.Pdf

Features

  • Load HTML templates from files
  • Replace placeholders with data
  • Support for conditional rendering
  • Support for lists and collections
  • Combine multiple HTML files into a single PDF

Installation

To install TemplateExport.Pdf, add the following NuGet package to your project:

dotnet add package TemplateExport.Pdf

Usage

1. Configure Services

First, configure the services in your Main function or startup class:

using Microsoft.Extensions.DependencyInjection;
using TemplateExport.Pdf.Extensions;

var services = new ServiceCollection();
services.AddTemplateExportPdf();
var serviceProvider = services.BuildServiceProvider();

2. Add html template on given location

Create html template on given location

<html>
	<div template-if="{{Person::IsOld}}">
		<div template-if="{{Person::IsOld}}" style="color: red">  
		  Old: {{Person::Age}}   
		</div>  
		<div template-else style="color: green">  
		  Young: {{Person::Age}}  
		</div>
	</div>
	<div template-for="{{People}}">
		<div style="color:blue">
			{{People::Name}}
		</div>
		<div style="color:gray">
			{{People::Age}}
		</div>
	</div>
</html>

3. Create a PDF Export Configuration

Create a configuration for exporting a PDF document:

using TemplateExport.Pdf.Models;

var config = PdfExportConfiguration.CreateBuilder()
    .UseTemplatePath("./Resources/template.html")
    .UseOutputPath("./Resources/output.pdf")
    .AddDataSet("Person", new Person { Name = "John", Age = 30 })
    .Build();

4. Export the PDF

Resolve the ITemplateExportPdf service and call the Export method:

var pdfExport = serviceProvider.GetService<ITemplateExportPdf>();
pdfExport.Export(config);

5. Configuration options


UseTemplateStringStartsWith("{{") // set placeholders starts with string (Default: "{{")

UseTemplateStringEndsWith("}}") // set placeholders starts with string (Default: "}}")

UseTemplateStringSeparator("::") // set placeholders separator string (Default: "::")

UseOutputStream(stream) // set output stream for pdf

UseOutputPath(path) // set output path for pdf

AddDataSet("Person", new Person { Name = "John", Age = 30 }) // add data set to configuration

SetPageOrientation(PageOrientation.Landscape) // set page orientation (Default: Portrait)

SetPageSize(PageSize.A3) // set page size (Default: A4)

Example

Here is a complete example of exporting a PDF document:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using TemplateExport.Pdf;
using TemplateExport.Pdf.Extensions;
using TemplateExport.Pdf.Models;

var services = new ServiceCollection();
services.AddTemplateExportPdf();
var serviceProvider = services.BuildServiceProvider();

var pdfExport = serviceProvider.GetService<ITemplateExportPdf>();

var person = new Person { Name = "John", Age = 30 };
var people = new List<Person>
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 35 }
};

var config = PdfExportConfiguration.CreateBuilder()
    .UseTemplatePath("./Resources/template.html")
    .UseOutputPath("./Resources/output.pdf")
    .AddDataSet("Person", person)
    .AddDataSet("People", people)  
	.SetPageOrientation(PageOrientation.Landscape)  
	.SetPageSize(PageSize.A3)
    .Build();

var stopwatch = new Stopwatch();
stopwatch.Start();

pdfExport.Export(config);

stopwatch.Stop();
Console.WriteLine($"Elapsed time: {stopwatch.ElapsedMilliseconds} ms");

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
0.0.1 291 3/3/2025