Westwind.WebView.HtmlToPdf
0.2.0
See the version list below for details.
dotnet add package Westwind.WebView.HtmlToPdf --version 0.2.0
NuGet\Install-Package Westwind.WebView.HtmlToPdf -Version 0.2.0
<PackageReference Include="Westwind.WebView.HtmlToPdf" Version="0.2.0" />
paket add Westwind.WebView.HtmlToPdf --version 0.2.0
#r "nuget: Westwind.WebView.HtmlToPdf, 0.2.0"
// Install Westwind.WebView.HtmlToPdf as a Cake Addin #addin nuget:?package=Westwind.WebView.HtmlToPdf&version=0.2.0 // Install Westwind.WebView.HtmlToPdf as a Cake Tool #tool nuget:?package=Westwind.WebView.HtmlToPdf&version=0.2.0
Html to PDF using WebView on Windows
This library provides a quick way to print HTML to PDF on Windows using the WebView control. You can generate PDF from HTML using a few different mechanisms:
- To file
- To Stream
- Using Async Call
- Using Event Callbacks
Using the WebView control has advantages in that it doesn't require any runtimes to be distributed since the WebView runtime is now part of Windows 11 and Windows 10 and updated automatically with Windows update.
Prerequisites
BBBBB The components requires:
- Running on Windows 11/10 Server 2019/2022
- App that targets
net8.0-windows
ornet6.0-windows
- Running with an Interactive User Context
- Desktop Applications
- Console Applications
The component does not support:
- Non Windows platforms
- Running inside of a server, non-logged-on user context (ie. SYSTEM)
Using the library
You can install the library from NuGet:
PS> install-package westwind.webview.htmltopdf
or:
dotnet add package westwind.webview.htmltopdf
Async Call Syntax for File Output
// Full path to File or URL
var htmlFile = Path.GetFullPath("HtmlSampleFileLonger-SelfContained.html");
var outputFile = Path.GetFullPath(@".\test2.pdf");
File.Delete(outputFile);
var host = new HtmlToPdfHost();
// all overrides are optional!
var pdfPrintSettings = new WebViewPrintSettings()
{
MarginBottom = 0.2F,
MarginLeft = 0.2f,
MarginRight = 0.2f,
MarginTop = 0.4f,
ScaleFactor = 0.8F,
ShouldPrintHeaderandFooter = false,
ColorMode = WebViewPrintColorModes.Grayscale,
};
var result = await host.PrintToPdfAsync(htmlFile, outputFile, pdfPrintSettings);
Assert.IsTrue(result.IsSuccess, result.Message);
ShellUtils.OpenUrl(outputFile);
Async Call Syntax for Stream Result
var htmlFile = Path.GetFullPath("HtmlSampleFileLonger-SelfContained.html");
var outputFile = Path.GetFullPath(@".\test3.pdf");
File.Delete(outputFile);
var host = new HtmlToPdfHost();
var pdfPrintSettings = new WebViewPrintSettings()
{
MarginBottom = 0.2F,
MarginLeft = 0.2f,
MarginRight = 0.2f,
MarginTop = 0.4f,
ScaleFactor = 1,
HeaderTitle = "Markdown Monster",
ShouldPrintHeaderandFooter = true,
ColorMode = WebViewPrintColorModes.Grayscale,
FooterUri = "https://west-wind.com"
};
// We're interested in result.ResultStream
var result = await host.PrintToPdfStreamAsync(htmlFile, pdfPrintSettings);
Assert.IsTrue(result.IsSuccess, result.Message);
Assert.IsNotNull(result.ResultStream); // MemoryStream instance
Debug.WriteLine($"Stream Length: {result.ResultStream.Length}");
// Copy resultstream to output file and display
File.Delete(outputFile);
using var fstream = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write);
result.ResultStream.CopyTo(fstream);
result.ResultStream.Close(); // Close returned stream!
ShellUtils.OpenUrl(outputFile);
Event Syntax to PDF File
var htmlFile = Path.GetFullPath("HtmlSampleFile-SelfContained.html");
var outputFile = Path.GetFullPath(@".\test.pdf");
File.Delete(outputFile);
var host = new HtmlToPdfHost();
// Callback when complete
host.OnPrintCompleteAction = (result) =>
{
if (result.IsSuccess)
{
ShellUtils.OpenUrl(outputFile);
Assert.IsTrue(true);
}
else
{
Assert.Fail(result.Message);
}
};
var pdfPrintSettings = new WebViewPrintSettings()
{
MarginBottom = 0.2F,
MarginLeft = 0.2f,
MarginRight = 0.2f,
MarginTop = 0.4f,
ScaleFactor = 0.8f
};
host.PrintToPdf(htmlFile, outputFile, pdfPrintSettings);
Event Syntax to Stream
// File or URL
var htmlFile = Path.GetFullPath("HtmlSampleFile-SelfContained.html");
var host = new HtmlToPdfHost();
// Callback on completion
host.OnPrintCompleteAction = (result) =>
{
if (result.IsSuccess)
{
// create file so we can display
var outputFile = Path.GetFullPath(@".\test1.pdf");
File.Delete(outputFile);
using var fstream = new FileStream(outputFile, FileMode.OpenOrCreate, FileAccess.Write);
result.ResultStream.CopyTo(fstream);
result.ResultStream.Close(); // Close returned stream!
ShellUtils.OpenUrl(outputFile);
Assert.IsTrue(true);
}
else
{
Assert.Fail(result.Message);
}
};
var pdfPrintSettings = new WebViewPrintSettings()
{
MarginBottom = 0.2F,
MarginLeft = 0.2f,
MarginRight = 0.2f,
MarginTop = 0.4f,
ScaleFactor = 0.8f,
};
host.PrintToPdfStream(htmlFile, pdfPrintSettings);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0-windows7.0 is compatible. |
-
net8.0-windows7.0
- Microsoft.Web.WebView2 (>= 1.0.2365.46)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Westwind.WebView.HtmlToPdf:
Package | Downloads |
---|---|
Westwind.WebView.HtmlToPdf.Extended
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.