Code sample for md/2/pdf
API
C#
This C# code sample demonstrates how to access the PDFTeam md/2/pdf
API endpoint. This endpoint converts markdown files to PDF. The sample converts a two page report in markdown ('md') format to a PDF and adds overlays with some text and a QR code. Also added will be a header that prints the document title on every page. The resulting PDF will be stored in AWS S3, the permalink is returned to the caller.
Parameters that will be POST'ed to the API are in a separate file. This C# sample expects the params.json
-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:
The params
-file defines two overlays, one with the text "TOP SECRET" in red, the other one with a barcode. Headers and footers are defined near the end:
// Sample C# code for accessing the PDFTeam API, endpoint: md/2/pdf
// Overlay #1: Text "TOP SECRET"
// Overlay #2: QR barcode
// Header: Document title
//
// File: params.json
{
"job": "ACME ANALYSIS",
"user": "YOUR_USERNAME",
"key": "YOUR_APIKEY",
"test": false,
"style": {
"fixed": false,
"size": 10
},
"inputOptions": {
"source": "https://pdfteam.com/samples/coyote_report.md"
},
"outputOptions": {
"storage": true,
"file": "acme_analysis.pdf"
},
"pdfOptions": {
"format": "A4",
"margin": {
"left": "2cm",
"top": "2cm",
"right": "2cm",
"bottom": "3cm"
},
},
"overlays": [
{
"position": {
"x": "170mm",
"y": "270mm"
},
"type": "barcode",
"config": {
"type": 19,
"data": "https://pdfteam.com"
}
},
{
"rotate": 45,
"position": {
"x": "center",
"y": "middle"
},
"type": "text",
"config": {
"data": "TOP SECRET",
"font": "Helvetica Bold",
"size": 72,
"color": "#ff0000",
"opacity": 0.5
}
},
],
"hfOptions": {
"footer": " ",
"header":
"<div style=\"color:#0000ff;font-size: 12px;margin-left:auto;margin-right:auto;\">TITLE: <span class=\"title\"></span></div>"
}
}
NOTE: Setting "test" to TRUE will not consume any credits but the generated PDF carries a watermark.
Main C# program:
// Sample C# code for accessing the PDFTeam API, endpoint: md/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/md/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:
{"endpoint": "md/2/pdf", "job": "ACME ANALYSIS", "credits": 1, "result": "https://pdfteam.s3.eu-north-1.amazonaws.com/.../acme_analysis.pdf"}
Sample result: