Convert HTML to PDF | C#

In this guide, you find information on how to convert an HTML document into a Portable Document Format (PDF) file format. HTML to PDF conversion is often required to establish limited access to documents editing or copying; to produce official documentation or send some information, for example, by email. In this article, you find information on how to convert HTML to PDF using ConvertHTML() methods of the Converter class, and how to apply PdfSaveOptions and ICreateStreamProvider parameters.

Any conversion you want to perform involves loading an HTML document and saving it in PDF format. It can be different scenarios but it can be made with a few required steps:

  1. Load an HTML document into a Document object using one of HTMLDocument() constructors. You can load HTML from a file, HTML code, stream, or URL (see the Creating an HTML Document article).
  2. Initialize a PdfSaveOptions object.
  3. Invoke one of the ConvertHTML() methods and pass the required parameters to it.

Text “Banner HTML to PDF Converter”

HTML to PDF by a single line of code

HTML to PDF conversion is very popular. To perform this feature, Aspose.HTML for .NET offer the static methods of the Converter class as an understandable and straightforward way to convert HTML code into a PDF file literally with a single line of code!

using System.IO;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
     // Invoke the ConvertHTML method to convert the HTML code to PDF           
     Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));

Convert HTML to PDF

Converting a file to another format using the ConvertHTML() method is a sequence of operations among which document loading and saving:

  1. Load an HTML file using one of HTMLDocument() constructors of the HTMLDocument class. In the example we use HTMLDocument(string) constructor that initializes an HTML document from a file.
  2. Create a new PdfSaveOptions object.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a PDF file. You need to pass the HTMLDocument, PdfSaveOptions, and output file path to the ConvertHTML() method to convert HTML to PDF.

Please take a look over the following C# code snippet that shows the HTML to PDF conversion process for the spring.html file.

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "spring.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "spring-output.pdf");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize PdfSaveOptions 
    var options = new PdfSaveOptions();
    
    // Convert HTML to PDF
    Converter.ConvertHTML(document, options, savePath);    

The figure illustrates the spring-output.pdf file.

Text “spring-output.pdf image”

Save Options

Aspose.HTML allows converting HTML to PDF using default or custom save options. PdfSaveOptions usage enables you to customize the rendering process; you can specify the page size, margins, file permissions, Css, etc.

Property Description
JpegQuality Specifies the quality of JPEG compression for images. The default value is 95.
Css Gets a CssOptions object which is used for configuration of CSS properties processing.
DocumentInfo This property contains information about the output PDF document.
BackgroundColor This property sets the color that will fill the background of every page. By default, this property is Transparent.
PageSetup This property gets a page setup object and uses it for configuration output page-set.
HorizontalResolution Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolution Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
Encryption This property gets or sets encryption details. If it is not set, then no encryption will be performed.

Convert HTML to PDF using PdfSaveOptions

With Aspose.HTML, you can convert files programmatically with full control over a wide range of conversion parameters. To convert HTML to PDF with PdfSaveOptions specifying, you should follow a few steps:

  1. Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class (drawing.html).
  2. Create a new PdfSaveOptions object and specify the required properties.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a PDF file. You need to pass the HTMLDocument, PdfSaveOptions, and output file path to the ConvertHTML() method to convert HTML to PDF.

The following example shows how to use PdfSaveOptions and create a PDF file with custom save options:

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
using System.Drawing;
using Aspose.Html.Drawing;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "drawing.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "drawing-options.pdf");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize PdfSaveOptions. Set up the page-size 600x300 pixels, margins, resolutions and change the background color to AliceBlue 
    var options = new PdfSaveOptions()
    {                
        HorizontalResolution = 200,
        VerticalResolution = 200,
        JpegQuality = 100,
        BackgroundColor = Color.AliceBlue                
    };
    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 300), new Margin(20, 10, 10, 10));     
    
    // Convert HTML to PDF
    Converter.ConvertHTML(document, options, savePath);

The PdfSaveOptions() constructor initializes an instance of the PdfSaveOptions class that is passed to ConvertHTML() method. The ConvertHTML() method takes the document, options, output file path savePath and performs the conversion operation. The PdfSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to PDF format.

In the above example, we use:

  • JpegQuality property that enables you to specify the quality of JPEG compression for images;
  • BackgroundColor property that sets the color that will fill the background. The default BackgroundColor is Transparent;
  • HorizontalResolution and VerticalResolution properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi;
  • PageSetup property that specifies the page size and margins.

The figure illustrates the drawing-options.pdf file.

Text “drawing-options.pdf image”

Output Stream Providers

If it is required to save files in the remote storage (e.g., cloud, database, etc.) you can implement ICreateStreamProvider interface to have manual control over the file creating process. This interface is designed as a callback object to create a stream at the beginning of the document/page (depending on the output format) and release the early created stream after rendering the document/page.

The example below shows how to implement and use your own MemoryStreamProvider in the application:

using System.IO;
using System.Collections.Generic;
...
    class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
    {
        // List of MemoryStream objects created during the document rendering
        public List<MemoryStream> Streams { get; } = new List<MemoryStream>();

        public Stream GetStream(string name, string extension)
        {
            // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats.
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public Stream GetStream(string name, string extension, int page)
        {
            // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public void ReleaseStream(Stream stream)
        {
            //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
        }
    
        public void Dispose()
        {
            // Releasing resources
            foreach (var stream in Streams)
                stream.Dispose();
        }
    }
using System.IO;
using Aspose.Html;
using System.Linq;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
...
    // Create an instance of MemoryStreamProvider
    using var streamProvider = new MemoryStreamProvider();

    // Initialize an HTML document
    using var document = new HTMLDocument(@"<h1>Convert HTML to PDF File Format!</h1>", ".");
    
    // Convert HTML to PDF using the MemoryStreamProvider
    Converter.ConvertHTML(document, new PdfSaveOptions(), streamProvider);
    
    // Get access to the memory stream that contains the result data
    var memory = streamProvider.Streams.First();
    memory.Seek(0, SeekOrigin.Begin);
    
    // Flush the result data to the output file
    using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
    {
        memory.CopyTo(fs);
    }