The HTML to PDF Converter component allows you to convert HTML and SVG to PDF and to images.
This component is bundled and distributed as two separate NuGet packages for Windows and Linux, each including the same .NET Standard 2.0 library and different native runtimes. Targeting .NET Standard 2.0 makes the packages compatible with a wide range of .NET Core and .NET Framework versions.
You can reference the Winnovative.Pdf.Next.HtmlToPdf.Windows NuGet package in applications running on Windows or the Winnovative.Pdf.Next.HtmlToPdf.Linux NuGet package in applications running on Linux to enable HTML to PDF conversion in your application. The package for Windows is referenced by the Winnovative.Pdf.Next.Windows metapackage for all components, and the package for Linux is referenced by the Winnovative.Pdf.Next.Linux metapackage for all components.
There are also multiplatform metapackages that reference both the Windows and Linux HTML to PDF packages: Winnovative.Pdf.Next.HtmlToPdf for the HTML to PDF functionality and Winnovative.Pdf.Next for the entire Winnovative PDF Next library.
The main functionality of the component is to convert HTML to PDF, HTML to image and SVG to PDF, but it offers much more. You can automatically generate a PDF document outline and table of contents, set permissions, password-protect or digitally sign the generated PDF document.
You can use one of the HtmlToPdfConverter class methods to convert a URL or an HTML string to a PDF document.
The class is defined in the Core component of the library, but using it requires the Core runtime. The HTML to PDF NuGet packages described above correctly reference both the Core assembly and the Core runtime in your application.
The resulted PDF document can be:
produced in a memory buffer using the HtmlToPdfConverterConvertUrl(String) and HtmlToPdfConverterConvertHtml(String, String) methods
saved in a file on disk using the HtmlToPdfConverterConvertUrlToFile(String, String) and HtmlToPdfConverterConvertHtmlToFile(String, String, String) methods
There are also asynchronous variants of these methods that follow the Task-based Asynchronous Pattern (TAP) in .NET, allowing HTML to PDF conversion to run in parallel using async and await. These methods share the same names as their synchronous counterparts and include the "Async" suffix. They also accept an optional System.ThreadingCancellationToken parameter that can be used to cancel the conversion operation where applicable.
The HtmlToPdfConverterConvertUrlAsync(String, CancellationToken) and HtmlToPdfConverterConvertHtmlAsync(String, String, CancellationToken) methods can be used to generate a PDF document from a URL or from an HTML string held in memory.
The HtmlToPdfConverterConvertUrlToFileAsync(String, String, CancellationToken) and HtmlToPdfConverterConvertHtmlToFileAsync(String, String, String, CancellationToken) methods can be used to generate a PDF document from a URL or from an HTML string and save it directly to a file.
You can find an example of how to use the asynchronous HTML to PDF conversion methods in the Convert Multiple HTML Pages to PDF in Parallel topic.
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Winnovative_Next_AspNetDemo.Models;
using Winnovative_Next_AspNetDemo.Models.HTML_to_PDF;
// Use Winnovative Namespace
using Winnovative.Pdf.Next;
namespace Winnovative_Next_AspNetDemo.Controllers.HTML_to_PDF
{
public class HTML_to_PDF_Getting_StartedController : Controller
{
// GET: Getting_Started
public ActionResult Index()
{
var model = new HTML_to_PDF_Getting_Started_ViewModel();
return View(model);
}
[HttpPost]
public ActionResult ConvertHtmlToPdf(HTML_to_PDF_Getting_Started_ViewModel model)
{
if (!ModelState.IsValid)
{
var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
throw new ValidationException(errorMessage);
}
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the library in demo mode
Licensing.LicenseKey = "3FJDU0ZDU0NTQkddQ1NAQl1CQV1KSkpKU0M=";
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
htmlToPdfConverter.HtmlViewerWidth = model.HtmlViewerWidth;
// Set the initial HTML viewer height in pixels
if (model.HtmlViewerHeight.HasValue)
htmlToPdfConverter.HtmlViewerHeight = model.HtmlViewerHeight.Value;
// Set the HTML content zoom percentage similar to zoom level in a browser
htmlToPdfConverter.HtmlViewerZoom = model.HtmlViewerZoom;
// Optionally load the lazy images
htmlToPdfConverter.LoadLazyImages = model.LoadLazyImages;
// Set the lazy images load mode
htmlToPdfConverter.LazyImagesLoadMode = model.LazyImagesLoadMode == "Browser" ?
LazyImagesLoadMode.Browser : LazyImagesLoadMode.Custom;
// Set the media type used in @media rules when rendering HTML to PDF
htmlToPdfConverter.MediaType = model.MediaType == "Print" ? "print" : "screen";
// Automatically resize the PDF page width to match the HtmlViewerWidth property
// The default value is true
htmlToPdfConverter.PdfDocumentOptions.AutoResizePdfPageWidth = model.AutoResizePdfPageWidth;
// Set the PDF page size, which can be a predefined size like A4 or a custom size in points
// The default is A4
// Important Note: The PDF page width is automatically determined from the HTML viewer width
// when the AutoResizePdfPageWidth property is true
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize(model.PdfPageSize);
// Set the PDF page orientation to Portrait or Landscape. The default is Portrait
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation(model.PdfPageOrientation);
// Set the PDF page margins in points. The default is 0
htmlToPdfConverter.PdfDocumentOptions.LeftMargin = model.LeftMargin;
htmlToPdfConverter.PdfDocumentOptions.RightMargin = model.RightMargin;
htmlToPdfConverter.PdfDocumentOptions.TopMargin = model.TopMargin;
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = model.BottomMargin;
// Set the maximum time, in seconds, to wait for the HTML page to load
// The default value is 120 seconds
htmlToPdfConverter.NavigationTimeout = model.NavigationTimeout;
// Set an additional delay, in seconds, to wait for asynchronous content after the initial load
// The default value is 0
if (model.ConversionDelay.HasValue)
htmlToPdfConverter.ConversionDelay = model.ConversionDelay.Value;
// The buffer to receive the generated PDF document
byte[] outPdfBuffer = null;
if (model.HtmlPageSource == "Url")
{
string url = model.Url;
// Convert the HTML page given by an URL to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
}
else
{
string htmlString = model.HtmlString;
string baseUrl = model.BaseUrl;
// Convert a HTML string with a base URL to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlString, baseUrl);
}
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
if (!model.OpenInline)
{
// send as attachment
fileResult.FileDownloadName = "HTML_to_PDF_Getting_Started.pdf";
}
return fileResult;
}
private PdfPageSize SelectedPdfPageSize(string selectedValue)
{
switch (selectedValue)
{
case "A0":
return PdfPageSize.A0;
case "A1":
return PdfPageSize.A1;
case "A10":
return PdfPageSize.A10;
case "A2":
return PdfPageSize.A2;
case "A3":
return PdfPageSize.A3;
case "A4":
return PdfPageSize.A4;
case "A5":
return PdfPageSize.A5;
case "A6":
return PdfPageSize.A6;
case "A7":
return PdfPageSize.A7;
case "A8":
return PdfPageSize.A8;
case "A9":
return PdfPageSize.A9;
case "ArchA":
return PdfPageSize.ArchA;
case "ArchB":
return PdfPageSize.ArchB;
case "ArchC":
return PdfPageSize.ArchC;
case "ArchD":
return PdfPageSize.ArchD;
case "ArchE":
return PdfPageSize.ArchE;
case "B0":
return PdfPageSize.B0;
case "B1":
return PdfPageSize.B1;
case "B2":
return PdfPageSize.B2;
case "B3":
return PdfPageSize.B3;
case "B4":
return PdfPageSize.B4;
case "B5":
return PdfPageSize.B5;
case "Flsa":
return PdfPageSize.Flsa;
case "HalfLetter":
return PdfPageSize.HalfLetter;
case "Ledger":
return PdfPageSize.Ledger;
case "Legal":
return PdfPageSize.Legal;
case "Letter":
return PdfPageSize.Letter;
case "Letter11x17":
return PdfPageSize.Letter11x17;
case "Note":
return PdfPageSize.Note;
default:
return PdfPageSize.A4;
}
}
private PdfPageOrientation SelectedPdfPageOrientation(string selectedValue)
{
return selectedValue == "Portrait" ? PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
}
}
}