Skip to content

Commit 0a3da1e

Browse files
author
Zack Schwartz
committed
update documentation for 2.0.0
1 parent 7d2668c commit 0a3da1e

File tree

2 files changed

+180
-65
lines changed

2 files changed

+180
-65
lines changed

Api2Pdf.DotNet/Api2Pdf.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,13 @@ public class Api2PdfResult
465465
[JsonIgnore]
466466
public byte[] ResultAsBytes { private get; set; }
467467

468-
public void SavePdf(string localPath)
468+
public void SaveFile(string localPath)
469469
{
470470
var wc = new System.Net.WebClient();
471471
wc.DownloadFile(FileUrl, localPath);
472472
}
473473

474-
public byte[] GetPdfBytes()
474+
public byte[] GetFileBytes()
475475
{
476476
if (ResultAsBytes != null)
477477
return ResultAsBytes;

README.md

Lines changed: 178 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# api2pdf.dotnet
2-
.NET bindings for [Api2Pdf REST API](https://www.api2pdf.com/documentation)
2+
.NET bindings for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2)
33

4-
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**.
4+
Api2Pdf.com is a powerful REST API for instantly generating PDF and Office documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), Email files, and images. The API also supports merge / concatenation of two or more PDFs, setting passwords on PDFs, and adding bookmarks to PDFs. Api2Pdf is a wrapper for popular libraries such as **wkhtmltopdf**, **Headless Chrome**, **PdfSharp**, and **LibreOffice**.
55

