|
| 1 | +# api2pdf.dotnet |
| 2 | +.NET bindings for [Api2pdf REST API](https://www.api2pdf.com/documentation) |
| 3 | + |
| 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**. |
| 5 | + |
| 6 | +- [Installation](#installation) |
| 7 | +- [Resources](#resources) |
| 8 | +- [Authorization](#authorization) |
| 9 | +- [Usage](#usage) |
| 10 | +- [FAQ](#faq) |
| 11 | + |
| 12 | + |
| 13 | +## <a name="installation"></a>Add a dependency |
| 14 | + |
| 15 | +### nuget |
| 16 | + |
| 17 | +Run the nuget command for installing the client `Install-Package Api2Pdf` |
| 18 | + |
| 19 | +## <a name="resources"></a>Resources |
| 20 | + |
| 21 | +Resources this API supports: |
| 22 | + |
| 23 | +- [wkhtmltopdf](#wkhtmltopdf) |
| 24 | +- [Headless Chrome](#chrome) |
| 25 | +- [LibreOffice](#libreoffice) |
| 26 | +- [Merge / Concatenate PDFs](#merge) |
| 27 | +- [Helper Methods](#helpers) |
| 28 | + |
| 29 | +## <a name="authorization"></a>Authorization |
| 30 | + |
| 31 | +### Acquire API Key |
| 32 | +1. Create an account and login at [portal.api2pdf.com](https://portal.api2pdf.com) |
| 33 | +2. Add a balance to your account (no monthly commitment, sign up with as little as $1) |
| 34 | +3. Create an application and grab your API Key |
| 35 | + |
| 36 | +## <a name="#usage"></a>Usage |
| 37 | + |
| 38 | +### Initialize the Client |
| 39 | + |
| 40 | +All usage starts by initializing the client by passing your API key as a parameter to the constructor. |
| 41 | + |
| 42 | + var a2pClient = Api2Pdf("YOUR-API-KEY"); |
| 43 | + |
| 44 | +Once you initialize the client, you can make calls like so: |
| 45 | + |
| 46 | + var apiResponse = a2pClient.HeadlessChrome.FromHtml('<p>Hello, World</p>'); |
| 47 | + Console.WriteLine(apiResponse.Pdf); |
| 48 | + Console.ReadLine(); |
| 49 | + |
| 50 | +### Result Object |
| 51 | + |
| 52 | +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. |
| 53 | + |
| 54 | + { |
| 55 | + 'pdf': 'https://link-to-pdf-only-available-for-24-hours', |
| 56 | + 'mbIn': 0.08421039581298828, |
| 57 | + 'mbOut': 0.08830547332763672, |
| 58 | + 'cost': 0.00017251586914062501, |
| 59 | + 'success': True, |
| 60 | + 'error': None, |
| 61 | + 'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb' |
| 62 | + } |
| 63 | + |
| 64 | +### <a name="wkhtmltopdf"></a> wkhtmltopdf |
| 65 | + |
| 66 | +**Convert HTML to PDF** |
| 67 | + |
| 68 | + var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>"); |
| 69 | + |
| 70 | +**Convert HTML to PDF (load PDF in browser window and specify a file name)** |
| 71 | + |
| 72 | + var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", inline=true, outputFileName="test.pdf"); |
| 73 | + |
| 74 | +**Convert HTML to PDF (use keyword arguments for advanced wkhtmltopdf settings)** |
| 75 | +[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) |
| 76 | + |
| 77 | + options = { |
| 78 | + 'orientation': 'landscape', |
| 79 | + 'pageSize': 'A4' |
| 80 | + } |
| 81 | + var apiResponse = a2pClient.WkHtmlToPdf.FromHtml("<p>Hello, World</p>", **options); |
| 82 | + |
| 83 | +**Convert URL to PDF** |
| 84 | + |
| 85 | + var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com"); |
| 86 | + |
| 87 | +**Convert URL to PDF (load PDF in browser window and specify a file name)** |
| 88 | + |
| 89 | + var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", inline=true, outputFileName="test.pdf"); |
| 90 | + |
| 91 | +**Convert URL to PDF (use keyword arguments for advanced wkhtmltopdf settings)** |
| 92 | +[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) |
| 93 | + |
| 94 | + options = { |
| 95 | + 'orientation': 'landscape', |
| 96 | + 'pageSize': 'A4' |
| 97 | + } |
| 98 | + var apiResponse = a2pClient.WkHtmlToPdf.FromUrl("http://www.api2pdf.com", **options); |
| 99 | + |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## <a name="chrome"></a>Headless Chrome |
| 104 | + |
| 105 | +**Convert HTML to PDF** |
| 106 | + |
| 107 | + var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>"); |
| 108 | + |
| 109 | +**Convert HTML to PDF (load PDF in browser window and specify a file name)** |
| 110 | + |
| 111 | + var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", inline=true, outputFileName="test.pdf"); |
| 112 | + |
| 113 | +**Convert HTML to PDF (use keyword arguments for advanced Headless Chrome settings)** |
| 114 | +[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) |
| 115 | + |
| 116 | + options = { |
| 117 | + 'orientation': 'landscape', |
| 118 | + 'pageSize': 'A4' |
| 119 | + } |
| 120 | + var apiResponse = a2pClient.HeadlessChrome.FromHtml("<p>Hello, World</p>", **options); |
| 121 | + |
| 122 | +**Convert URL to PDF** |
| 123 | + |
| 124 | + var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com"); |
| 125 | + |
| 126 | +**Convert URL to PDF (load PDF in browser window and specify a file name)** |
| 127 | + |
| 128 | + var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", inline=true, outputFileName="test.pdf"); |
| 129 | + |
| 130 | +**Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings)** |
| 131 | +[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) |
| 132 | + |
| 133 | + options = { |
| 134 | + 'orientation': 'landscape', |
| 135 | + 'pageSize': 'A4' |
| 136 | + } |
| 137 | + var apiResponse = a2pClient.HeadlessChrome.FromUrl("http://www.api2pdf.com", **options); |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## <a name="libreoffice"></a>LibreOffice |
| 142 | + |
| 143 | +LibreOffice supports the conversion to PDF from the following file formats: |
| 144 | + |
| 145 | +- doc, docx, xls, xlsx, ppt, pptx, gif, jpg, png, bmp, rtf, txt, html |
| 146 | + |
| 147 | +You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF. |
| 148 | + |
| 149 | +**Convert Microsoft Office Document or Image to PDF** |
| 150 | + |
| 151 | + var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx"); |
| 152 | + |
| 153 | +**Convert Microsoft Office Document or Image to PDF (load PDF in browser window and specify a file name)** |
| 154 | + |
| 155 | + var apiResponse = a2pClient.LibreOffice.Convert("https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx", inline=true, outputFileName="test.pdf"); |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +## <a name="merge"></a>Merge / Concatenate Two or More PDFs |
| 160 | + |
| 161 | +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. |
| 162 | + |
| 163 | +**Merge PDFs from list of URLs to existing PDFs** |
| 164 | + |
| 165 | + var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"}; |
| 166 | + var apiResponse = a2pClient.Merge(links_to_pdfs); |
| 167 | + |
| 168 | +**Merge PDFs from list of URLs to existing PDFs (load PDF in browser window and specify a file name)** |
| 169 | + |
| 170 | + var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"}; |
| 171 | + var apiResponse = a2pClient.Merge(links_to_pdfs, inline=true, outputFileName="test.pdf"); |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## <a name="helpers"></a>Helper Methods |
| 176 | + |
| 177 | +**void Api2PdfResponse.SavePdf(string localPath)** |
| 178 | + |
| 179 | +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. |
| 180 | + |
| 181 | + var a2pClient = Api2Pdf("YOUR-API-KEY"); |
| 182 | + var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"}; |
| 183 | + var apiResponse = a2pClient.Merge(links_to_pdfs, inline=true, outputFileName="test.pdf"); |
| 184 | + apiResponse.SavePdf("path-to-local-folder"); |
| 185 | + |
| 186 | +**byte[] Api2PdfResponse.GetPdfBytes()** |
| 187 | + |
| 188 | +You can use `GetPdfBytes()` method to download the pdf to a byte array. |
| 189 | + |
| 190 | + var a2pClient = Api2Pdf("YOUR-API-KEY"); |
| 191 | + var links_to_pdfs = new List<string>() {"https://LINK-TO-PDF", "https://LINK-TO-PDF"}; |
| 192 | + var apiResponse = a2pClient.Merge(links_to_pdfs, inline=true, outputFileName="test.pdf"); |
| 193 | + apiResponse.GetPdfBytes(); |
| 194 | + |
| 195 | +--- |
| 196 | +## <a name="faq"></a>FAQ |
| 197 | + |
| 198 | +#### How do you bill? |
| 199 | +$1 will be deducted from your balance every month as long as you maintain an active account. This charge begins 30 days after your first sign up for the service. In addition, we charge $0.001 per megabyte (data in + data out). We require customers to maintain a positive balance on their account to use the service. You can turn off auto-recharge at any time and let your funds run out if you no longer wish to use the service. See our [pricing calculator](https://www.api2pdf.com/pricing/). |
| 200 | + |
| 201 | +#### Do you offer free accounts? |
| 202 | +The average customer spents about $2/month on our product. We do not have free accounts as this time. Feel free to check out alternatives and competitors. |
| 203 | + |
| 204 | +#### Cancellation and refunds |
| 205 | +We do not have any long term contracts. You can leave us at anytime with no further commitments. As our minimum cost is $1.00, we do not provide refunds. |
| 206 | + |
| 207 | +#### Are there any limits? |
| 208 | +Api2Pdf does not set any specific limits on PDF file size, however our system does have processing power limitations. Each PDF request is provided 3 GB of RAM to work with and 90 seconds to generate the PDF. We offer WKHTMLTOPDF, Headless Chrome, and LibreOffice to do conversions. Our platform will have the same limits as those underlying components. If the underlying component fails to convert to PDF, it will also fail via our service. Some examples are: |
| 209 | + |
| 210 | +- Password protected PDFs |
| 211 | +- Encrypted PDFs |
| 212 | +- HTML that references erroneous content |
| 213 | +- Protected Office Documents |
| 214 | + |
| 215 | +#### How long are PDFs stored on Api2Pdf.com? |
| 216 | +After generating a PDF via the API, you are provided with a link to the file. This link will hold the PDF for 24 hours. If you wish to keep your PDF long term, download the file to your local cache. |
0 commit comments