How to convert HTML to PDF using Chromium

Creating PDF files programmatically is a common requirement in many applications. This guide will demonstrate how to convert an HTML file to a PDF using the open-source Chromium Embedded Framework (CefSharp) in a C# project with Visual Studio on Windows OS. This tutorial is designed for beginners and includes detailed steps, example code, and guidance to ensure you can follow along easily.

Overview of Chromium and CefSharp

Chromium is an open-source web browser project that forms the basis for Google Chrome. Its robust rendering engine can be leveraged for tasks beyond just web browsing, such as creating PDFs from HTML. CefSharp (Chromium Embedded Framework for .NET) is a .NET wrapper around Chromium, making integrating Chromium’s capabilities into .NET applications easy. CefSharp provides WinForms and WPF controls for embedding a web browser and an off-screen mode for tasks like rendering HTML to PDF.

Using CefSharp's off-screen mode, we can programmatically load HTML files and generate PDF outputs. This guide focuses on the OffScreen package of CefSharp, which is optimized for background tasks like HTML rendering without a visible UI.

Prerequisites

Before starting, ensure your environment is set up correctly:

  1. Basic Knowledge of C# (Optional)

    This guide is for beginners, but familiarity with basic concepts like variables, methods, and namespaces can be helpful. If you're new to C#, consider reviewing the C# documentation.

  2. HTML File to Convert

    Prepare an HTML file for conversion to PDF. For testing purposes, you can create a simple Sample.html file with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sample HTML</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        h1 { color: #007BFF; }
    </style>
</head>
<body>
    <h1>Hello, PDF!</h1>
    <p>This is a test HTML file for PDF conversion.</p>
</body>
</html>

Step 1: Setting Up the Project

1.1 Create a New Project

  1. Open Visual Studio.

  2. Click on File > New > Project.

  3. Choose Console App under .NET. Click Next.

    Choose Console App
  4. Name your project (e.g., HtmlToPdfConverter). Choose a suitable location and click Create.

    Name your project

1.2 Install the CefSharp NuGet Package

  1. Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

  2. Search for CefSharp.OffScreen in the Browse tab.

    Search for CefSharp.OffScreen
  3. Select the package and click Install.

    CefSharp.OffScreen Install
  4. Accept the license agreement to complete the installation.

Step 2: Configuring CefSharp

CefSharp requires some initialization before it can be used. Below is the setup process.

Initialize CefSharp

Add the following code to your Program.cs file:

using System;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.OffScreen;

namespace HtmlToPdfConverter
{
    class Program
    {
        private static ChromiumWebBrowser browser;
        static void Main(string[] args)
        {
            // Initialize CefSharp
            var settings = new CefSettings();
            Cef.Initialize(settings);

            // Convert HTML to PDF
            HtmlToPdfHeadless().GetAwaiter().GetResult();

            // Shutdown CefSharp
            Cef.Shutdown();
        }

        private static async Task HtmlToPdfHeadless()
        {
            // Implementation goes here (explained in Step 3)
        }
    }
}

Step 3: Implementing the HTML-to-PDF Conversion Logic

Load the HTML and Generate the PDF

Update the ConvertHtmlToPdf method as follows:

private static async Task HtmlToPdfHeadless()
{
    string inputPath = @"C:\Test\Sample.html";
    string outputPath = @"C:\Test\Output.pdf";

    browser = new ChromiumWebBrowser(inputPath);

    // Wait for browser to load
    await browser.WaitForInitialLoadAsync();

    // Alternatively wait for the browser to stop rendering 
    await browser.WaitForRenderIdleAsync();

    // Save the PDF
    bool success = await browser.PrintToPdfAsync(outputPath);

    if (success)
    {
        Console.WriteLine("PDF successfully saved to {outputPath}");
    }
    else
    {
        Console.WriteLine("Failed to save PDF.");
    }

    // Prevent the application from exiting immediately
    Console.ReadLine();

    // Exit the application
    Environment.Exit(0);
}

Step 4: Test the Application

  1. Place your HTML file (e.g., Sample.html) in the project directory.

  2. Press F5 to run the application.

  3. Check if the output PDF (e.g., Output.pdf) is generated in the specified location.

    Output PDF

Example Code with Comments

Here is the complete code with additional comments:

using System;
using System.Threading.Tasks;
using CefSharp;
using CefSharp.OffScreen;

namespace HtmlToPdfConverter
{
    class Program
    {
        private static ChromiumWebBrowser browser;
        static void Main(string[] args)
        {
            // Initialize CefSharp
            var settings = new CefSettings();
            Cef.Initialize(settings);

            // Convert HTML to PDF
            HtmlToPdfHeadless().GetAwaiter().GetResult();

            // Shutdown CefSharp
            Cef.Shutdown();
        }

        private static async Task HtmlToPdfHeadless()
        {
            string inputPath = @"C:\Test\Sample.html";
            string outputPath = @"C:\Test\Output.pdf";

            browser = new ChromiumWebBrowser(inputPath);

            // Wait for browser to load
            await browser.WaitForInitialLoadAsync();

            // Alternatively wait for the browser to stop rendering 
            await browser.WaitForRenderIdleAsync();

            // Save the PDF
            bool success = await browser.PrintToPdfAsync(outputPath);

            if (success)
            {
                Console.WriteLine("PDF successfully saved to {outputPath}");
            }
            else
            {
                Console.WriteLine("Failed to save PDF.");
            }

            // Prevent the application from exiting immediately
            Console.ReadLine();

            // Exit the application
            Environment.Exit(0);
        }
    }
}