66
- [Installation](#installation)
77
- [Resources](#resources)
@@ -42,57 +42,84 @@ All usage starts by initializing the client by passing your API key as a paramet
4242

4343
Once you initialize the client, you can make calls like so:
4444

45-
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
46-
Console.WriteLine(apiResponse.Pdf);
45+
var request = new ChromeHtmlToPdfRequest
46+
{
47+
Html = "<p>Hello World</p>"
48+
};
49+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
50+
Console.WriteLine(apiResponse.FileUrl);
4751
Console.ReadLine();
52+
53+
For each endpoint there are common options you can specify too:
54+
55+
- *Inline* - set to True if you would like the PDF to be opened up in a browser window. Set to false if you want the browser to "download" the file.
56+
- *FileName* - specify your own file name such as "sample.pdf". Inline option above should be set to false if you specify your own file name. If no file name is provided, a random file name will be generated for you.
4857

4958
### Result Object
5059

51-
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.
60+
An `Api2PdfResult` 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 bandwidth out, seconds of compute, and the cost for the API call, typically very small fractions of a penny.
5261

5362
{
54-
'pdf': 'https://link-to-pdf-only-available-for-24-hours',
55-
'mbIn': 0.08421039581298828,
63+
'fileUrl': 'https://link-to-file-available-for-24-hours',
5664
'mbOut': 0.08830547332763672,
5765
'cost': 0.00017251586914062501,
66+
'seconds': 2,
5867
'success': True,
5968
'error': None,
6069
'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
6170
}
71+
72+
---
6273

63-
### <a name="wkhtmltopdf"></a> wkhtmltopdf
74+
## <a name="wkhtmltopdf"></a> wkhtmltopdf
6475

6576
**Convert HTML to PDF**
6677

67-
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>");
68-
69-
**Convert HTML to PDF (load PDF in browser window and specify a file name)**
70-
71-
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");
78+
var request = new WkhtmlHtmlToPdfRequest
79+
{
80+
Html = "<p>Hello World</p>"
81+
};
82+
var apiResponse = a2pClient.Wkhtml.HtmlToPdf(request);
7283

7384
**Convert HTML to PDF (use dictionary for advanced wkhtmltopdf settings)**
7485
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
7586

7687
var options = new Dictionary<string, string>();
7788
options.Add("orientation", "landscape");
7889
options.Add("pageSize", "Letter");
79-
var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", options: options);
8090

81-
**Convert URL to PDF**
91+
var request = new WkhtmlHtmlToPdfRequest
92+
{
93+
Html = "<p>Hello World</p>",
94+
FileName = "sample.pdf",
95+
Inline = true
96+
Options = options
97+
}
98+
var apiResponse = a2pClient.Wkhtml.HtmlToPdf(request);
8299

83-
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com");
84-
85-
**Convert URL to PDF (load PDF in browser window and specify a file name)**
100+
**Convert URL to PDF**
86101

87-
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");
102+
var request = new WkhtmlUrlToPdfRequest
103+
{
104+
Url = "https://www.api2pdf.com"
105+
};
106+
var apiResponse = a2pClient.WkHtml.UrlToPdf(request);
88107

89108
**Convert URL to PDF (use dictionary for advanced wkhtmltopdf settings)**
90109
[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)
91110

92111
var options = new Dictionary<string, string>();
93112
options.Add("orientation", "landscape");
94113
options.Add("pageSize", "Letter");
95-
var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", options: options);
114+
115+
var request = new WkhtmlUrlToPdfRequest
116+
{
117+
Url = "https://wwww.api2pdf.com",
118+
FileName = "sample.pdf",
119+
Inline = true
120+
Options = options
121+
}
122+
var apiResponse = a2pClient.WkHtml.UrlToPdf(request);
96123

97124

98125
---
@@ -101,33 +128,57 @@ An `Api2PdfResponse` object is returned from every API call. If a call is unsucc
101128

102129
**Convert HTML to PDF**
103130

104-
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>");
105-
106-
**Convert HTML to PDF (load PDF in browser window and specify a file name)**
107-
108-
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", inline: true, outputFileName: "test.pdf");
131+
var request = new ChromeHtmlToPdfRequest
132+
{
133+
Html = "<p>Hello World</p>"
134+
};
135+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
109136

110137
**Convert HTML to PDF (use dictionary for advanced Headless Chrome settings)**
111138
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
112139

113-
var options = new Dictionary<string, string>();
114-
options.Add("landscape", "true");
115-
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", options: options);
140+
var options = new ChromeHtmlToPdfOptions
141+
{
142+
Delay = 3000,
143+
HeaderTemplate = "<div style=\"font-size:12px;\">Header Content Here</div>",
144+
DisplayHeaderFooter = true,
145+
Landscape = true
146+
}
147+
var request = new ChromeHtmlToPdfRequest
148+
{
149+
Html = "<p>Hello World</p>",
150+
FileName = "sample.pdf",
151+
Inline = true
152+
Options = options
153+
};
154+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
116155

117156
**Convert URL to PDF**
118157

119-
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com");
120-
121-
**Convert URL to PDF (load PDF in browser window and specify a file name)**
122-
123-
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", inline: true, outputFileName: "test.pdf");
158+
var request = new ChromeUrlToPdfRequest
159+
{
160+
Url = "https://www.api2pdf.com"
161+
};
162+
var apiResponse = a2pClient.Chrome.UrlToPdf(request);
124163

125164
**Convert URL to PDF (use dictionary for advanced Headless Chrome settings)**
126165
[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)
127166

128-
var options = new Dictionary<string, string>();
129-
options.Add("landscape", "true");
130-
var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", options: options);
167+
var options = new ChromeUrlToPdfOptions
168+
{
169+
Delay = 3000,
170+
HeaderTemplate = "<div style=\"font-size:12px;\">Header Content Here</div>",
171+
DisplayHeaderFooter = true,
172+
Landscape = true
173+
}
174+
var request = new ChromeUrlToPdfRequest
175+
{
176+
Url = "https://www.api2pdf.com",
177+
FileName = "sample.pdf",
178+
Inline = true
179+
Options = options
180+
};
181+
var apiResponse = a2pClient.Chrome.UrlToPdf(request);
131182

132183
---
133184

@@ -141,58 +192,122 @@ You must provide a url to the file. Our engine will consume the file at that URL
141192

142193
**Convert Microsoft Office Document or Image to PDF**
143194

144-
var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx");
145-
146-
**Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)**
195+
var request = new LibreFileConversionRequest
196+
{
197+
Url = "https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"
198+
}
199+
var apiResponse = a2pClient.LibreOffice.AnyToPdf(request);
200+
201+
**Convert HTML to Microsoft Word or Docx**
147202

148-
var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", inline: true, outputFileName: "test.pdf");
203+
var request = new LibreFileConversionRequest
204+
{
205+
Url = "http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html"
206+
}
207+
var apiResponse = a2pClient.LibreOffice.HtmlToDocx(request);
208+
209+
**Convert HTML to Microsoft Excel or Xlsx**
210+
211+
var request = new LibreFileConversionRequest
212+
{
213+
Url = "http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html"
214+
}
215+
var apiResponse = a2pClient.LibreOffice.HtmlToXlsx(request);
216+
217+
**Convert PDF to HTML**
218+
219+
var request = new LibreFileConversionRequest
220+
{
221+
Url = "http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf"
222+
}
223+
var apiResponse = a2pClient.LibreOffice.PdfToHtml(request);
149224

150225
---
151226

152-
## <a name="merge"></a>Merge / Concatenate Two or More PDFs
227+
## <a name="merge"></a>PdfSharp
228+
229+
**Merge PDFs from list of URLs to existing PDFs**
153230

154231
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.
155232

156-
**Merge PDFs from list of URLs to existing PDFs**
233+
var pdfsToMerge = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
234+
var request = new PdfMergeRequest
235+
{
236+
Urls = pdfsToMerge
237+
};
238+
var apiResponse = a2pClient.PdfSharp.MergePdfs(request);
239+
240+
**Add PDF bookmarks to an existing PDF**
157241

158-
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
159-
var apiResponse = a2pClient.Merge(links_to_pdfs);
242+
var bookmarks = new List<PdfBookmark>()
243+
{
244+
new PdfBookmark
245+
{
246+
Page = 0,
247+
Title = "Introduction"
248+
},
249+
new PdfBookmark
250+
{
251+
Page = 1,
252+
Title = "Second Page"
253+
}
254+
}
255+
var request = new PdfBookmarkRequest
256+
{
257+
Url = "https://LINK-TO-PDF,
258+
Bookmarks = bookmarks
259+
};
260+
var apiResponse = a2pClient.PdfSharp.SetBookmarks(request);
160261

161-
**Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)**
262+
**Add password to existing PDF**
162263

