EVO 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.
HTML Viewer Options
HTML Viewer Width. This option defines the width of the virtual browser window used by the converter to render the HTML content.
The corresponding property that can be set in code is HtmlToPdfConverterHtmlViewerWidth.
Changing this value can affect the layout of the rendered HTML content, in the same way that resizing a browser window can change the layout of a web page. The converter uses this width as the viewport for HTML rendering before generating the PDF.
This property takes effect only when PdfDocumentOptionsAutoResizePdfPageWidth is set to true, which is the default value. In this mode, the PDF page width is automatically calculated based on HtmlToPdfConverterHtmlViewerWidth multiplied by a factor of 0.75.
The default value is 1024 px, which results in a PDF page width of 768 pt, approximately 10.66 inches.
Any PDF page margins defined in code are added to the calculated value to determine the final PDF page width.
If the rendered HTML content exceeds the specified HtmlToPdfConverterHtmlViewerWidth, it is scaled down to fit within the PDF page boundaries.
HTML Viewer Height. This option defines the height of the virtual browser window used by the converter to render the HTML content.
The corresponding property that can be set in code is HtmlToPdfConverterHtmlViewerHeight.
This value represents the initial height of the HTML rendering viewport. The converter automatically determines the full height of the HTML content and extends beyond the initial viewer height as needed to render the entire content.
This setting can influence elements that are rendered only when they become visible within the viewport, such as lazy-loaded images.
As a result, the complete HTML content is included in the generated PDF, regardless of the initial viewer height.
HTML Viewer Zoom. Specifies the zoom percentage applied to the HTML content when rendering it to PDF.
The corresponding property that can be set in code is HtmlToPdfConverterHtmlViewerZoom.
The default value is 100.
The zoom behavior depends on how the HTML content is structured.
For content with a fixed layout, for example, when the overall page width is constrained by a main container element with an explicit width, the converter may adjust the rendered output to fit the PDF page width within a certain zoom range. Outside this range, the content is scaled proportionally according to the specified zoom level. When the zoom exceeds the upper limit of this range, any content that extends beyond the right boundary of the PDF page will be truncated.
For content that can reflow based on the viewport size, the zoom is applied by scaling CSS pixels. As a result, elements such as text and images defined using pixel units are resized proportionally with the zoom level.
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.
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, which is the default, 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.
To generate a PDF with the exact page size specified by PdfDocumentOptionsPdfPageSize, both PdfDocumentOptionsAutoResizePdfPageWidth and PdfDocumentOptionsAutoResizePdfPageHeight must be set to false.
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 is automatically adjusted based on the HtmlToPdfConverterHtmlViewerWidth value.
The corresponding property that can be set in code is PdfDocumentOptionsAutoResizePdfPageWidth.
The default value is true.
When enabled, the PDF page width is calculated from HtmlToPdfConverterHtmlViewerWidth, scaled by a factor of 0.75 to account for the 96 DPI HTML rendering resolution. The PDF page height remains based on the PdfDocumentOptionsPdfPageSize setting.
When disabled, the PDF page width is taken directly from PdfDocumentOptionsPdfPageSize.
Auto Resize PDF Page Height. Specifies whether the PDF page height is automatically adjusted to match the height of the rendered HTML content.
The corresponding property that can be set in code is PdfDocumentOptionsAutoResizePdfPageHeight.
The default value is false.
When enabled, the PDF page height is calculated based on the HTML content height at the standard 96 DPI rendering resolution. The final page height is also influenced by the HtmlToPdfConverterHtmlViewerHeight property, which defines the initial rendering viewport height. As a result, the PDF page height may be greater than the actual content height if the viewer height is larger.
In this mode, the entire content is rendered on a single PDF page with the calculated height.
When disabled, the PDF page height is taken from the PdfDocumentOptionsPdfPageSize property.
To generate a PDF with the exact page size specified by PdfDocumentOptionsPdfPageSize, both PdfDocumentOptionsAutoResizePdfPageWidth and PdfDocumentOptionsAutoResizePdfPageHeight must be set to false.
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using EvoPdf_Next_AspNetDemo.Models;
using EvoPdf_Next_AspNetDemo.Models.HTML_to_PDF;
// Use EVO PDF Namespace
using EvoPdf.Next;
namespace EvoPdf_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;
// 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;
}
}
}