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 care enumerated below, grouped in a few categories.
HTML Viewer Options
HTML Viewer Width. This option is the equivalent in converter of the browser window width. The property you can set in your code to control the browser window width is HtmlToPdfConverterHtmlViewerWidth. When the browser window width is changed the HTML content displayed inside the window can have a different layout and something similar happens when you change the HTML Viewer width of the converter. At a given viewer width, the converter will capture by default the whole height of the HTML content
HTML Viewer Height. This option is the equivalent in converter of the browser window height and it represents the initial height of the HTML viewer. The HTML to PDF Converter will automatically determine the HTML content height and will go beyond the initial viewer height if necessary to convert the entire HTML content to PDF. The property you can set in your code to control the browser window height is HtmlToPdfConverterHtmlViewerHeight
HTML Viewer Zoom. The HTML viewer zoom percentage in HTML to PDF Converter similar to zoom level in a browser. The default value of this property is 100. The property you can set in your code to control the zoom level is HtmlToPdfConverterHtmlViewerZoom
Auto Resize Viewer Height. This options controls if the HTML viewer will be automatically resized to the HTML content height determined after the initial loading. This is useful to render web pages which load only the images which are are visible in browser window. The property you can set in your code to control the HTML Viewer height auto resize is HtmlToPdfConverterAutoResizeHtmlViewerHeight
Load Lazy Images. This option controls whether lazy images, which are typically loaded by a browser only when they become visible in the browser window, are also loaded during the HTML to PDF conversion. By default, this property is set to true and the lazy images are loaded. The property you can set in your code to control the loading of lazy images is HtmlToPdfConverterLoadLazyImages. Additionally, it is possible to choose between the browser’s internal mechanism and a custom algorithm for loading lazy images using the HtmlToPdfConverterLazyImagesLoadMode property. By default, the Browser mode is used by the converter.
Media Type. This options controls the style used by the web page if it has different styles for printing and for displaying on the screen. By default the converter uses the print media type. The browser is using the screen media type by default and this might be a reason for the eventual difference in styles between generated PDF and the browser rendering. The property you can set in your code to control the media type used for rendering the web page is HtmlToPdfConverterMediaType
Conversion Delay. This option controls additional time in seconds to wait for asynchronous items to be completely loaded or for a web page redirect to finish before starting the rendering in HTML to PDF converter. It is used when the conversion triggering mode is Auto. Default value is 0. The property you can set in your code to control conversion delay is HtmlToPdfConverterConversionDelay
Navigation Timeout. This option controls The HTML to PDF converter navigation timeout in seconds. Default value is 120. The property you can set in your code to control navigation timeout is HtmlToPdfConverterNavigationTimeout
PDF Page Options
PDF Page Size. You can set the page size of the generated PDF document to a standard size like A4, Letter, Legal, etc. or you can set custom PDF page size giving the width and height in points. 1 point is 1/72 inches. By default the PDF page size is A4. The property you can set in your code to control the PDF page size is PdfDocumentOptionsPdfPageSize. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property
PDF Page Orientation. You can set the page orientation of the generated PDF document to Portrait or to Landscape. By default the page orientation is Portrait. The property you can set in your code to control the PDF page orientation is PdfDocumentOptionsPdfPageOrientation. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property
PDF Page Margins. You can individually set the left, right, top and bottom page margins of the generated PDF document. The margins are expressed in points. 1 point is 1/72 inches. By default the PDF page margins are all 0 points. The properties you can set in your code to control the PDF page margins are PdfDocumentOptionsLeftMargin, PdfDocumentOptionsRightMargin, PdfDocumentOptionsTopMargin and PdfDocumentOptionsBottomMargin. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property
Auto Resize PDF Page Width Option. Use this option to automatically resize the PDF page width to a custom value to match the HtmlViewerWidth at the default 96 DPI resolution while the PDF page height is still taken from PdfPageSize property. The default value is true. The property you can set in your code for this option is PdfDocumentOptionsAutoResizePdfPageWidth. An object of PdfDocumentOptions type is exposed by the HtmlToPdfConverterPdfDocumentOptions property
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
// Use EVO PDF Namespace
using EvoPdf.Chromium;
namespace EvoPdf_Chromium_AspNetDemo.Controllers.HTML_to_PDF
{
public class HTML_to_PDF_Getting_StartedController : Controller
{
// GET: Getting_Started
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult ConvertHtmlToPdf(IFormCollection collection)
{
// 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 = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
// 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 = int.Parse(collection["htmlViewerWidthTextBox"]);
// Set the initial HTML viewer height in pixels
if (collection["htmlViewerHeightTextBox"][0].Length > 0)
htmlToPdfConverter.HtmlViewerHeight = int.Parse(collection["htmlViewerHeightTextBox"]);
// Set the HTML content zoom percentage similar to zoom level in a browser
htmlToPdfConverter.HtmlViewerZoom = int.Parse(collection["htmlViewerZoomTextBox"]);
// optionally auto resize HTML viewer height at the HTML content size determined after the initial loading
htmlToPdfConverter.AutoResizeHtmlViewerHeight = collection["autoResizeViewerHeightCheckBox"].Count > 0;
// optionally load the lazy images
htmlToPdfConverter.LoadLazyImages = collection["loadLazyImagesCheckBox"].Count > 0;
// Set the lazy images load mode
htmlToPdfConverter.LazyImagesLoadMode = collection["lazyImgesLoadModeDropDownList"] == "Browser" ?
LazyImagesLoadMode.Browser : LazyImagesLoadMode.Custom;
// Set the media type for which to render HTML to PDF
htmlToPdfConverter.MediaType = collection["MediaType"] == "printMediaTypeRadioButton" ? "print" : "screen";
// Set PDF page size which can be a predefined size like A4 or a custom size in points
// Leave it not set to have a default A4 PDF page
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize(collection["pdfPageSizeDropDownList"]);
// Set PDF page orientation to Portrait or Landscape
// Leave it not set to have a default Portrait orientation for PDF page
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation(collection["pdfPageOrientationDropDownList"]);
// optionally auto resize PDF page width to HTML viewer width
htmlToPdfConverter.PdfDocumentOptions.AutoResizePdfPageWidth = collection["autoResizePdfPageWidthCheckBox"].Count > 0;
// Set PDF page margins in points or leave them not set to have a PDF page without margins
htmlToPdfConverter.PdfDocumentOptions.LeftMargin = int.Parse(collection["leftMarginTextBox"]);
htmlToPdfConverter.PdfDocumentOptions.RightMargin = int.Parse(collection["rightMarginTextBox"]);
htmlToPdfConverter.PdfDocumentOptions.TopMargin = int.Parse(collection["topMarginTextBox"]);
htmlToPdfConverter.PdfDocumentOptions.BottomMargin = int.Parse(collection["bottomMarginTextBox"]);
// Set the maximum time in seconds to wait for HTML page to be loaded
// Leave it not set for a default 120 seconds maximum wait time
htmlToPdfConverter.NavigationTimeout = int.Parse(collection["navigationTimeoutTextBox"]);
// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
if (collection["conversionDelayTextBox"][0].Length > 0)
htmlToPdfConverter.ConversionDelay = int.Parse(collection["conversionDelayTextBox"]);
// The buffer to receive the generated PDF document
byte[] outPdfBuffer = null;
if (collection["HtmlPageSource"] == "convertUrlRadioButton")
{
string url = collection["urlTextBox"];
// Convert the HTML page given by an URL to a PDF document in a memory buffer
outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
}
else
{
string htmlString = collection["htmlStringTextBox"];
string baseUrl = collection["baseUrlTextBox"];
// 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 (collection["openInlineCheckBox"].Count == 0)
{
// 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;
}
}
}