How to generate PDF invoice from HTML template for HTML to PDF API in C# with Web API

How to generate PDF invoice from HTML template for HTML to PDF API in C#: How To Tutorial

Writing of the code to generate PDF invoice from HTML template in C# can be done by developers of any level using Web API. Web API was made to help with HTML to PDF API in C#. Web API is the flexible Web API that includes full set of functions from e-signature requests to data extraction, OCR, images recognition, pdf splitting and pdf splitting. Can also generate barcodes and read barcodes from images, scans and pdf.

C# code snippet like this for Web API works best when you need to quickly implement HTML to PDF API in your C# application. Follow the instruction and copy – paste code for C# into your project’s code editor. Test C# sample code examples whether they respond your needs and requirements for the project. Web API – free trial version is on available our website. Also, there are other code samples to help you with your C# application included into trial version.

Try Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

using System; using System.IO; using System.Net; using Newtonsoft.Json.Linq; namespace ByteScoutWebApiExample { class Program { // The authentication key (API Key). // Get your own by registering at const String API_KEY = "***********************************"; static void Main(string[] args) { // HTML template string template = File.ReadAllText(@".\invoice_template.html"); // Data to fill the template string templateData = File.ReadAllText(@".\invoice_data.json"); // Destination PDF file name string destinationFile = @".\result.pdf"; // Create standard .NET web client instance WebClient webClient = new WebClient(); // Set API Key webClient.Headers.Add("x-api-key", API_KEY); try { // Prepare URL for `HTML to PDF` API call string request = Uri.EscapeUriString(string.Format( "{0}", Path.GetFileName(destinationFile))); // Prepare request body in JSON format JObject jsonObject = new JObject( new JProperty("html", template), new JProperty("templateData", templateData)); webClient.Headers.Add("Content-Type", "application/json"); // Execute request string response = webClient.UploadString(request, jsonObject.ToString()); // Parse JSON response JObject json = JObject.Parse(response); if (json["error"].ToObject<bool>() == false) { // Get URL of generated PDF file string resultFileUrl = json["url"].ToString(); webClient.Headers.Remove("Content-Type"); // remove the header required for only the previous request // Download the PDF file webClient.DownloadFile(resultFileUrl, destinationFile); Console.WriteLine("Generated PDF document saved as \"{0}\" file.", destinationFile); } else { Console.WriteLine(json["message"].ToString()); } } catch (WebException e) { Console.WriteLine(e.ToString()); } webClient.Dispose(); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } } }

Try Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)

<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40" /> </packages>

Try Web API today:  60 Day Free Trial (on-premise) or  Web API (on-demand version)



Get 60 Day Free Trial or Visit Web API Home Page

Explore Web API Documentation

Get Web API Free Training


Get Your Free API Key

Explore Web API Documentation

Related Samples: