👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Convert HTML to PDF Report in .NET

Convert HTML to PDF Report in .NET

report

3 Articles

Improve

In this article, let's learn about how to do Convert HTML to PDF in .NET.

Table of Contents

  1. Introduction
  2. Why HTML to PDF ?
  3. Using wkhtmltopdf
  4. Using Chrome Headless
  5. Using Selenium Webdriver
  6. Using window.print()
  7. Summary

Introduction

Converting HTML to PDF is a common requirement in many software applications. The need arises to create PDF versions of HTML documents for archiving or printing purposes, or to generate reports, invoices, and other types of documents. In this article, we will explore different approaches to convert HTML to PDF using .NET.

Why HTML to PDF ?

HTML is a markup language that is designed to be displayed in web browsers. However, it is not designed to be printed or saved as a document. Converting HTML to PDF allows you to preserve the original layout, formatting, and graphics of the HTML document, as well as to add features such as headers, footers, and page numbers. HTML5 combined with CSS3 gives the most powerful and flexible and dynamic layout that can used easily converted to PDF using print media query.

Using wkhtmltopdf

wkhtmltopdf is a command-line tool that converts HTML to PDF using the WebKit rendering engine. To use wkhtmltopdf in .NET,

  1. Download and install wkhtmltopdf latest version from here.
  2. Use the below code.
  3. And call the method as HtmlToPdf("test", new string[] { "https://www.google.com" }, new string[] { "-s A5" });
  4. If you need to convert HTML string to PDF, the tweak the above method and replace the Arguments to Process StartInfo as $@"/C echo | set /p=""{htmlText}"" | ""{pdfHtmlToPdfExePath}"" {((options == null) ? "" : string.Join(" ", options))} - ""C:\Users\xxxx\Desktop\{outputFilename}""";

Code Sample - Convert HTML to PDF using wkhtmltopdf

Drawbacks using wkhtmltopdf

  1. The latest build of wkhtmltopdf as of writing this article does not support latest HTML5 and CSS3. Hence if you try to export any html that as CSS GRID then the output will not be as expected.
  2. You need to handle concurrency issues.

Using Chrome Headless

Chrome headless is a feature of the Google Chrome browser that allows you to run Chrome in a headless environment, without a graphical user interface. This feature can be used to convert HTML to PDF by printing the HTML document to a PDF file. To use chrome headless in .NET,

  1. Download and install Chrome headless latest version from here.
  2. Use the below code.
  3. This will convert html file to pdf file.
  4. If you need to convert some url to pdf then use the following as Argument to Process StartInfo @"/C --headless --disable-gpu --run-all-compositor-stages-before-draw --print-to-pdf-no-header --print-to-pdf=""C:/Users/Abdul Rahman/Desktop/test.pdf"" ""https://www.google.com"""

Code Sample - Convert HTML to PDF using Chrome Headless

Drawbacks using Chrome Headless

  1. This works as expected with latest HTML5 and CSS3 features. Output will be same as you view in browser but when running this via IIS you need to run the AppliactionPool of your application under LocalSystem Identity or you need to provide read/write access to IISUSRS.

Using Selenium Webdriver

Selenium WebDriver is a popular Nuget package used for automating web browsers. It can be used to open a webpage and interact with it programmatically, including printing the page. To use Selenium Webdriver in .NET,

  1. Install Nuget Packages Selenium.WebDriver and Selenium.WebDriver.ChromeDriver.
  2. Use the below code.

Code Sample - Convert HTML to PDF using Selenium WebDriver

Drawbacks using Selenium WebDriver

  1. This approach needs latest chrome browser to be installed in the server where the app runs.
  2. If the chrome browser version in server is updated then Selenium.WebDriver.ChromeDriver Nuget package needs to be updated. Else this will throw run time error due to version mismatch.

Advantages using Selenium WebDriver

  1. This just needs an Nuget installation and works as expected with latest HTML5 and CSS3 features. Output will be same as you view in browser.

Note:The above drawbacks can be overcome if we are running app in docker. All we need to do is to install chrome when building app image using Dockerfile.

Note:With this approach, please make sure to add <PublishChromeDriver>true</PublishChromeDriver> in .csproj file as shown below:

Code Sample - Convert HTML to PDF using Selenium WebDriver Project Settings

This will publish the chrome driver when publishing the project.

Using window.print()

If the users are using your app from browser then you can rely on JavaScript and use window.print() and necessary print media css to generate PDF from the browser. For example generating invoice from browser in an inventory app.

Code Sample - Convert HTML to PDF using window print

Demo - HTML to PDF Demo using window.print()

Scenario - Let's try converting this page HTML to PDF from I ❤️ .NET. The examples uses staright window.print() method but the ideas are open to control and change layout & appearance using print media css.

Drawbacks using window.print()

  1. In SPA like Blazor, we need to do some workaround with iframe to print sections of page.

Advantages using window.print()

  1. No dependency on any tools.
  2. PDF generated directly from HTML, CSS and JS in browser.
  3. Faster
  4. Supports all the latest CSS properties.

Summary

In this article we learn't how to convert HTML to PDF in .NET. Converting HTML to PDF is a common requirement in many software applications. There are several ways to convert HTML to PDF using .NET. The most preferred approach is to use browser window.print() in front end apps and use Selenium Webdriver in backend API's.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Report
  • HTML
  • PDF