Api2Pdf 1.1.0
See the version list below for details.
dotnet add package Api2Pdf --version 1.1.0
NuGet\Install-Package Api2Pdf -Version 1.1.0
<PackageReference Include="Api2Pdf" Version="1.1.0" />
paket add Api2Pdf --version 1.1.0
#r "nuget: Api2Pdf, 1.1.0"
// Install Api2Pdf as a Cake Addin
#addin nuget:?package=Api2Pdf&version=1.1.0
// Install Api2Pdf as a Cake Tool
#tool nuget:?package=Api2Pdf&version=1.1.0
api2pdf.dotnet
.NET bindings for Api2Pdf REST API
Api2Pdf.com is a REST API for instantly generating PDF documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), and images. The API also supports merge / concatenation of two or more PDFs. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, and LibreOffice.
- Installation
- Resources
- Authorization
- Usage
Add a dependency
nuget
Run the nuget command for installing the client Install-Package Api2Pdf
Resources
Resources this API supports:
- wkhtmltopdf
- Headless Chrome
- LibreOffice
- Merge / Concatenate PDFs
- Helper Methods
Authorization
Acquire API Key
Create an account at portal.api2pdf.com to get an API key.
Usage
Initialize the Client
All usage starts by initializing the client by passing your API key as a parameter to the constructor.
var a2pClient = Api2Pdf("YOUR-API-KEY");
Once you initialize the client, you can make calls like so:
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
Console.WriteLine(apiResponse.Pdf);
Console.ReadLine();
Result Object
An Api2PdfResponse
object is returned from every API call. If a call is unsuccessful then success
will show False and the error
will provide the reason for failure. Additional attributes include the total data usage in, out, and the cost for the API call, typically very small fractions of a penny.
{
'pdf': 'https://link-to-pdf-only-available-for-24-hours',
'mbIn': 0.08421039581298828,
'mbOut': 0.08830547332763672,
'cost': 0.00017251586914062501,
'success': True,
'error': None,
'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}
wkhtmltopdf
Convert HTML to PDF
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>");
Convert HTML to PDF (load PDF in browser window and specify a file name)
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");
Convert HTML to PDF (use dictionary for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.
var options = new Dictionary<string, string>();
options.Add("orientation", "landscape");
options.Add("pageSize", "Letter");
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", options: options);
Convert URL to PDF
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com");
Convert URL to PDF (load PDF in browser window and specify a file name)
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");
Convert URL to PDF (use dictionary for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.
var options = new Dictionary<string, string>();
options.Add("orientation", "landscape");
options.Add("pageSize", "Letter");
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", options: options);
Headless Chrome
Convert HTML to PDF
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
Convert HTML to PDF (load PDF in browser window and specify a file name)
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");
Convert HTML to PDF (use dictionary for advanced Headless Chrome settings) View full list of Headless Chrome options available.
var options = new Dictionary<string, string>();
options.Add("landscape", "true");
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", options: options);
Convert URL to PDF
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com");
Convert URL to PDF (load PDF in browser window and specify a file name)
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");
Convert URL to PDF (use dictionary for advanced Headless Chrome settings) View full list of Headless Chrome options available.
var options = new Dictionary<string, string>();
options.Add("landscape", "true");
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", options: options);
LibreOffice
LibreOffice supports the conversion to PDF from the following file formats:
- doc, docx, xls, xlsx, ppt, pptx, gif, jpg, png, bmp, rtf, txt, html
You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF.
Convert Microsoft Office Document or Image to PDF
var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx");
Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)
var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", inline: true, outputFileName: "test.pdf");
Merge / Concatenate Two or More PDFs
To use the merge endpoint, supply a list of urls to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list.
Merge PDFs from list of URLs to existing PDFs
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs);
Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
Helper Methods
Api2PdfResponse Delete(string responseId)
By default, Api2Pdf will delete your PDF 24 hours after it has been generated. For those with high security needs, you may want to delete your PDF on command. You can do so by making an DELETE api call with the responseId
attribute that was returned on the original JSON payload.
var a2pClient = Api2Pdf("YOUR-API-KEY");
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello World</p>");
var responseId = apiResponse.ResponseId;
//delete PDF
a2pClient.Delete(responseId);
void Api2PdfResponse.SavePdf(string localPath)
On any Api2PdfResponse
that succesfully generated a pdf, you can use the handy SavePdf(string localPdf)
method to download the pdf to a local cache.
var a2pClient = Api2Pdf("YOUR-API-KEY");
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
apiResponse.SavePdf("path-to-local-folder");
byte[] Api2PdfResponse.GetPdfBytes()
You can use GetPdfBytes()
method to download the pdf to a byte array.
var a2pClient = Api2Pdf("YOUR-API-KEY");
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
apiResponse.GetPdfBytes();
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.0 netstandard2.1 |
.NET Framework | net461 net462 net463 net47 net471 net472 net48 net481 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen40 tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 10.0.3)
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.