Webwonders.PdfGenerator 13.0.0

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

// Install Webwonders.PdfGenerator as a Cake Tool
#tool nuget:?package=Webwonders.PdfGenerator&version=13.0.0

Webwonders.PdfGenerator

About

Generate PDF's from HTML-files in Umbraco. Webwonders.PdfGenerator is a wrapper around WkHtmlToPdfDotNet (https://github.com/HakanL/WkHtmlToPdf-DotNet)

How to use

The package defines a service: IWebwondersHtmlToPdfService, which can be injected in your code. This service has one method: GetPdfMemoryStream(string pdfTheme, object viewModel, HtmlToPdfSettings? settings = null).

PdfTheme is the name of the type of PDF you want to generate. This should also be the name of the folder where you locate the views for the PDF: /Views/Pdf/Pdftheme. In this folder three views can be located: Header.cshtml, Body.cshtml and Footer.cshtml. The Body is required, the Header and Footer views are optional. By specifying UseHeaderHtml and UseFooterHtml in the settings you can indicate if you want to use the Header and Footer views.

There is an option to use a custom stylesheet for the PDF: pdfStyle.css. If a stylesheet with the name pdfStyle.css exists in the folder /wwwroot/PdfThemes/pdfTheme, this stylesheet is used to style the PDF.

ViewModel is the viewmodel you want to pass to the views: all views are rendered with the viewmodel you pass.

Settings are taken from WkHtmlToPdfDotNet, but are all combined in one class: HtmlToPdfSettings, they all are optional and have default values.

The result of GetpdfMemoryStream is a named tuple: (bool success, MemoryStream? stream), indicating success and the MemoryStream containing the PDF.

Example


// create viewmodel
var pdfViewModel = new PdfViewModel
{
    OrderId = orderId,
    InvoiceNumber = invoiceNumber,
    InvoiceDate = invoiceDate
};

// define settings, optional: all settings have defaults
var pdfSettings = new HtmlToPdfSettings()
{
    Margins = new MarginSettings { Top = 50, Bottom = 65 },
    DocumentTitle = fileName,
    PagesCount = true,
    DefaultEncoding = "utf-8",
    UseHeaderHtml = true,
    UseFooterHtml = true,
    HeaderSpacing = 20,
    FooterSpacing = 30,
};

// get memory stream of PDF
(bool success, MemoryStream? stream) = htmlToPdfService.GetPdfMemoryStream("Invoice", pdfViewModel, pdfSettings);

if (success && stream != null)
{
    // Do something with the stream
}

Note

Do not use Webwonders.PdfGenerator with any untrusted HTML - be sure to sanitize any user-supplied HTML/JS, otherwise it can lead to complete takeover of the server it is running on.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
13.0.0 134 12/14/2023
12.0.5 863 9/22/2023