Winnovative HTML to PDF Converter allows you to set various options to control the internal HTML viewer and PDF document properties. The most important options are listed below, grouped into several categories.
Page Layout
The page size, the width at which the HTML is laid out and the scale at which it is drawn are set together by a layout method of the converter. Each method covers one kind of document and sets the related options below; the viewer width and the zoom are computed when the PDF is generated, from the page size, the orientation and the margins in use at that moment, so these can be set before or after the call.
FitBrowserWindowToPage(PdfPageSize, PdfPageOrientation, Int32). A page of the given size, with the HTML laid out as in a browser window of the given width and scaled to the page width. A new converter uses this layout with an A4 page and a 1024 pixel window.
LayoutAtPageWidth(PdfPageSize, PdfPageOrientation, Boolean, String). A page of the given size, with the HTML laid out at the page width, without scaling, and the print media type. For templates designed for the paper size.
PrintLikeChrome(PdfPageSize, PdfPageOrientation). The output of Save as PDF in Chrome: as LayoutAtPageWidth, with 1 cm margins and no backgrounds.
SinglePageOfWidth(Int32, Int32). One page of the given width in points, as tall as the content. For receipts and tickets.
PageWidthFromBrowserWindow(Int32, Boolean). A page as wide as the browser window, with the HTML drawn 1:1, paginated or on a single page.
The page size, the orientation, the margins and the other options can be changed after a method. Setting the viewer width or the zoom after a method ends the automatic layout: the values set are used as they are, without adjusting them to the page, until a layout method is called again. The layout in use is reported by LayoutMethod. The methods, their parameters and the settings for the usual cases are described in HTML to PDF Page Setup and Scaling.
HTML Viewer Options
HTML Viewer Width. The width in pixels of the browser window in which the HTML is loaded and laid out. The corresponding property is HtmlToPdfConverterHtmlViewerWidth.
The layout methods of the converter set it: 1024 pixels by default, for the desktop layout of a page. In the default layout, FitBrowserWindowToPage, the window is scaled to the width of the PDF page; with PageWidthFromBrowserWindow the PDF page takes the width of the window, one pixel being 0.75 points, so 1024 pixels give a page 768 points wide plus the margins. Setting the property keeps the value set, as described in HTML to PDF Page Setup and Scaling.
HTML Viewer Height. The height in pixels of the browser window in which the HTML is loaded, 2048 by default. The corresponding property is HtmlToPdfConverterHtmlViewerHeight.
The height of the PDF pages does not depend on it: the converter renders the whole content whatever the window height. It matters for pages that load content when it scrolls into view, and for the single page layouts, where the page is at least as tall as the window, so a small height suits short content.
HTML Viewer Zoom. The scale, in percent, at which the HTML layout is drawn on the PDF page. The corresponding property is HtmlToPdfConverterHtmlViewerZoom. Values from 10 to 200 are supported, with decimals.
The zoom works like the zoom of a browser when it prints: at a lower zoom more content fits on a line and everything is smaller. The layout methods compute it when the PDF is generated, from the page size, the orientation and the margins: 77.47 in the default layout on A4. Setting the property replaces the computed value with the value set.
Load Lazy Images. Specifies whether lazy-loaded images are loaded during the HTML to PDF conversion process.
The corresponding property that can be set in code is HtmlToPdfConverterLoadLazyImages.
The default value is true.
These images are typically loaded by a browser only when they become visible within the viewport.
The loading behavior can be further configured using the HtmlToPdfConverterLazyImagesLoadMode property, which allows selecting between the browser’s internal mechanism and a custom loading approach. By default, Browser mode is used.
Media Type. Specifies the media type used when rendering the web page.
The corresponding property that can be set in code is HtmlToPdfConverterMediaType.
By default, the converter uses the screen media type, in every page layout; the PrintLikeChrome layout selects print, as a browser does when it prints.
This option determines which CSS media rules are applied, such as those defined for print or screen.
Conversion Delay. Specifies the additional time, in seconds, to wait for asynchronous content to load or for a page redirect to complete before starting the conversion.
The corresponding property that can be set in code is HtmlToPdfConverterConversionDelay.
The default value is 0.
This option is used when the conversion is triggered automatically.
Navigation Timeout. Specifies the navigation timeout, in seconds.
The corresponding property that can be set in code is HtmlToPdfConverterNavigationTimeout.
The default value is 120.
PDF Page Options
PDF Page Size. Specifies the page size of the generated PDF document.
The corresponding property that can be set in code is PdfDocumentOptionsPdfPageSize.
The default page size is A4.
A standard size such as A4, Letter, or Legal can be selected, or a custom page size can be defined by specifying the width and height in points (1 point = 1/72 inch).
The PDF document options are exposed through an instance of PdfDocumentOptions, which is accessible through the HtmlToPdfConverterPdfDocumentOptions property.
When PdfDocumentOptionsAutoResizePdfPageWidth is set to true, the default is false, the converter automatically adjusts the PDF page width to match the HtmlToPdfConverterHtmlViewerWidth value at the standard 96 DPI HTML rendering resolution.
When PdfDocumentOptionsAutoResizePdfPageHeight is set to true, the default is false, the PDF page height is automatically adjusted to match the full height of the HTML content.
The page has the exact size given by PdfDocumentOptionsPdfPageSize when both properties are false, the default.
PDF Page Orientation. Specifies the page orientation of the generated PDF document.
The corresponding property that can be set in code is PdfDocumentOptionsPdfPageOrientation.
The available values are Portrait and Landscape. The default value is Portrait.
The PDF document options are exposed through an instance of PdfDocumentOptions, which is accessible through the HtmlToPdfConverterPdfDocumentOptions property.
PDF Page Margins. Specifies the margins of the generated PDF document.
The corresponding properties that can be set in code are PdfDocumentOptionsLeftMargin, PdfDocumentOptionsRightMargin, PdfDocumentOptionsTopMargin and PdfDocumentOptionsBottomMargin.
The margins are expressed in points (1 point = 1/72 inch). The default value for all margins is 0.
The PDF document options are exposed through an instance of PdfDocumentOptions, which is accessible through the HtmlToPdfConverterPdfDocumentOptions property.
Auto Resize PDF Page Width. Specifies whether the PDF page width follows the browser window width. The corresponding property is PdfDocumentOptionsAutoResizePdfPageWidth. The default value is false.
When true, the page is as wide as the browser window, one pixel being 0.75 points, plus the margins, and the HTML is drawn without scaling; the page height comes from PdfDocumentOptionsPdfPageSize. This is the setting made by PageWidthFromBrowserWindow and SinglePageOfWidth. To make the page follow the browser window, prefer calling PageWidthFromBrowserWindow, which also sets the window width and the zoom. While it is true, the viewer width and the zoom are not computed by the layout methods: the values set are used, 1024 pixels and 100 percent by default. Setting it back to false restores the layout of the last layout method called. When false, the page has the size given by PdfDocumentOptionsPdfPageSize and the HTML is laid out for that page.
Auto Resize PDF Page Height. Specifies whether the PDF page is as tall as the rendered HTML content, so that the whole content is on one page. The corresponding property is PdfDocumentOptionsAutoResizePdfPageHeight. The default value is false.
The page width comes from the page size, or from the browser window when PdfDocumentOptionsAutoResizePdfPageWidth is true. The page is at least as tall as the browser window, so set a small HtmlToPdfConverterHtmlViewerHeight for short content. When false, the page height is taken from PdfDocumentOptionsPdfPageSize.
using System;
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 the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.LicenseKey = "your-license-key";
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set the initial HTML viewer height in pixels
if (model.HtmlViewerHeight.HasValue)
htmlToPdfConverter.HtmlViewerHeight = model.HtmlViewerHeight.Value;
// 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 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 media type used in @media rules when rendering HTML to PDF
htmlToPdfConverter.MediaType = model.MediaType == "Print" ? "print" : "screen";
// Set the page layout: how the width at which the HTML is laid out relates to the PDF page width
PdfPageSize pageSize = SelectedPdfPageSize(model.PdfPageSize);
PdfPageOrientation pageOrientation = SelectedPdfPageOrientation(model.PdfPageOrientation);
switch (model.PageLayout)
{
case "FitBrowserWindowToPage":
// Fixed page size: the HTML is laid out as in a browser window of the given width and the result
// is scaled to the content width of the page, so a responsive site keeps its desktop layout.
// This is the default layout of the converter, with an A4 page and a 1024 pixel window
htmlToPdfConverter.FitBrowserWindowToPage(pageSize, pageOrientation, model.HtmlViewerWidth);
break;
case "LayoutAtPageWidth":
// Fixed page size: the HTML is laid out at the content width of the page, one CSS pixel
// being 0.75 points. For HTML templates designed for the paper size, usually with the print media type
htmlToPdfConverter.LayoutAtPageWidth(pageSize, pageOrientation, mediaType: htmlToPdfConverter.MediaType);
break;
default:
// The PDF page width follows the browser window width and the HTML is drawn 1:1;
// the page height comes from the page size and the orientation
htmlToPdfConverter.PageWidthFromBrowserWindow(model.HtmlViewerWidth, singlePage: model.SinglePage);
htmlToPdfConverter.HtmlViewerZoom = model.HtmlViewerZoom;
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = pageSize;
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = pageOrientation;
break;
}
// A single page as tall as the content, with the page width of the layout. The page is at least as tall
// as the browser window, so the window height is set to 1 pixel for the page to end with the content
if (model.SinglePage)
{
htmlToPdfConverter.PdfDocumentOptions.AutoResizePdfPageHeight = true;
htmlToPdfConverter.HtmlViewerHeight = 1;
}
// Sets the PDF standard for the generated document
// Leave as None to generate a plain PDF without an accessibility structure tree or archival metadata
htmlToPdfConverter.PdfDocumentOptions.PdfStandard = model.PdfStandard;
// 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;
}
}
}