163-
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
164-
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
264+
var request = new PdfPasswordRequest
265+
{
266+
Url = "https://LINK-TO-PDF",
267+
UserPassword = "password"
268+
};
269+
var apiResponse = a2pClient.PdfSharp.SetPassword(request);
165270

166271
---
167272

168273
## <a name="helpers"></a>Helper Methods
169274

170-
**Api2PdfResponse Delete(string responseId)**
275+
**Delete a file conversion on command from API2PDF servers**
171276

172-
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.
277+
By default, Api2Pdf will delete your generated file 24 hours after it has been generated. For those with high security needs, you may want to delete your file on command. You can do so by making an DELETE api call with the `responseId` attribute that was returned on the original JSON payload.
173278

174279
var a2pClient = Api2Pdf("YOUR-API-KEY");
175-
var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello World</p>");
176-
var responseId = apiResponse.ResponseId;
177-
//delete PDF
178-
a2pClient.Delete(responseId);
280+
var request = new ChromeHtmlToPdfRequest
281+
{
282+
Html = "<p>Hello World</p>"
283+
};
284+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
285+
286+
//delete file
287+
a2pClient.Utilities.Delete(apiResponse.ResponseId);
179288

180-
**void Api2PdfResponse.SavePdf(string localPath)**
289+
**void Api2PdfResult.SavePdf(string localPath)**
181290

182-
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.
291+
On any `Api2PdfResult` that succesfully generated a file, you can use the handy `SaveFile(string localPath)` method to download the file to a local cache.
183292

184293
var a2pClient = Api2Pdf("YOUR-API-KEY");
185-
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
186-
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
187-
apiResponse.SavePdf("path-to-local-folder");
294+
var request = new ChromeHtmlToPdfRequest
295+
{
296+
Html = "<p>Hello World</p>"
297+
};
298+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
299+
apiResponse.SaveFile("path-to-local-folder");
188300

189-
**byte[] Api2PdfResponse.GetPdfBytes()**
301+
**byte[] Api2PdfResult.GetFileBytes()**
190302

191-
You can use `GetPdfBytes()` method to download the pdf to a byte array.
303+
You can use `GetFileBytes()` method to download the file to a byte array.
192304

193305
var a2pClient = Api2Pdf("YOUR-API-KEY");
194-
var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"};
195-
var apiResponse = a2pClient.Merge(links_to_pdfs, inline: true, outputFileName: "test.pdf");
196-
apiResponse.GetPdfBytes();
306+
var request = new ChromeHtmlToPdfRequest
307+
{
308+
Html = "<p>Hello World</p>"
309+
};
310+
var apiResponse = a2pClient.Chrome.HtmlToPdf(request);
311+
var resultAsBytes = apiResponse.GetFileBytes();
197312

198313

0 commit comments

Comments
 (0)