
Convert HTML to PDF Report in .NET
report
3 Articles
In this article, let's learn about how to do Convert HTML to PDF
in .NET.
Table of Contents
- Introduction
- Why HTML to PDF ?
-
Using
wkhtmltopdf
-
Using
Chrome Headless
-
Using
Selenium Webdriver
-
Using
window.print()
- 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,
- Download and install
wkhtmltopdf
latest version from here. - Use the below code.
- And call the method as
HtmlToPdf("test", new string[] { "https://www.google.com" }, new string[] { "-s A5" });
-
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
-
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. - 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,
- Download and install
Chrome headless
latest version from here. - Use the below code.
- This will convert html file to pdf file.
-
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
-
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 theAppliactionPool
of your application underLocalSystem Identity
or you need to provideread/write
access toIISUSRS
.
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,
- Install Nuget Packages
Selenium.WebDriver
andSelenium.WebDriver.ChromeDriver
. - Use the below code.
Code Sample - Convert HTML to PDF using Selenium WebDriver
Drawbacks using Selenium WebDriver
- This approach needs latest chrome browser to be installed in the server where the app runs.
-
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
- 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()
- In SPA like Blazor, we need to do some workaround with
iframe
to print sections of page.
Advantages using window.print()
- No dependency on any tools.
- PDF generated directly from HTML, CSS and JS in browser.
- Faster
- 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.