How to generate PDF invoice from HTML template for HTML to PDF API in C# with PDF.co 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 PDF.co Web API. PDF.co Web API was made to help with HTML to PDF API in C#. PDF.co 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 PDF.co 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.

PDF.co 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.

On-demand (REST Web API) version:
 Web API (on-demand version)

On-premise offline SDK for Windows:
 60 Day Free Trial (on-premise)

Program.cs
      
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 https://app.pdf.co/documentation/api 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( "https://api.pdf.co/v1/pdf/convert/from/html?name={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(); } } }

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

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

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

VIDEO

ON-PREMISE OFFLINE SDK

60 Day Free Trial or Visit PDF.co Web API Home Page

Explore PDF.co Web API Documentation

Explore Samples

Sign Up for PDF.co Web API Online Training

ON-DEMAND REST WEB API

Get Your API Key

Explore Web API Docs

Explore Web API Samples

Related Samples: