C# .NET PDF Generation API Quick Start
Use the PDFBolt REST API to generate PDFs from URLs, HTML, or published templates in C#. Follow the steps below to send your first PDF generation request.
1. Get Your API Key
Copy a conversion API key from API Credentials in your Dashboard. Use it to replace the API key placeholder in the examples below.
If you don’t have an account, sign up. The Free plan includes 50 document conversions per month. No credit card is required.
2. Make Your First Request
The examples use .NET’s built-in HttpClient and System.Text.Json. No additional packages are required.
For your first request, select Direct to generate a PDF and save it locally.
Choose your endpoint:
- Direct
- Sync
- Async
The Direct endpoint returns the generated PDF in the HTTP response. The examples below save it to a local file.
Choose your source:
- URL
- HTML
- Template
Convert a web page to PDF:
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class DirectUrl {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
url = "https://example.com",
format = "A4",
printBackground = true
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/direct"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("webpage.pdf", pdfBytes);
Console.WriteLine("PDF generated successfully");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert HTML to PDF (HTML must be Base64-encoded):
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class DirectHtml {
public static async Task Main(string[] args) {
using var client = new HttpClient();
string htmlContent = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
string base64Html = Convert.ToBase64String(Encoding.UTF8.GetBytes(htmlContent));
var requestData = new {
html = base64Html,
format = "A4",
printBackground = true,
margin = new {
top = "30px",
left = "30px"
}
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/direct"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("document.pdf", pdfBytes);
Console.WriteLine("PDF generated successfully");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert a template with data to PDF:
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class DirectTemplate {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
templateId = "your-template-id",
templateData = new {
client_name = "John Doe",
invoice_number = "INV-001",
total_amount = "$299.99",
line_items = new object[] {
new { description = "Web Development", unit_price = "$200.00" },
new { description = "Design Services", unit_price = "$99.99" }
}
}
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/direct"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("invoice.pdf", pdfBytes);
Console.WriteLine("PDF generated successfully");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Create and publish a template using the Dashboard Template Designer, Template API, or an AI assistant via MCP. Pass its UUID as templateId and your document data as templateData. Match the data fields to the template variables.
The Sync endpoint returns a JSON response containing a PDF download URL. The link is valid for 24 hours.
Choose your source:
- URL
- HTML
- Template
Convert a web page and get a download URL:
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class SyncUrl {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
url = "https://example.com",
format = "A4",
printBackground = true
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/sync"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"PDF URL: {result.GetProperty("documentUrl").GetString()}");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert HTML and get a download URL (HTML must be Base64-encoded):
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class SyncHtml {
public static async Task Main(string[] args) {
using var client = new HttpClient();
string htmlContent = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
string base64Html = Convert.ToBase64String(Encoding.UTF8.GetBytes(htmlContent));
var requestData = new {
html = base64Html,
format = "A4",
printBackground = true,
margin = new {
top = "30px",
left = "30px"
}
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/sync"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"PDF URL: {result.GetProperty("documentUrl").GetString()}");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert a template with data and get a download URL:
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class SyncTemplate {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
templateId = "your-template-id",
templateData = new {
client_name = "John Doe",
invoice_number = "INV-001",
total_amount = "$299.99",
line_items = new object[] {
new { description = "Web Development", unit_price = "$200.00" },
new { description = "Design Services", unit_price = "$99.99" }
}
}
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/sync"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"PDF URL: {result.GetProperty("documentUrl").GetString()}");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Create and publish a template using the Dashboard Template Designer, Template API, or an AI assistant via MCP. Pass its UUID as templateId and your document data as templateData. Match the data fields to the template variables.
The Async endpoint returns a requestId immediately, then sends the conversion result to your HTTPS webhook as a signed JSON payload. Async requires a paid plan.
Replace https://your-app.com/webhook with your HTTPS webhook URL.
Choose your source:
- URL
- HTML
- Template
Submit a web page URL for PDF conversion:
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AsyncUrl {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
url = "https://example.com",
format = "A4",
printBackground = true,
webhook = "https://your-app.com/webhook"
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/async"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"Request ID: {result.GetProperty("requestId").GetString()}");
Console.WriteLine("The conversion result will be sent to your webhook.");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert HTML and receive a webhook callback (HTML must be Base64-encoded):
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AsyncHtml {
public static async Task Main(string[] args) {
using var client = new HttpClient();
string htmlContent = "<html><body><h1>Hello!</h1><p>This is a sample PDF.</p></body></html>";
string base64Html = Convert.ToBase64String(Encoding.UTF8.GetBytes(htmlContent));
var requestData = new {
html = base64Html,
format = "A4",
printBackground = true,
margin = new {
top = "30px",
left = "30px"
},
webhook = "https://your-app.com/webhook"
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/async"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"Request ID: {result.GetProperty("requestId").GetString()}");
Console.WriteLine("The conversion result will be sent to your webhook.");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Convert a template with data and receive a webhook callback:
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class AsyncTemplate {
public static async Task Main(string[] args) {
using var client = new HttpClient();
var requestData = new {
templateId = "your-template-id",
templateData = new {
client_name = "John Doe",
invoice_number = "INV-001",
total_amount = "$299.99",
line_items = new object[] {
new { description = "Web Development", unit_price = "$200.00" },
new { description = "Design Services", unit_price = "$99.99" }
}
},
webhook = "https://your-app.com/webhook"
};
var request = new HttpRequestMessage {
Method = HttpMethod.Post,
RequestUri = new Uri("https://api.pdfbolt.com/v1/async"),
Content = new StringContent(
JsonSerializer.Serialize(requestData),
Encoding.UTF8,
"application/json"
)
};
request.Headers.Add("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");
try {
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode) {
var errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"HTTP {(int)response.StatusCode}");
Console.WriteLine($"Error Message: {errorContent}");
return;
}
var responseContent = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<JsonElement>(responseContent);
Console.WriteLine($"Request ID: {result.GetProperty("requestId").GetString()}");
Console.WriteLine("The conversion result will be sent to your webhook.");
} catch (Exception ex) {
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Create and publish a template using the Dashboard Template Designer, Template API, or an AI assistant via MCP. Pass its UUID as templateId and your document data as templateData. Match the data fields to the template variables.
Next Steps
📄️ API Endpoints
Explore request and response details for each endpoint
📄️ Conversion Parameters
Configure page size, margins, headers, footers, and more
📄️ Error Handling
Understand error codes and troubleshoot API requests
📄️ Templates
Create reusable PDF layouts and fill them with your data