|
| 1 | +# api2pdf.java |
| 2 | +Java client 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](https://www.api2pdf.com/faq) |
| 11 | + |
| 12 | + |
| 13 | +## <a name="installation"></a>Installation |
| 14 | + |
| 15 | + Coming soon |
| 16 | + |
| 17 | +## <a name="resources"></a>Resources |
| 18 | + |
| 19 | +Resources this API supports: |
| 20 | + |
| 21 | +- [wkhtmltopdf](#wkhtmltopdf) |
| 22 | +- [Headless Chrome](#chrome) |
| 23 | +- [LibreOffice](#libreoffice) |
| 24 | +- [Merge / Concatenate PDFs](#merge) |
| 25 | + |
| 26 | +## <a name="authorization"></a>Authorization |
| 27 | + |
| 28 | +### Acquire API Key |
| 29 | + |
| 30 | +Create an account at [portal.api2pdf.com](https://portal.api2pdf.com/register) to get your API key. |
| 31 | + |
| 32 | +## <a name="#usage"></a>Usage |
| 33 | + |
| 34 | +### Initialize the Client |
| 35 | + |
| 36 | +All usage starts by requiring api2pdf and creating a new object. |
| 37 | + |
| 38 | + package com.api2pdf.client; |
| 39 | + import com.api2pdf.models.Api2PdfResponse; |
| 40 | + |
| 41 | + Api2PdfClient a2pClient = new Api2PdfClient('YOUR-API-KEY'); |
| 42 | + |
| 43 | + |
| 44 | +Once you initialize the client, you can make calls like so: |
| 45 | + |
| 46 | +``` |
| 47 | +Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromHtml("<p>test</p>", true, "test.pdf"); |
| 48 | +System.out.println(pdfResponse.getPdf()); |
| 49 | +``` |
| 50 | + |
| 51 | +### Successful Result Format |
| 52 | + |
| 53 | + { |
| 54 | + 'pdf': 'https://link-to-pdf-only-available-for-24-hours', |
| 55 | + 'mbIn': 0.08421039581298828, |
| 56 | + 'mbOut': 0.08830547332763672, |
| 57 | + 'cost': 0.00017251586914062501, |
| 58 | + 'success': true, |
| 59 | + 'error': null, |
| 60 | + 'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb' |
| 61 | + } |
| 62 | + |
| 63 | +### Failed Result Format |
| 64 | + |
| 65 | + { |
| 66 | + 'success': false, |
| 67 | + 'error': 'some reason for the error', |
| 68 | + 'responseId': '6e46637a-650d-46d5-af0b-3d7831baccbb' |
| 69 | + } |
| 70 | + |
| 71 | +## <a name="wkhtmltopdf"></a> wkhtmltopdf |
| 72 | + |
| 73 | +**Convert HTML to PDF (load PDF in browser window (true or false) and specify a file name)**. |
| 74 | + |
| 75 | +``` |
| 76 | +Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromHtml("<p>Hello, World</p>", true, "test.pdf"); |
| 77 | +System.out.println(pdfResponse.getPdf()); |
| 78 | +``` |
| 79 | + |
| 80 | +**Convert HTML to PDF (use HashMap for advanced wkhtmltopdf settings)** |
| 81 | +[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) |
| 82 | + |
| 83 | +``` |
| 84 | +Map<String, String> options = new HashMap<String, String>(); |
| 85 | +options.put("orientation", "landscape"); |
| 86 | +options.put("pageSize", "A4"); |
| 87 | +Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromHtml("<p>Hello, World</p>", true, "test.pdf", options); |
| 88 | +System.out.println(pdfResponse.getPdf()); |
| 89 | +``` |
| 90 | + |
| 91 | +**Convert URL to PDF (load PDF in browser window (true or false) and specify a file name)**. |
| 92 | + |
| 93 | +``` |
| 94 | +Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromUrl("https://www.github.com", true, "test.pdf"); |
| 95 | +System.out.println(pdfResponse.getPdf()); |
| 96 | +``` |
| 97 | + |
| 98 | +**Convert URL to PDF (use object for advanced wkhtmltopdf settings)** |
| 99 | +[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) |
| 100 | + |
| 101 | +``` |
| 102 | +Map<String, String> options = new HashMap<String, String>(); |
| 103 | +options.put("orientation", "landscape"); |
| 104 | +options.put("pageSize", "A4"); |
| 105 | +Api2PdfResponse pdfResponse = a2pClient.wkhtmlToPdfFromUrl("https://www.github.com", true, "test.pdf", options); |
| 106 | +System.out.println(pdfResponse.getPdf()); |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## <a name="chrome"></a>Headless Chrome |
| 112 | + |
| 113 | +**Convert HTML to PDF (load PDF in browser window (true or false) and specify a file name)** |
| 114 | + |
| 115 | +``` |
| 116 | +Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromHtml("<p>Hello World</p>", true, "test.pdf"); |
| 117 | +System.out.println(pdfResponse.getPdf()); |
| 118 | +``` |
| 119 | + |
| 120 | +**Convert HTML to PDF (use options for advanced Headless Chrome settings)** |
| 121 | +[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) |
| 122 | + |
| 123 | +``` |
| 124 | +Map<String, String> options = new HashMap<String, String>(); |
| 125 | +options.put("landscape", "true"); |
| 126 | +Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromHtml("<p>Hello World</p>", true, "test.pdf", options); |
| 127 | +System.out.println(pdfResponse.getPdf()); |
| 128 | +``` |
| 129 | + |
| 130 | +**Convert URL to PDF (load PDF in browser window (true or false) and specify a file name)** |
| 131 | + |
| 132 | +``` |
| 133 | +Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromUrl("https://www.github.com", true, "test.pdf"); |
| 134 | +System.out.println(pdfResponse.getPdf()); |
| 135 | +``` |
| 136 | + |
| 137 | +**Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings)** |
| 138 | +[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) |
| 139 | + |
| 140 | +``` |
| 141 | +Map<String, String> options = new HashMap<String, String>(); |
| 142 | +options.put("landscape", "true"); |
| 143 | +Api2PdfResponse pdfResponse = a2pClient.headlessChromeFromUrl("https://www.github.com", true, "test.pdf", options); |
| 144 | +System.out.println(pdfResponse.getPdf()); |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## <a name="libreoffice"></a>LibreOffice |
| 150 | + |
| 151 | +LibreOffice supports the conversion to PDF from the following file formats: |
| 152 | + |
| 153 | +- doc, docx, xls, xlsx, ppt, pptx, gif, jpg, png, bmp, rtf, txt, html |
| 154 | + |
| 155 | +You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF. |
| 156 | + |
| 157 | +**Convert Microsoft Office Document or Image to PDF (load PDF in browser window (true or false) and specify a file name)** |
| 158 | + |
| 159 | +``` |
| 160 | +Api2PdfResponse pdfResponse = a2pClient.libreofficeConvert("https://your-url-to-file", true, "test.pdf"); |
| 161 | +System.out.println(pdfResponse.getPdf()); |
| 162 | +``` |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +## <a name="merge"></a>Merge / Concatenate Two or More PDFs |
| 167 | + |
| 168 | +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. |
| 169 | + |
| 170 | +**Merge PDFs from array of URLs to existing PDFs (load PDF in browser window (true or false) and specify a file name)**** |
| 171 | + |
| 172 | +``` |
| 173 | +String[] urls = { "your-url-to-pdf1.pdf", "your-url-to-pdf2.pdf" }; |
| 174 | +Api2PdfResponse pdfResponse = a2pClient.merge(urls, true, "test.pdf"); |
| 175 | +System.out.println(pdfResponse.getPdf()); |
| 176 | +``` |
0 commit comments