CobaltPDF.Requests
A lightweight, dependency-free companion package to CobaltPDF.
Install this package on the client (your web app, API gateway, mobile backend, etc.) to build and send PDF generation requests — without pulling in Chromium, Playwright, or any other heavy dependency. The CobaltPDF server handles the actual rendering.
Installation
dotnet add package CobaltPDF.Requests
How it works
| Package |
Install on |
Responsibility |
CobaltPDF.Requests |
Client |
Build and send PdfRequest |
CobaltPDF |
Rendering server / Azure Function |
Receive, execute, return PDF |
Both packages share the same PdfRequest model. The server-side CobaltPDF package provides a request.ExecuteAsync(engine) extension method that maps every property back to the fluent API automatically.
Quick start
// Client — only CobaltPDF.Requests installed
var request = new PdfRequest
{
Url = "https://myapp.com/invoice/42",
Landscape = true,
PaperFormat = "A4",
Metadata = new() { Title = "Invoice #42", Author = "Billing System" }
};
var response = await httpClient.PostAsJsonAsync("/api/pdf", request);
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
PdfRequest properties
Source (one required)
| Property |
Type |
Description |
Url |
string? |
URL to render. Mutually exclusive with Html. |
Html |
string? |
Raw HTML to render. Mutually exclusive with Url. |
Layout
| Property |
Type |
Default |
Description |
Landscape |
bool |
false |
Landscape orientation. |
PaperFormat |
string |
"A4" |
Paper size: "A4", "A3", "Letter", "Legal", "Tabloid", etc. |
Margins |
PdfRequestMargins? |
null |
Page margins (null = CobaltPDF default of 1 cm). |
Grayscale |
bool |
false |
Render in grayscale. |
PrintBackground |
bool |
true |
Include CSS background colours and images. |
MediaType |
string |
"screen" |
CSS media type to emulate: "screen" or "print". |
| Property |
Type |
Description |
Header |
string? |
HTML template printed at the top of every page. Supports <span class='pageNumber'> and <span class='totalPages'>. |
Footer |
string? |
HTML template printed at the bottom of every page. Same token support as Header. |
Watermark
| Property |
Type |
Description |
Watermark |
PdfRequestWatermark? |
Overlay applied to every page. See Watermark below. |
Wait strategy
| Property |
Type |
Default |
Description |
WaitStrategy |
string |
"networkIdle" |
When to capture. See Wait strategies below. |
WaitTimeoutMs |
int |
30000 |
Timeout in ms for the wait strategy. |
JavaScript
| Property |
Type |
Default |
Description |
CustomJs |
string? |
null |
JavaScript executed after page load, before capture. |
BypassCsp |
bool |
false |
Bypass Content Security Policy (required when injecting scripts into strict pages). |
| Property |
Type |
Default |
Description |
UserAgent |
string? |
null |
Overrides the browser User-Agent for every request. null uses Chromium's default. |
ExtraHeaders |
Dictionary<string, string> |
{} |
Additional HTTP headers sent with every request (navigation + sub-resources). |
var request = new PdfRequest
{
Url = "https://example.com",
UserAgent = "Mozilla/5.0 (compatible; MyBot/1.0)",
ExtraHeaders = new()
{
["Authorization"] = "Bearer eyJ...",
["Accept-Language"] = "en-GB",
["X-Api-Key"] = "secret"
}
};
Cookies & storage
| Property |
Type |
Default |
Description |
Cookies |
List<PdfRequestCookie> |
[] |
Cookies injected before navigation. |
LocalStorage |
Dictionary<string, string> |
{} |
Key/value pairs written to localStorage before the page loads. |
SessionStorage |
Dictionary<string, string> |
{} |
Key/value pairs written to sessionStorage before the page loads. |
var request = new PdfRequest
{
Url = "https://example.com",
Cookies = [new() { Name = "session", Value = "abc123" }],
LocalStorage = new() { ["theme"] = "dark" }
};
Lazy loading
| Property |
Type |
Default |
Description |
LazyLoadPages |
int? |
null |
Number of viewport-heights to scroll before capture. null = disabled. |
LazyLoadDelayMs |
int |
200 |
Pause between each scroll step (ms). |
Encryption
| Property |
Type |
Description |
Encryption |
PdfRequestEncryption? |
Password-protect the output PDF. null = no encryption. |
Encryption = new()
{
UserPassword = "open123",
OwnerPassword = "admin456", // optional — random generated if omitted
AllowPrinting = true,
AllowCopying = false,
AllowModification = false
}
| Property |
Type |
Description |
Metadata |
PdfRequestMetadata? |
Descriptive metadata embedded in the PDF document properties. |
Metadata = new()
{
Title = "Invoice #42",
Author = "Billing System",
Subject = "Monthly invoice",
Keywords = "invoice, billing",
Creator = "MyApp"
}
Supporting types
PdfRequestMargins
All values are CSS length strings: "10mm", "1cm", "0.5in", "20px".
Margins = new()
{
Top = "10mm",
Bottom = "10mm",
Left = "15mm",
Right = "15mm"
}
PdfRequestCookie
| Property |
Type |
Default |
Description |
Name |
string |
(required) |
Cookie name. |
Value |
string |
(required) |
Cookie value. |
Domain |
string? |
null |
Domain scope. null = inferred from the rendered URL. |
Path |
string |
"/" |
Cookie path. |
Expires |
DateTime? |
null |
Expiry. null = session cookie. |
Secure |
bool |
true |
HTTPS-only. |
HttpOnly |
bool |
false |
Inaccessible to JavaScript. |
Watermark
| Property |
Type |
Default |
Description |
Html |
string |
(required) |
HTML markup for the watermark (inline CSS supported). |
Rotation |
int |
0 |
Clockwise rotation in degrees. |
Opacity |
double |
0.5 |
0.0 (transparent) to 1.0 (opaque). |
Vertical |
string |
"middle" |
"top", "middle", or "bottom". |
Horizontal |
string |
"center" |
"left", "center", or "right". |
Wait strategies
| Value |
Description |
"networkIdle" |
Wait until no network requests for 500 ms (default). |
"signal" |
Wait for window.cobaltNotifyRender() to be called from CustomJs. |
"selector:#my-id" |
Wait until the CSS selector is present in the DOM. |
"js:window.myApp.ready===true" |
Poll a JavaScript expression until it returns truthy. |
"delay:2000" |
Fixed delay in milliseconds. |
PdfResponse
The server returns a PdfResponse after a successful render.
| Property |
Type |
Description |
Data |
string |
Base64-encoded PDF bytes. |
SizeBytes |
long |
Size of the PDF in bytes. |
RenderedAt |
DateTime |
UTC timestamp of when the render completed. |
Helpers:
byte[] bytes = response.ToBytes();
MemoryStream stream = response.ToStream();
Full example
using CobaltPdf.Requests;
var request = new PdfRequest
{
Url = "https://myapp.com/invoice/42",
Landscape = false,
PaperFormat = "A4",
MediaType = "print",
// HTTP headers
UserAgent = "Mozilla/5.0 (compatible; InvoiceBot/1.0)",
ExtraHeaders = new()
{
["Authorization"] = "Bearer eyJ...",
["Accept-Language"] = "en-GB"
},
// Auth cookie
Cookies = [new() { Name = "session", Value = "abc123" }],
// Wait for React app to signal readiness
WaitStrategy = "signal",
CustomJs = "window.cobaltNotifyRender();",
// PDF metadata
Metadata = new()
{
Title = "Invoice #42",
Author = "Billing System"
},
// Password protect
Encryption = new() { UserPassword = "open123" }
};
var httpResponse = await httpClient.PostAsJsonAsync("/api/pdf", request);
httpResponse.EnsureSuccessStatusCode();
var pdfResponse = await httpResponse.Content.ReadFromJsonAsync<PdfResponse>();
await File.WriteAllBytesAsync("invoice.pdf", pdfResponse!.ToBytes());
License
See the CobaltPDF licensing page for details.