Converting an HTML page to a PDF document in C# is a common task, often used to create invoices, reports, contracts, and other business documents from HTML templates. Converting an HTML page to a PDF document in C# is also essential for creating static, portable, and print-ready documents from dynamic web content.
The process of converting an HTML page to a PDF document involves the following steps:
- Open the source HTML file and parse the HTML page code.
- Get the CSS and JS files used on the HTML page.
- Parse the CSS styles in CSS files.
- Parse the JavaScript code in JS files.
- Run the JavaScript code and get the code of resulting HTML page.
- Create a new PDF document.
- Convert raster and vector graphics on the HTML page (including CSS styles and transparency) to graphics on the PDF page and add the graphics to the resulting PDF document.
- Convert text on the HTML page (including CSS styles) to text on the PDF page and add the text to the resulting PDF document.
- Save the created PDF document to a file.
Specification of HTML5 standard (
https://html.spec.whatwg.org/print.pdf) contains 1553 pages.
Specification of CSS2 standard (
https://www.w3.org/TR/CSS22/css2.pdf) contains 426 pages.
Specification of JavaScript standard (
https://ecma-international.org/wp-content/uploads/ECMA-262_16th_edition_june_2025.pdf) contains 835 pages.
The steps for converting HTML to PDF and information about HTML5, CSS2, and JavaScript specifications are provided above to make it clear that, in general, converting HTML to PDF is a very complex task if implemented from scratch.
At the same time, this complex task is already implemented in modern web browsers, which render HTML pages perfectly and can save rendered HTML page to a PDF file. Therefore, a reasonable question arises: is it possible to use a web browser in a C# application to convert an HTML page to a PDF document?
In this article, we will look at how to convert an HTML page of any complexity to a PDF document in a C# application absolutely free and with maximum quality using Chromium.
Chromium is an open-source web browser project that is primarily used as the basis for most modern web browsers, including Google Chrome, Microsoft Edge, Opera, and others. It's designed with speed, security, and stability in mind, and features significant contributions from developers at Google, Microsoft, and other companies.
Converting an HTML page to a PDF document in C# using the Chrome command line
The easiest way to convert an HTML page to a PDF document is to run the following Chrome command from the command line:
"C:\Program Files\Google\Chrome\Application\chrome.exe" --headless --disable-gpu —run-all-compositor-stages-before-draw --no-margins --printBackground=true --no-pdf-header-footer --print-to-pdf="D:\result.pdf" "https://www.vintasoft.com/index.html"
Here's a brief description of the parameters used in the Chrome command line:
- --headless – specifies that Chrome should run without a graphical interface.
- --disable-gpu – specifies that the GPU should be disabled when rendering an HTML page. This parameter is required for compatibility with older versions of Chrome.
- --run-all-compositor-stages-before-draw – specifies that all rendering and composition stages should be completed before the HTML page is rendered.
- --no-margins – specifies that the HTML page should be rendered without margins.
- --printBackground – specifies that the HTML page should contain background graphics.
- --no-pdf-header-footer – specifies that the generated PDF document should not contain a header/footer with document information.
- --print-to-pdf – specifies the path to the saved PDF file. The path must be absolute.
The above command can be run from the Windows/Linux/maOS command line or from C# code.
Here is C# code that demonstrates how to convert an HTML page to a PDF document in a .NET console application using Chrome:
using System.Diagnostics;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
string chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
string sourceUrl = "https://www.vintasoft.com/index.html";
string resultPdfFilePath = @"D:\result.pdf";
string chromeArgsTemplate =
"--headless --no-pdf-header-footer --no-margins --disable-gpu --run-all-compositor-stages-before-draw --printBackground=true --print-to-pdf=\"{0}\" {1}";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = chromePath,
Arguments = string.Format(chromeArgsTemplate, resultPdfFilePath, sourceUrl),
UseShellExecute = false,
CreateNoWindow = false
};
using (Process process = Process.Start(startInfo))
{
process.WaitForExit();
}
Console.WriteLine("HTML page is converted to the PDF document.");
}
}
}
This conversion method can be used if the Chrome web browser is installed on computer and you know the path to the "chrome.exe" file.
The resulting .NET application is small in size because it does not contain any additional libraries and relies entirely on the Chrome web browser.
Converting an HTML page to a PDF document in C# using the CefSharp.OffScreen nuget-package
The CefSharp.OffScreen nuget-package contains a compiled version of the CefSharp library and allows developers to run a full instance of the Chromium web browser in the background without a visible user interface.
If you want to use CefSharp.OffScreen library in a .NET Framework application, simply add a reference to the CefSharp.OffScreen nuget-package to the .NET Framework application.
If you want to use CefSharp.OffScreen library in a .NET Core application, simply add a reference to the CefSharp.OffScreen.NETCore nuget-package to the .NET Core application.
Here is the C# code for a .NET console application that converts an HTML page to a PDF document using CefSharp.OffScreen library:
using CefSharp;
using CefSharp.OffScreen;
using System;
using System.Threading.Tasks;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
string sourceUrl = "https://www.vintasoft.com/index.html";
string resultPdfFilePath = @"D:\result.pdf";
Task task = ConvertHtmlToPdfUsingCefSharpOffScreen(sourceUrl, resultPdfFilePath);
task.Wait();
Console.WriteLine("HTML page is converted to the PDF document.");
Console.ReadKey();
}
static async Task ConvertHtmlToPdfUsingCefSharpOffScreen(string url, string pdfPath)
{
// if CEF is not initialized
if (Cef.IsInitialized == null || Cef.IsInitialized == false)
{
// create CEF settings
CefSettings settings = new CefSettings();
// initialize CEF
Cef.Initialize(settings);
}
// create Chromium web browser
using (ChromiumWebBrowser browser = new ChromiumWebBrowser(url))
{
// wait while page will be loaded
await browser.WaitForInitialLoadAsync();
// create PDF print settings
PdfPrintSettings printSettings = new PdfPrintSettings
{
MarginType = CefPdfPrintMarginType.None,
PrintBackground = true,
Landscape = false,
Scale = 1.0,
PreferCssPageSize = true,
DisplayHeaderFooter = false
};
// print web page to a PDF file
await browser.PrintToPdfAsync(pdfPath, printSettings);
}
}
}
}
This conversion method is independent of the Chrome web browser, and in this case, there's no need to compile CefSharp yourself, as the creators of the CefSharp.OffScreen nuget-package have already done this.
The resulting .NET application is completely standalone and large in size (just over 300 MB) because it contains CefSharp library files.
Converting an HTML page to a PDF document in C# using the CefSharp library
CefSharp is an open-source project that provides a .NET wrapper for the Chromium Embedded Framework (CEF). CefSharp allows developers to embed a fully functional Chromium-based web browser directly into C# or VB.NET desktop applications.
The CefSharp repository is located on GitHub:
https://github.com/cefsharp/cefsharp
The repository contains 15 projects, including a sample console application, a sample WinForms application, and a sample WPF application.
Try running the sample WPF application (project "CefSharp.Wpf.Example.netcore") and you'll see a WPF application with page navigation similar to Chrome. The WPF application includes link navigation, CSS styles, and JavaScript code—it feels just like Chrome. In the application's main menu, you can select menu "Tests => Print Current Tab to PDF" and save the rendered HTML page as a PDF file.
This conversion method is independent of the Chrome web browser, but you will need to manually compile CefSharp from the source code, which is done automatically when compiling the CefSharp project in Visual Studio.
The resulting .NET application (the "CefSharp.Wpf.Example.netcore" project) is completely standalone and has a large size (just over 400 MB) because it contains CefSharp library files.
Processing PDF documents with the VintaSoft Imaging .NET SDK
VintaSoft chose not to include functionality for converting HTML pages to PDF documents in its SDKs because the task is very complex and is already implemented in modern web browsers.
At the same time, the
VintaSoft Imaging .NET SDK and
VintaSoft PDF .NET Plug-in allow you to create, view, verify, print, annotate, edit, protect, sign, optimize, compress, convert, and save PDF and PDF/A documents in Windows, Linux, and macOS.
Accordingly, the VintaSoft SDK can be used to process the created PDF document, namely:
Detailed information about the capabilities of processing PDF documents using
VintaSoft Imaging .NET SDK +
VintaSoft PDF .NET Plug-in can be found here:
https://www.vintasoft.com/vspdf-dotnet-index.html