EVO PDF Logo

HTML to PDF Elements

EVO PDF Client for .NET Core

EVO HTML to PDF Converter allows you to convert a HTML page to PDF by adding a EvoPdfClientHtmlToPdfElement object at a given location in a PDF document. You can add multiple HTML to PDF Elements in the same PDF document.

A HtmlToPdfElement allows you to set most of the HTML to PDF conversion options available when converting HTML to PDF using the HtmlToPdfConverter class. The most important options controlling the HTML to PDF conversion using a HtmlToPdfElement are 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 and represents the preferred width in which to render the HTML content. The property you can set in your code to control the browser window width is HtmlToPdfElementHtmlViewerWidth. 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. The actual HTML content width can be larger in case the HTML page cannot be entirely displayed in the given viewer width. The HTML content can be further scaled to fit the PDF page based on the converter scaling options set in your application. The HTML content size is expressed in pixels and the PDF page size is in expressed in points, 1 point being 1/72 inches. The converter is using a 96 DPI resolution to transform pixels to points with the following formula: Points = Pixels/96 * 72

  • HTML Viewer Height. This option is the equivalent in converter of the browser window height and can be used to limit the conversion to the top part of the HTML page. If this property is not set the entire page will be converted. The property you can set in your code to control the browser window height is HtmlToPdfElementHtmlViewerHeight

  • Clip HTML Content Width. You can set this option to force the HTML content width to be exactly HtmlViewerWidth pixels. If this option is false then actual HTML content width can be larger than HtmlViewerWidth pixels in case the HTML page cannot be entirely displayed in the given viewer width. By default this option is false and the HTML content is not clipped. The property you can set in your code for this option is HtmlToPdfElementClipHtmlView

HTML Content Options

Code Sample - Add HTML to PDF Elements to a PDF Document

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

// Use EVO PDF Namespace
using EvoPdfClient;

namespace EvoHtmlToPdfDemo.Controllers.PDF_Creator
{
    public class PDF_Creator_HTML_to_PDF_ElementsController : Controller
    {
        // GET: PDF_Creator_HTML_to_PDF_Elements
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            // Get the server options
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string servicePassword = collection["textBoxServicePassword"];
            bool useServicePassword = servicePassword.Length > 0;
            bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService";
            string webServiceUrl = collection["textBoxWebServiceUrl"];

            // Create a PDF document
            Document pdfDocument = null;
            if (useTcpService)
                pdfDocument = new Document(serverIP, serverPort);
            else
                pdfDocument = new Document(true, webServiceUrl);

            // Set optional service password
            if (useServicePassword)
                pdfDocument.ServicePassword = servicePassword;

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            // Create a PDF page where to add the first HTML
            PdfPage firstPdfPage = pdfDocument.AddPage();

            // The element location in PDF
            float xLocation = float.Parse(collection["xLocationTextBox"]);
            float yLocation = float.Parse(collection["yLocationTextBox"]);

            // The URL of the HTML page to convert to PDF
            string urlToConvert = collection["urlTextBox"];

            // Create the HTML to PDF element
            HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(xLocation, yLocation, urlToConvert);

            // Optionally set the HTML viewer width
            htmlToPdfElement.HtmlViewerWidth = int.Parse(collection["htmlViewerWidthTextBox"]);

            // Optionally set the HTML viewer height
            if (collection["htmlViewerHeightTextBox"][0].Length > 0)
                htmlToPdfElement.HtmlViewerHeight = int.Parse(collection["htmlViewerHeightTextBox"]);

            // Optionally set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
            htmlToPdfElement.ClipHtmlView = collection["clipContentCheckBox"].Count > 0;

            // Optionally set the destination width in PDF
            if (collection["contentWidthTextBox"][0].Length > 0)
                htmlToPdfElement.Width = float.Parse(collection["contentWidthTextBox"]);

            // Optionally set the destination height in PDF
            if (collection["contentHeightTextBox"][0].Length > 0)
                htmlToPdfElement.Height = float.Parse(collection["contentHeightTextBox"]);

            // Optionally set a delay before conversion to allow asynchonous scripts to finish
            htmlToPdfElement.ConversionDelay = 2;

            // Add the HTML to PDF element to PDF document
            // The AddElementResult contains the bounds of the HTML to PDF Element in last rendered PDF page
            // such that you can start a new PDF element right under it
            firstPdfPage.AddElement(htmlToPdfElement);

            // Save the PDF document in a memory buffer
            byte[] outPdfBuffer = pdfDocument.Save();

            // Send the PDF file to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "HTML_to_PDF_Elements.pdf";

            return fileResult;
        }
    }
}
See Also

Other Resources