How to render HTML to PDF document using Aspose.HTML for .NET
Tags: csharp, html, pdf
Published: 2017-12-24 20:49:05

---
title: How to render HTML to PDF document using Aspose.HTML for .NET
published: true
description: This article will illustrate how to convert HTML content to PDF, a feature that will prove useful, for example, in a web application.
tags: #csharp, #html, #pdf
---
In this article, we'll look at examples of how to render HTML to PDF documents using the Aspose.HTML library

First, we need to make some initial steps to prepare example project: create a Console Application project, [add Aspose.HTML library] (https://docs.aspose.com/display/htmlnet/Installation?lipi=urn%3Ali%3Apage%3Ad_flagship3_pulse_read%3BM0rDF0suQYiXE3wwXCJOAw%3D%3D), create an HTMLDocument object, and generate (or load) HTML content.

{% gist https://gist.github.com/andruhovski/3bd1dabfda15ed8bf4f57263e46a217a %}

Let's try to render PDF document with default rendering options and file to store output. In this example we use following classes: 
* [PdfRenderingOptions] (https://apireference.aspose.com/net/html/aspose.html.rendering.pdf/pdfrenderingoptions) (represents rendering options for PdfDevice), 
* [PdfDevice](https://apireference.aspose.com/net/html/aspose.html.rendering.pdf/pdfdevice) (represents a target device for rendering document), 
* [HtmlRenderer](https://apireference.aspose.com/net/html/aspose.html.rendering/htmlrenderer) (represents an HTML document renderer). 

So, to render PDF document we need to create instances of those and run the rendering process.

{% gist https://gist.github.com/andruhovski/2088bcad2d7134fee646092c29fec85c %}

We've generated a simple PDF document here, but that's not very practical. In reality we'll often have requirements about paper size, page orientation, margins, etc. There are number of configuration options that we can set to make PdfDevice more suitable for our real-world needs. All of them are listed and explained below:
* [Encryption](https://apireference.aspose.com/net/html/aspose.html.rendering.pdf/pdfrenderingoptions/properties/encryption) - gets or sets a encryption details. If not set, then no encryption will be performed;
* [JpegQuality](https://apireference.aspose.com/net/html/aspose.html.rendering.pdf/pdfrenderingoptions/properties/JpegQuality) - specifies the quality of JPEG compression for images (if JPEG compression is used);
* [PageSetup](https://apireference.aspose.com/net/html/aspose.html.rendering/renderingoptions/properties/pagesetup) - gets a page setup object is used for configuration output page-set.

Now let's talk about advanced settings PdfRenderingOptions. Assume, we need to get the document with paper size A3 and margins: top – 20mm, bottom – 15mm, left/right – 15mm and these settings will be applied for any page. In order to setup page settings and margins for any pages, we'll use [PageSetup.AnyPage](https://apireference.aspose.com/net/html/aspose.html.rendering/pagesetup/properties/anypage) property. This property is the instance of [Page class](https://apireference.aspose.com/net/html/aspose.html.drawing/page). To define the page layout we need to set up [Size](https://apireference.aspose.com/net/html/aspose.html.drawing/page/properties/size) and [Margin](https://apireference.aspose.com/net/html/aspose.html.drawing/page/properties/margin). In both cases we'll use Unit class to get appropriate measurement units. Following example shows how to set up page settings to any page:

{% gist https://gist.github.com/andruhovski/3d5d195acf6510878da778f8d30b53bb %}

The PdfRenderingOptions also offers the feature to set mirror pages and their rendering. When using this approach, we need to specify the Page settings for both Left and Right pages. Note, that values are set simultaneously for the left and right pages by calling [SetLeftRightPage](https://apireference.aspose.com/net/html/aspose.html.rendering/pagesetup/methods/setleftrightpage) method.

{% gist https://gist.github.com/andruhovski/466587b814d68e913b536ac6056c8880 %}

The last feature of PdfRenderingOptions, described in this article, is the [FirstPage](https://apireference.aspose.com/net/html/aspose.html.rendering/pagesetup/properties/firstpage) property. This property is used to set the cover page configuration and the values of this property are set similarly to AnyPage.