.NET bindings for the Api2Pdf REST API.
Api2Pdf.com is a powerful REST API for document generation, file conversion, and automated content extraction in .NET applications. It supports HTML to PDF, URL to PDF, HTML to image, URL to image, Microsoft Office document conversion, email and image file conversion, PDF page extraction, PDF password protection, file zipping, barcode and QR code generation, markdown conversion, structured PDF data extraction, and image previews or thumbnails for PDF, office, and email files. Api2Pdf is built on proven engines and libraries including wkhtmltopdf, Headless Chrome, PdfSharp, LibreOffice, and related tools to provide reliable PDF generation, document processing, and file transformation workflows through a single API.
The package targets netstandard2.0 and wraps the current Api2Pdf service groups for Chrome, wkhtmltopdf, LibreOffice, Markitdown, OpenDataLoader, PdfSharp, Zip, Zebra, and utility endpoints.
dotnet add package Api2PdfInstall-Package Api2Pdf -Version 2.2.0Create an account at portal.api2pdf.com to get your API key.
using Api2Pdf;
var client = new Api2Pdf("YOUR-API-KEY");
var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = "<html><body><h1>Hello, world!</h1></body></html>"
});
if (result.Success)
{
Console.WriteLine(result.FileUrl);
}
else
{
Console.WriteLine(result.Error);
}Async usage works the same way:
using Api2Pdf;
var client = new Api2Pdf("YOUR-API-KEY");
var result = await client.Chrome.UrlToPdfAsync(new ChromeUrlToPdfRequest
{
Url = "https://www.api2pdf.com"
});The default constructor uses https://v2.api2pdf.com.
If you want to route requests to a different Api2Pdf domain, use the apiKey + baseUrl constructor:
using Api2Pdf;
var client = new Api2Pdf("YOUR-API-KEY", "https://your-custom-domain.api2pdf.com");The package also exposes a constant for the XL cluster:
using Api2Pdf;
var client = new Api2Pdf("YOUR-API-KEY", Api2PdfBaseUrls.V2Xl);v2-xl.api2pdf.com provides much larger compute resources and is intended for heavier workloads, with additional cost compared to the default cluster.
Most API methods return an Api2PdfResult.
{
"fileUrl": "https://link-to-file-available-for-24-hours",
"mbOut": 0.08830547332763672,
"cost": 0.00017251586914062501,
"seconds": 2,
"success": true,
"error": null,
"responseId": "6e46637a-650d-46d5-af0b-3d7831baccbb"
}Important members:
Success: whether the request succeeded.Error: error text when the request fails.FileUrl: URL of the generated file when the API returns standard JSON.ResponseId: identifier used byUtilities.Delete(...).GetFileBytes(): returns bytes already in memory for binary responses, or downloads fromFileUrlwhen needed.
Most request models support these properties:
FileName: set the output file name.Inline:trueto open in the browser,falseto trigger download behavior.UseCustomStorageandStorage: send the output directly to your own storage target.OutputBinary: when the endpoint supports it, request binary content instead of the standard JSON payload.ExtraHTTPHeaders: forward custom headers when Api2Pdf fetches a source URL.
Example custom storage configuration:
var request = new ChromeHtmlToPdfRequest
{
Html = "<p>Hello World</p>",
UseCustomStorage = true,
Storage = new CustomStorageOptions
{
Method = "PUT",
Url = "https://your-presigned-upload-url"
}
};var htmlPdf = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = "<p>Hello World</p>",
Options = new ChromeHtmlToPdfOptions
{
Delay = 3000,
DisplayHeaderFooter = true,
HeaderTemplate = "<div style=\"font-size:12px;\">Header</div>",
FooterTemplate = "<div style=\"font-size:12px;\">Footer</div>",
Landscape = true,
PreferCSSPageSize = true
}
});
var urlPdf = client.Chrome.UrlToPdf(new ChromeUrlToPdfRequest
{
Url = "https://www.api2pdf.com",
ExtraHTTPHeaders = new Dictionary<string, string>
{
["Authorization"] = "Bearer token-for-the-source-site"
},
Options = new ChromeUrlToPdfOptions
{
PuppeteerWaitForMethod = "WaitForNavigation",
PuppeteerWaitForValue = "Load"
}
});For simple GET-style URL conversion, you can also call:
var result = client.Chrome.UrlToPdf("https://www.api2pdf.com");var result = client.Chrome.MarkdownToPdf(new ChromeMarkdownToPdfRequest
{
Markdown = "# Invoice\n\nThis PDF was generated from markdown."
});var htmlImage = client.Chrome.HtmlToImage(new ChromeHtmlToImageRequest
{
Html = "<p>Hello image</p>",
Options = new ChromeHtmlToImageOptions
{
FullPage = true,
ViewPortOptions = new ViewPortOptions
{
Width = 1440,
Height = 900
}
}
});
var urlImage = client.Chrome.UrlToImage("https://www.api2pdf.com");
var markdownImage = client.Chrome.MarkdownToImage(new ChromeMarkdownToImageRequest
{
Markdown = "# Screenshot\n\nGenerated from markdown."
});var htmlPdf = client.Wkhtml.HtmlToPdf(new WkhtmlHtmlToPdfRequest
{
Html = "<p>Hello World</p>",
EnableToc = true,
Options = new Dictionary<string, string>
{
["orientation"] = "landscape",
["pageSize"] = "Letter"
},
TocOptions = new Dictionary<string, string>
{
["disableDottedLines"] = "true"
}
});
var urlPdf = client.Wkhtml.UrlToPdf(new WkhtmlUrlToPdfRequest
{
Url = "https://www.api2pdf.com"
});For the simple GET route:
var result = client.Wkhtml.UrlToPdf("https://www.api2pdf.com");Use LibreOffice endpoints for file and Office conversions.
Convert a file URL to PDF:
var result = client.LibreOffice.AnyToPdf(new LibreFileConversionRequest
{
Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
});Generate a thumbnail:
var result = client.LibreOffice.Thumbnail(new LibreFileConversionRequest
{
Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
});Convert HTML or a URL to DOCX or XLSX:
var docx = client.LibreOffice.HtmlToDocx(new LibreHtmlOrUrlConversionRequest
{
Html = "<html><body><h1>Hello Word</h1></body></html>"
});
var xlsx = client.LibreOffice.HtmlToXlsx(new LibreHtmlOrUrlConversionRequest
{
Url = "https://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html"
});Convert a file URL to markdown:
var result = client.Markitdown.ConvertToMarkdown(new MarkitdownRequest
{
Url = "https://example.com/sample.docx"
});Extract structured content from a PDF URL:
var json = client.OpenDataLoader.PdfToJson(new OpenDataLoaderRequest
{
Url = "https://example.com/sample.pdf"
});
var markdown = client.OpenDataLoader.PdfToMarkdown(new OpenDataLoaderRequest
{
Url = "https://example.com/sample.pdf"
});
var html = client.OpenDataLoader.PdfToHtml(new OpenDataLoaderRequest
{
Url = "https://example.com/sample.pdf"
});Merge PDFs:
var result = client.PdfSharp.MergePdfs(new PdfMergeRequest
{
Urls = new List<string>
{
"https://LINK-TO-PDF-1",
"https://LINK-TO-PDF-2"
}
});Set a password:
var result = client.PdfSharp.SetPassword(new PdfPasswordRequest
{
Url = "https://LINK-TO-PDF",
UserPassword = "password",
OwnerPassword = "owner-password"
});Extract a page range:
var result = client.PdfSharp.ExtractPages(new PdfExtractPagesRequest
{
Url = "https://LINK-TO-PDF",
Start = 0,
End = 2
});Create a zip from multiple files:
var result = client.Zip.GenerateZip(new ZipRequest
{
Files = new List<ZipFileInfo>
{
new ZipFileInfo
{
Url = "https://example.com/report.pdf",
FileName = "docs/report.pdf"
},
new ZipFileInfo
{
Url = "https://example.com/image.png",
FileName = "images/image.png"
}
},
OutputBinary = true
});
byte[] zipBytes = result.GetFileBytes();Generate a barcode or QR code:
var result = client.Zebra.GenerateBarcode(new ZebraRequest
{
Format = "QR_CODE",
Value = "https://www.api2pdf.com",
Width = 300,
Height = 300,
ShowLabel = false
});Delete a generated file:
var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = "<p>Hello World</p>"
});
client.Utilities.Delete(result.ResponseId);Check status or remaining balance:
string status = client.Utilities.Status();
string balance = client.Utilities.Balance();Save to disk:
var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = "<p>Hello World</p>"
});
result.SaveFile("path-to-local-file.pdf");Get bytes directly:
var result = client.Chrome.HtmlToPdf(new ChromeHtmlToPdfRequest
{
Html = "<p>Hello World</p>",
OutputBinary = true
});
byte[] bytes = result.GetFileBytes();The repo uses a conventional layout:
src/Api2Pdffor the shipping librarytests/Api2Pdf.Testsfor contract and regression tests