Api2Pdf 2.2.0
dotnet add package Api2Pdf --version 2.2.0
NuGet\Install-Package Api2Pdf -Version 2.2.0
<PackageReference Include="Api2Pdf" Version="2.2.0" />
<PackageVersion Include="Api2Pdf" Version="2.2.0" />
<PackageReference Include="Api2Pdf" />
paket add Api2Pdf --version 2.2.0
#r "nuget: Api2Pdf, 2.2.0"
#:package Api2Pdf@2.2.0
#addin nuget:?package=Api2Pdf&version=2.2.0
#tool nuget:?package=Api2Pdf&version=2.2.0
api2pdf.dotnet
.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.
Installation
dotnet add package Api2Pdf
Install-Package Api2Pdf -Version 2.1.1
Quick Start
Create 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"
});
Custom Domains
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.
Understanding Responses
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.
Common Request Features
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"
}
};
Chrome
HTML or URL to PDF
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");
Markdown to PDF
var result = client.Chrome.MarkdownToPdf(new ChromeMarkdownToPdfRequest
{
Markdown = "# Invoice\n\nThis PDF was generated from markdown."
});
HTML, URL, or Markdown to image
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."
});
Wkhtmltopdf
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");
LibreOffice
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"
});
Markitdown
Convert a file URL to markdown:
var result = client.Markitdown.ConvertToMarkdown(new MarkitdownRequest
{
Url = "https://example.com/sample.docx"
});
OpenDataLoader
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"
});
PdfSharp
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
});
Zip
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();
Zebra
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
});
Utilities
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();
Working With Files
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();
Development
The repo uses a conventional layout:
src/Api2Pdffor the shipping librarytests/Api2Pdf.Testsfor contract and regression tests
Resources
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Api2Pdf:
| Package | Downloads |
|---|---|
|
Olivitec.Framework.Converter.Pdf
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.