Code sample for html/2/pdf API

C#


This C# code demonstrates how to access the PDFTeam html/2/pdf API endpoint. This endpoint loads HTML input (either as a URL or directly as a string) and converts it to PDF. In this case, we convert an HTML invoice to a PDF and add watermarks with some text, a company logo and a QR code. The resulting PDF will be stored in AWS S3, the permalink is then returned to the caller.

Parameters that will be POST'ed to the PDFTEam API are in params.json. Code in program.cs below expects the file to be in the same folder as the executable. Create a build action so that the file is copied to the output directory.

Here's a sample Visual Studio solution, note highlight in properties of the file:

Visual Studio setup for PDFTam API Convert HTML to PDF

The params-file defines three overlays, the first with the text "PAST DUE" in red, the second with a barcode. FInally, a third overlay watermarks the PDF with a logo:

// Sample C# code for accessing the PDFTeam API, endpoint: html/2/pdf
// Overlay #1: Text "PAST DUE"
// Overlay #2: QR barcode
// Overlay #3: Company logo
//
// File: params.json

{
  "job": "ACME INVOICE",
  "user": "YOUR_USERNAME",
  "key": "YOUR_APIKEY",
  "test": true,
  "inputOptions": {
    "source": "https://pdfteam.com/samples/invoice.html"
  },
  "outputOptions": {
    "storage": true,
    "file": "invoice.pdf"
  },
  "pdfOptions": {
    "format": "Letter"
  },
  "overlays": [
    {
      "rotate": 45,
      "position": {
        "x": "center",
        "y": "middle"
      },
      "type": "text",
      "config": {
        "data": "PAST DUE",
        "font": "Helvetica Bold",
        "size": 72,
        "color": "#ff0000",
        "opacity": 0.5
      }
    },
    {
      "position": {
        "x": "left",
        "y": "30mm"
      },
      "type": "barcode",
      "config": {
        "type": 19,
        "data": "https://pdfteam.com"
      }
    },
    {
      "position": {
        "x": "132mm",
        "y": "228mm"
      },
      "type": "image",
      "config": {
        "source": "https://pdfteam.com/samples/acme_logo.png"
      }
    }
  ]
}


NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark. This is useful for dialing in overlay settings like position or style.


Main C# program:

// Sample C# code for accessing the PDFTeam API, endpoint: html/2/pdf
// Code assumes .NET 8 (LTS). For older versions, we recommend Newtonsoft.JSON package
// to parse the params file
// File: program.cs

using System.Text;

namespace PDFTeam
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var uri = new Uri("https://api.pdfteam.com/api/html/2/pdf");

            using (var client = new HttpClient())
            {
                StreamReader sr = new StreamReader("params.json");
                string json = sr.ReadToEnd();
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = uri,
                    Content = content
                };

                using (var response = await client.SendAsync(request))
                {
                    response.EnsureSuccessStatusCode();
                    var body = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(body);
                }
            }
        }
    }
}


Sample response with the converted PDF:

{"endpoint": "html/2/pdf", "job": "ACME INVOICE", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../invoice.pdf"}


Sample result:

PDF Invoice with QR, watermark, logo converted from HTML with C#