HTML To PDF Converter Options

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.

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

Code Sample - HTML Content Destination and Scaling in PDF

C#
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;
        }
    }
}

See Also