Webwonders.PdfGenerator
13.0.0
dotnet add package Webwonders.PdfGenerator --version 13.0.0
NuGet\Install-Package Webwonders.PdfGenerator -Version 13.0.0
<PackageReference Include="Webwonders.PdfGenerator" Version="13.0.0" />
paket add Webwonders.PdfGenerator --version 13.0.0
#r "nuget: Webwonders.PdfGenerator, 13.0.0"
// 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 | Versions 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. |
-
net8.0
- Haukcode.WkHtmlToPdfDotNet (>= 1.5.86)
- RazorLight (>= 2.3.1)
- Umbraco.Cms.Web.BackOffice (= 13.0.0)
- Umbraco.Cms.Web.Website (= 13.0.0)
- Webwonders.Umbraco.Extensions (>= 13.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.