Westwind.WebView.HtmlToPdf 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Westwind.WebView.HtmlToPdf --version 0.4.0
NuGet\Install-Package Westwind.WebView.HtmlToPdf -Version 0.4.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="Westwind.WebView.HtmlToPdf" Version="0.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Westwind.WebView.HtmlToPdf --version 0.4.0
#r "nuget: Westwind.WebView.HtmlToPdf, 0.4.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 Westwind.WebView.HtmlToPdf as a Cake Addin
#addin nuget:?package=Westwind.WebView.HtmlToPdf&version=0.4.0

// Install Westwind.WebView.HtmlToPdf as a Cake Tool
#tool nuget:?package=Westwind.WebView.HtmlToPdf&version=0.4.0

Html to PDF using WebView on Windows

<a href="https://www.nuget.org/packages/Westwind.WebView.HtmlToPdf/">alternate text is missing from this package README image</a> alternate text is missing from this package README image

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

This library uses the built-in WebView2 Runtime in Windows so it has no external dependencies for your applications assuming you are running on a recent version of Windows that has the WebView2 Runtime installed.

Prerequisites

The components works with:

  • Windows 11/10 Server 2019/2022
  • Apps that target net8.0-windows or net6.0-windows
  • Desktop Applications
  • Console Applications
  • Service Application

The component does not support:

  • Non Windows platforms

Targets:

  • net8.0-windows
  • net6.0-windows
  • net472

Dependencies

Deployed applications have the following dependencies:

  • WebView2 Runtime
    On recent updates of Windows 11 and 10, the WebView is pre-installed as a system component. On Servers however, you may have to explicitly install the WebView Runtime.

  • Windows Desktop Runtime
    The WebView2 component is dependent on Windows Desktop Runtime libraries and therefore requires the Desktop runtime to be installed even for server applications.

Using the library

You can install the library from NuGet:

PS> install-package westwind.webview.htmltopdf

or:

dotnet add package westwind.webview.htmltopdf

The library has 4 separate output methods:

  • PrintToPdf() - Prints to file with a Callback
  • PrintToPdfStream() - Prints and returns a result.ResultStream in a Callback
  • PrintToPdfAsync() - Runs async to create a PDF file and waits for completion
  • PrintToPdfStreamAsync() - Runs async and returns a result.ResultStream

All of the methods take a file or Url as input. File names have to be fully qualified with a path. Output to file requires that you provide a filename.

All requests return a PdfPrintResult structure which has a IsSuccess flag you can check. For stream results, the ResultStream property will be set with a MemoryStream instance on success. Errors can use the Message or LastException to retrieve error information.

Async Call Syntax for File Output

// Url or full qualified file path
var htmlFile = Path.GetFullPath("HtmlSampleFileLonger-SelfContained.html");
var outputFile = Path.GetFullPath(@".\test2.pdf");
File.Delete(outputFile);

var host = new HtmlToPdfHost();
var result = await host.PrintToPdfAsync(htmlFile, outputFile);

Assert.IsTrue(result.IsSuccess, result.Message);
ShellUtils.OpenUrl(outputFile);  // display the PDF file

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()
{                
    // default margins are 0.4F
    MarginBottom = 0.2F,
    MarginLeft = 0.2f,
    MarginRight = 0.2f,
    MarginTop = 0.4f,
    ScaleFactor = 0.8F 
    ShouldPrintHeaderAndFooter = true,
    HeaderTitle = "Blog Post Title"
};

// We're interested in result.ResultStream
var result = await host.PrintToPdfStreamAsync(htmlFile, pdfPrintSettings);

Assert.IsTrue(result.IsSuccess, result.Message);
Assert.IsNotNull(result.ResultStream); // This is what we're after

Debug.WriteLine($"Stream Length: {result.ResultStream.Length}");

// Copy resultstream to output file so we can display it
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()
{
    // default margins are 0.4F
    MarginBottom = 0.2F,
    MarginLeft = 0.2f,
    MarginRight = 0.2f,
    MarginTop = 0.4f,
    ScaleFactor = 0.8f,
    PageRanges = "1,2,5-7"
};
host.PrintToPdf(htmlFile, outputFile, pdfPrintSettings);

// make sure app keeps running

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);

// make sure app keeps running

The Task based methods are easiest to use so that's the recommended syntax. The event based methods are there so you can more easily use this if you are not running in some sort of async environment already. Both approaches run on a separate STA thread to ensure that the WebView can run regardless of whether you are running inside of an application that has a main UI/STA thread.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
0.6.0 98 4/7/2024
0.5.0 205 3/31/2024
0.4.0 89 3/27/2024
0.3.0 87 3/27/2024
0.2.0 87 3/26/2024
0.1.0 93 3/25/2024