HiQPdf Next HTML to PDF Converter for .NET

HiQPdf Next HTML to PDF Converter for .NET Logo

The HTML to PDF Converter is a core component of the HiQPdf Next Library for .NET, using a Chromium-based rendering engine that enables accurate conversion of complex web pages to PDF with the latest HTML, CSS and JavaScript standards and technologies.

You can deploy the library on a variety of Windows and Linux platforms, including Azure App Service and Functions or Docker.

Download Now
Online Demo
Documentation

The HTML to PDF Converter is a core component of the HiQPdf Next Library for .NET, using a Chromium-based rendering engine that enables accurate conversion of complex web pages to PDF with the latest HTML, CSS and JavaScript standards and technologies. You can see the list of all HiQPdf Next components on the library page.

Compatibility Platforms

HiQPdf Next for .NET can run on a variety of Windows and Linux platforms in web, console and desktop applications across all modern .NET platforms. The library components can be used in Azure App Service and Azure Functions environments on both Windows and Linux. Deployment to Docker Windows and Linux containers is also supported.
The .NET library targets .NET Standard 2.0, which makes it compatible with a wide range of .NET Core and .NET Framework applications.

Getting Started

The online documentation contains Getting Started guides for Windows, Linux, Azure App Service and Azure Functions, with detailed instructions for integrating the library into your application and complete C# examples for each important feature of the library.
You can see the current capabilities of the library by checking the online demo application for this library and the API reference in the online documentation.

Download Demo Application

You can also download a free trial package for .NET, which includes an ASP.NET demo application project with complete C# source code as a starting point for experimenting with your own usage scenarios.
Running the samples in the demo application that involve HTML to PDF conversion features on Linux platforms might require installing some dependency packages. The documentation includes an entire section dedicated to building, publishing and running the demo application on multiple platforms.

NuGet Packages

For Windows deployments add a reference to the HiQPdf.Next.HtmlToPdf.Windows NuGet Package and for Linux deployments add a reference to the HiQPdf.Next.HtmlToPdf.Linux NuGet Package.
The package for Windows is referenced by the HiQPdf.Next.Windows metapackage for all components and the package for Linux is referenced by the HiQPdf.Next.Linux metapackage for all components.
There are also multiplatform metapackages that reference both the Windows and Linux HTML to PDF packages: HiQPdf.Next.HtmlToPdf for the HTML to PDF functionality and HiQPdf.Next for the entire HiQPdf Next library.

Installation

The HTML to PDF Converter component uses a platform specific runtime. On Windows platforms, the runtime generally does not require the installation of additional dependencies. On Linux platforms installing some dependency packages might be necessary, depending on the exact version of Linux you are using. In online documentation in the Getting Started and Publish guides you can find instructions about Linux dependencies installation on a variety of Linux platforms.

HiQPdf.Next Namespace

All components of the HiQPdf Next for .NET library share the same HiQPdf.Next namespace and can be used together in the same application. To use the library in your own code, add the using directive at the top of your C# source file, as shown below.
// Include the HiQPdf.Next namespace at the top of your C# file
using HiQPdf.Next;

Sample Code

After you add the reference to the NuGet package to your project, use the sample code below to convert a URL to a PDF document that you can either save to a file or send to the browser for download.
 // Create the HTML to PDF converter object
 HtmlToPdf converter = new HtmlToPdf();
 
 // Convert the HTML code to memory
 string urlToConvert = "http://www.hiqpdf.com";
 byte[] urlToPdfData = converter.ConvertUrlToMemory(urlToConvert);
 
 FileResult fileResult = new FileContentResult(urlToPdfData, "application/pdf");
 fileResult.FileDownloadName = "url_to_pdf.pdf";
 return fileResult;

Features List

HiQPdf Next for .NET offers advanced options for converting HTML to PDF and HTML to images. The library is more than just an HTML to PDF converter. It can also automatically generate a PDF document outline with bookmarks, set security permissions, password protect and digitally sign the generated PDF document.

Support for Cutting-Edge HTML, CSS and JavaScript Features

Using Chromium as the rendering engine ensures that the latest modern HTML, CSS, and JavaScript features are supported by the converter in conformance with the latest standards and technologies.

Available on Both Windows and Linux Platforms

HiQPdf Next for .NET can run on both Windows 64-bit and Linux 64-bit platforms. There are different NuGet packages for Windows and Linux, including the same .NET library but with different native runtimes. For Windows, the minimum required version is Windows 10 or Windows Server 2016.

Built for .NET Standard 2.0 for Maximum Compatibility

The .NET library targets .NET Standard 2.0, making it compatible with a wide range of .NET Core and .NET Framework applications. It is compatible with .NET 10.0, 9.0, 8.0, 7.0, 6.0, .NET Standard 2.0 and .NET Framework 4.6.2 to 4.8.1.

Fully Compatible with Azure App Service and Azure Functions on Both Windows and Linux

The converter can run without restrictions in your Azure App Service and Azure Functions .NET Core applications targeting both Windows and Linux platforms. Web fonts and other features are fully supported by HiQPdf Next for .NET. Online documentation offers detailed usage instructions for Azure applications targeting both Windows and Linux.

