POSTS
How to convert HTML to PDF in .NET
If you are using our HTML to PDF conversion API and you are on .NET Framework or .NET Core we have good news!
We created simple HttpClient extension method called ConvertToPdfAsync. You can use it as following:
using (var client = new HttpClient())
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
{
//prepare dynamic data (optional step)
var data = new Dictionary<string, string>() {
{ "invoiceNumber", "123" },
{ "customerName", "John Doe" },
{ "summary", "<u>example</u>" },
{ "totalAmount", "456.78" },
};
//call our extension method on HttpClient instance
//NOTE: you can get credentials at https://html2pdf.appbeat.io
var res = await client.ConvertToPdfAsync(
publicApiKey, secretSignKey,
new Uri("https://html2pdf.appbeat.io/examples/SimpleInvoiceTemplate.html"),
data: data, cancellationToken: cts.Token
);
//save converted PDF result to local disk
using (var fs = new FileStream(
$@"C:\Downloads\{Guid.NewGuid().ToString()}.pdf",
FileMode.CreateNew, FileAccess.Write))
{
await res.CopyToAsync(fs);
}
res.Dispose();
}
That’s it!
You can get full C# source code for ConvertToPdfAsync extension method and modify it if needed. Code is compatible with .NET Standard 2.0 and depends only on Newtonsoft.Json package.
You can get your free API credentials for PDF conversion at https://html2pdf.appbeat.io.
If you have question(s), please feel free to contact us. Feedback is appreciated!