Blog

Back to blog posts

Convert HTML to PDF with C# and .NET Core

Published Oct 18, 2018

Intro

.NET Core (dotnetcore) is all the rage now and it truly is the future of Microsoft’s .NET Framework. It is cross-platform, lightweight, and wicked fast. Api2Pdf’s web portal is built on .NET Core. So we love it. However, libraries that work with PDF manipulation have been slow to roll out .NET Core compatible versions. No worries, Api2Pdf’s client works just fine, allowing you to convert HTML to PDF in .NET Core without any issue at all.

Api2Pdf is a REST API that lets you generate PDFs at massive scale. It supports wkhtmltopdf, Headless Chrome, and LibreOffice.

Convert HTML to PDF with .NET Core and C#

You will want to install the Api2Pdf .NET Core client library. Click here to view it on Github. If you already your dotnet CLI open, you can install the package with the command:

dotnet add package api2pdf

Acquire an API key on portal.api2pdf.com. It only takes a minute.

Now in your code, initialize the client library with your API key.

var a2pClient = new Api2Pdf("YOUR-API-KEY");

Once you initialize the client, you can make calls like so:

var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
Console.WriteLine(apiResponse.Pdf);
Console.ReadLine();

That’s all there is to it. View the documentation to see what other methods are available. In addition to converting HTML to PDF, Api2Pdf supports URL to PDF (webpage to PDF), Word to PDF, Excel to PDF, Powerpoint to PDF (all using LibreOffice), and merging PDFs together. Api2Pdf also provides support for ZXING (Zebra Crossing) for the purposes of generating barcodes and QR codes.