NuGet Packages for Windows and Linux

HiQPdf Next for .NET is delivered as NuGet packages for Windows and Linux. The packages include the .NET Standard 2.0 library, the same for both platforms, and the specific native runtime for each platform.
ASP.NET Core Demo Application with C# Code for All Features
The zip package that can be downloaded from the website contains the project for the ASP.NET Core demo application with C# sample code for all major library features.

Simple and Flexible Licensing with a Single License for All Libraries

The license for HiQPdf Next for .NET works with both the classic HiQPdf Library for .NET and the multi-platform client-server solution. There are no additional runtime or deployment costs charged for using our software component in your applications.

Convert complex HTML with CSS and JavaScript to PDF ...

Support for the latest HTML and JavaScript standards

Merge Multiple HTML documents in the same PDF document

Create headers and footers with page numbers using HTML

Automatically repeat HTML tables headers and footers in PDF pages

Control PDF page breaks with CSS attributes in HTML

Create PDF document outline from HTML heading tags

Create a table of contents from HTML heading tags

Create tagged PDFs to support the accessibility features

Convert the regions from the HTML page selected by a CSS selector

Exclude regions from the HTML page selected by a CSS selector

Retrieve HTML Element Positions in PDF ...

Control the media type to render for screen or for print mode

Trigger conversion automatically or manually from JavaScript code

Control the PDF document display with PDF viewer preferences

Create secured and digitally signed PDF documents

Use HTTP GET or POST Requests to Access the HTML to Convert

Set HTTP headers when requesting the HTML page

Set cookies when requesting the HTML page

Convert HTML pages that require authentication

Convert HTML to JPEG, PNG and WEBP image formats

Asynchronous Convert and Save Functions

HiQPdf Next for .NET - HTML to PDF C# Code Sample for ASP.NET Core

using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using HiQPdf_Next_AspNetDemo.Models;

using HiQPdf.Next;

namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
    public class ConvertHtmlToPdfController : Controller
    {
        IWebHostEnvironment m_hostingEnvironment;
        public ConvertHtmlToPdfController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        // GET: ConvertHtmlToPdf
        public ActionResult Index()
        {
            var model = SetViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult ConvertToPdf(ConvertHtmlToPdfViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
                throw new ValidationException(errorMessage);
            }

            // Replace the demo serial number with the serial number received upon purchase
            // to run the converter in licensed mode
            Licensing.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            // Create an HTML to PDF converter object with default settings
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // Set browser width
            htmlToPdfConverter.BrowserWidth = model.BrowserWidth;

            // Set browser height if specified, otherwise use the default
            if (model.BrowserHeight.HasValue)
                htmlToPdfConverter.BrowserHeight = model.BrowserHeight.Value;

            // Set browser zoom
            htmlToPdfConverter.BrowserZoom = model.BrowserZoom;

            // Set an additional delay, in seconds, to wait for asynchronous content after the initial load
            // The default value is 0
            htmlToPdfConverter.WaitBeforeConvert = model.WaitTime;

            // Set the maximum time, in seconds, to wait for the HTML page to load
            // The default value is 120 seconds
            htmlToPdfConverter.HtmlLoadedTimeout = model.LoadHtmlTimeout;

            // 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 BrowserWidth property
            // The default value is true
            htmlToPdfConverter.Document.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 BrowserWidth
            // when the AutoResizePdfPageWidth property is true
            htmlToPdfConverter.Document.PageSize = GetSelectedPageSize(model.PageSize);

            // Set the PDF page orientation to Portrait or Landscape. The default is Portrait
            htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation(model.PageOrientation);

            // Set the PDF page margins in points. The default is 0
            htmlToPdfConverter.Document.Margins = new PdfMargins(
                        model.LeftMargin, model.RightMargin,
                        model.TopMargin, model.BottomMargin);

            // Set the document security
            htmlToPdfConverter.Document.Security.OpenPassword = model.OpenPassword;
            htmlToPdfConverter.Document.Security.AllowPrinting = model.AllowPrinting;

            // Convert HTML to PDF
            byte[] pdfBuffer = null;

            if (model.UrlOrHtmlCode == "ConvertUrl")
            {
                // Convert URL to a PDF memory buffer
                string url = model.Url;

                pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
            }
            else
            {
                // Convert HTML code
                string htmlCode = model.HtmlCode;
                string baseUrl = model.BaseUrl;

                // convert HTML code to a PDF memory buffer
                pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
            }

            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
            if (!model.OpenInline)
                fileResult.FileDownloadName = "HtmlToPdf.pdf";

            return fileResult;
        }

        private PdfPageSize GetSelectedPageSize(string pageSize)
        {
            switch (pageSize)
            {
                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 GetSelectedPageOrientation(string pageOrientation)
        {
            return pageOrientation == "Portrait" ?
                PdfPageOrientation.Portrait : PdfPageOrientation.Landscape;
        }

        private ConvertHtmlToPdfViewModel SetViewModel()
        {
            var model = new ConvertHtmlToPdfViewModel();
            return model;
        }
    }
}