-
Notifications
You must be signed in to change notification settings - Fork 0
/
Api2PdfClient.cls
149 lines (126 loc) · 6.3 KB
/
Api2PdfClient.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public class Api2PdfClient{
private static final String API2PDF_BASE_URL = 'https://v2018.api2pdf.com';
private static final String API2PDF_MERGE = API2PDF_BASE_URL + '/merge';
private static final String API2PDF_WKHTMLTOPDF_HTML = API2PDF_BASE_URL + '/wkhtmltopdf/html';
private static final String API2PDF_WKHTMLTOPDF_URL = API2PDF_BASE_URL + '/wkhtmltopdf/url';
private static final String API2PDF_CHROME_HTML = API2PDF_BASE_URL + '/chrome/html';
private static final String API2PDF_CHROME_URL = API2PDF_BASE_URL + '/chrome/url';
private static final String API2PDF_LIBREOFFICE_CONVERT = API2PDF_BASE_URL + '/libreoffice/convert';
private String apiKey;
public Api2PdfClient(String apiKey) {
this.apiKey = apiKey;
}
private HttpRequest getRequest(String endpoint) {
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Authorization', this.apiKey);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setHeader('User-Agent', 'Api2Pdf Client');
return req;
}
private Api2PdfResponse makeRequest(String payload, Boolean inlinePdf, String fileName, HttpRequest req){
Http http = new Http();
req.setBody(payload);
HTTPResponse res = http.send(req);
Blob body = res.getBodyAsBlob();
String requestString = body.toString();
return (Api2PdfResponse)JSON.deserialize(requestString, Api2PdfResponse.class);
}
public Api2PdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePdf, String fileName){
HttpRequest req = getRequest(API2PDF_LIBREOFFICE_CONVERT);
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
model.Url = officeFileUrl;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse mergePdf(String[] pdfUrls, boolean inlinePdf, String fileName) {
HttpRequest req = getRequest(API2PDF_MERGE);
Api2PdfMergeRequestModel model = new Api2PdfMergeRequestModel();
model.Urls = pdfUrls;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse wkhtmlToPdfFromHtml(String html, boolean inlinePdf, String fileName) {
HttpRequest req = getRequest(API2PDF_WKHTMLTOPDF_HTML);
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
model.Html = html;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse wkhtmlToPdfFromHtml(String html, boolean inlinePdf, String fileName, Map<String, String> options) {
HttpRequest req = getRequest(API2PDF_WKHTMLTOPDF_HTML);
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
model.Html = html;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
model.Options = options;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String fileName) {
HttpRequest req = getRequest(API2PDF_WKHTMLTOPDF_URL);
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
model.Url = url;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String fileName, Map<String, String> options) {
HttpRequest req = getRequest(API2PDF_WKHTMLTOPDF_URL);
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
model.Url = url;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
model.options = options;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName) {
HttpRequest req = getRequest(API2PDF_CHROME_HTML);
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
model.Html = html;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName, Map<String, String> options) {
HttpRequest req = getRequest(API2PDF_CHROME_HTML);
String optionsPayload = '';
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
model.Html = html;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
model.options = options;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName) {
HttpRequest req = getRequest(API2PDF_CHROME_URL);
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
model.Url = url;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName, Map<String, String> options) {
HttpRequest req = getRequest(API2PDF_CHROME_URL);
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
model.Url = url;
model.InlinePdf = inlinePdf;
model.FileName = fileName;
model.options = options;
String payload = JSON.serialize(model);
return makeRequest(payload, inlinePdf, fileName, req);
}
}