Skip to content

Commit ff1a416

Browse files
committed
fixed typo and upload readme
1 parent bf1be6a commit ff1a416

File tree

3 files changed

+188
-12
lines changed

3 files changed

+188
-12
lines changed

README.md

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
```

api2pdf/src/main/java/com/api2pdf/client/Api2PdfClient.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.net.URL;
1010
import java.util.Map;
1111

12-
import com.api2pdf.models.Api2pdfResponse;
12+
import com.api2pdf.models.Api2PdfResponse;
1313
import com.fasterxml.jackson.databind.ObjectMapper;
1414

1515
public class Api2PdfClient {
@@ -43,7 +43,7 @@ private HttpURLConnection getConnection(String endpoint) throws IOException {
4343

4444
}
4545

46-
private Api2pdfResponse makeRequest(String payload, boolean inlinePdf, String fileName, HttpURLConnection con)
46+
private Api2PdfResponse makeRequest(String payload, boolean inlinePdf, String fileName, HttpURLConnection con)
4747
throws IOException {
4848
// For POST only - START
4949
con.setDoOutput(true);
@@ -67,19 +67,19 @@ private Api2pdfResponse makeRequest(String payload, boolean inlinePdf, String fi
6767
in.close();
6868
String jsonResponse = response.toString();
6969
ObjectMapper objectMapper = new ObjectMapper();
70-
Api2pdfResponse api2pdfResponse = objectMapper.readValue(jsonResponse, Api2pdfResponse.class);
70+
Api2PdfResponse api2pdfResponse = objectMapper.readValue(jsonResponse, Api2PdfResponse.class);
7171
return api2pdfResponse;
7272
}
7373

74-
public Api2pdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePdf, String fileName)
74+
public Api2PdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePdf, String fileName)
7575
throws IOException {
7676
HttpURLConnection con = getConnection(API2PDF_LIBREOFFICE_CONVERT);
7777
String payload = "{\"url\":\"" + officeFileUrl + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName
7878
+ "\"}";
7979
return makeRequest(payload, inlinePdf, fileName, con);
8080
}
8181

82-
public Api2pdfResponse merge(String[] firstPdfUrls, boolean inlinePdf, String fileName) throws IOException {
82+
public Api2PdfResponse merge(String[] firstPdfUrls, boolean inlinePdf, String fileName) throws IOException {
8383
HttpURLConnection con = getConnection(API2PDF_MERGE);
8484
StringBuilder urls = new StringBuilder();
8585
for (String item : firstPdfUrls) {
@@ -90,27 +90,27 @@ public Api2pdfResponse merge(String[] firstPdfUrls, boolean inlinePdf, String fi
9090
return makeRequest(payload, inlinePdf, fileName, con);
9191
}
9292

93-
public Api2pdfResponse wkhtmlToPdfFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
93+
public Api2PdfResponse wkhtmlToPdfFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
9494
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_HTML);
9595
String payload = "{\"html\":\"" + html + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName
9696
+ "\"}";
9797
return makeRequest(payload, inlinePdf, fileName, con);
9898
}
9999

100-
public Api2pdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
100+
public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
101101
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_URL);
102102
String payload = "{\"url\":\"" + url + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName + "\"}";
103103
return makeRequest(payload, inlinePdf, fileName, con);
104104
}
105105

106-
public Api2pdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
106+
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
107107
HttpURLConnection con = getConnection(API2PDF_CHROME_HTML);
108108
String payload = "{\"html\":\"" + html + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName
109109
+ "\"}";
110110
return makeRequest(payload, inlinePdf, fileName, con);
111111
}
112112

113-
public Api2pdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName,
113+
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName,
114114
Map<String, String> options) throws IOException {
115115
HttpURLConnection con = getConnection(API2PDF_CHROME_HTML);
116116
StringBuilder optionsPayload = new StringBuilder();
@@ -123,13 +123,13 @@ public Api2pdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, St
123123
return makeRequest(payload, inlinePdf, fileName, con);
124124
}
125125

126-
public Api2pdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
126+
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
127127
HttpURLConnection con = getConnection(API2PDF_CHROME_URL);
128128
String payload = "{\"url\":\"" + url + "\",\"inlinePdf\":" + inlinePdf + ",\"fileName\":\"" + fileName + "\"}";
129129
return makeRequest(payload, inlinePdf, fileName, con);
130130
}
131131

132-
public Api2pdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName,
132+
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName,
133133
Map<String, String> options) throws IOException {
134134
HttpURLConnection con = getConnection(API2PDF_CHROME_URL);
135135
StringBuilder optionsPayload = new StringBuilder();

api2pdf/src/main/java/com/api2pdf/models/Api2pdfResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.api2pdf.models;
22

3-
public class Api2pdfResponse {
3+
public class Api2PdfResponse {
44

55
private String responseId;
66

0 commit comments

Comments
 (0)