1
1
package com .api2pdf .client ;
2
2
3
3
import java .io .BufferedReader ;
4
+ import java .io .BufferedWriter ;
4
5
import java .io .DataOutputStream ;
5
6
import java .io .IOException ;
6
7
import java .io .InputStreamReader ;
8
+ import java .io .OutputStreamWriter ;
7
9
import java .net .HttpURLConnection ;
8
- import java .net .ProtocolException ;
9
10
import java .net .URL ;
11
+ import java .nio .charset .StandardCharsets ;
10
12
import java .util .Map ;
11
13
12
14
import com .api2pdf .models .Api2PdfFromHtmlRequestModel ;
@@ -51,10 +53,9 @@ private Api2PdfResponse makeRequest(String payload, boolean inlinePdf, String fi
51
53
// For POST only - START
52
54
con .setDoOutput (true );
53
55
DataOutputStream wr = new DataOutputStream (con .getOutputStream ());
54
- wr .writeBytes (payload );
55
- wr .flush ();
56
- wr .close ();
57
- // For POST only - END
56
+ BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (wr , StandardCharsets .UTF_8 ));
57
+ writer .write (payload );
58
+ writer .close ();
58
59
59
60
int responseCode = con .getResponseCode ();
60
61
System .out .println ("POST Response Code :: " + responseCode );
@@ -77,7 +78,7 @@ private Api2PdfResponse makeRequest(String payload, boolean inlinePdf, String fi
77
78
public Api2PdfResponse libreofficeConvert (String officeFileUrl , boolean inlinePdf , String fileName )
78
79
throws IOException {
79
80
HttpURLConnection con = getConnection (API2PDF_LIBREOFFICE_CONVERT );
80
- Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
81
+ Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
81
82
model .setUrl (officeFileUrl );
82
83
model .setInlinePdf (inlinePdf );
83
84
model .setFileName (fileName );
@@ -88,7 +89,7 @@ public Api2PdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePd
88
89
89
90
public Api2PdfResponse merge (String [] pdfUrls , boolean inlinePdf , String fileName ) throws IOException {
90
91
HttpURLConnection con = getConnection (API2PDF_MERGE );
91
- Api2PdfMergeRequestModel model = new Api2PdfMergeRequestModel ();
92
+ Api2PdfMergeRequestModel model = new Api2PdfMergeRequestModel ();
92
93
model .setFileName (fileName );
93
94
model .setInlinePdf (inlinePdf );
94
95
model .setUrls (pdfUrls );
@@ -99,41 +100,43 @@ public Api2PdfResponse merge(String[] pdfUrls, boolean inlinePdf, String fileNam
99
100
100
101
public Api2PdfResponse wkhtmlToPdfFromHtml (String html , boolean inlinePdf , String fileName ) throws IOException {
101
102
HttpURLConnection con = getConnection (API2PDF_WKHTMLTOPDF_HTML );
102
- Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
103
+ Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
103
104
model .setFileName (fileName );
104
105
model .setInlinePdf (inlinePdf );
105
106
model .setHtml (html );
106
107
ObjectMapper objectMapper = new ObjectMapper ();
107
108
String payload = objectMapper .writeValueAsString (model );
108
109
return makeRequest (payload , inlinePdf , fileName , con );
109
110
}
110
-
111
- public Api2PdfResponse wkhtmlToPdfFromHtml (String html , boolean inlinePdf , String fileName , Map <String , String > options ) throws IOException {
111
+
112
+ public Api2PdfResponse wkhtmlToPdfFromHtml (String html , boolean inlinePdf , String fileName ,
113
+ Map <String , String > options ) throws IOException {
112
114
HttpURLConnection con = getConnection (API2PDF_WKHTMLTOPDF_HTML );
113
- Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
115
+ Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
114
116
model .setFileName (fileName );
115
117
model .setInlinePdf (inlinePdf );
116
118
model .setHtml (html );
117
119
model .setOptions (options );
118
120
ObjectMapper objectMapper = new ObjectMapper ();
119
121
String payload = objectMapper .writeValueAsString (model );
120
122
return makeRequest (payload , inlinePdf , fileName , con );
121
- }
123
+ }
122
124
123
125
public Api2PdfResponse wkhtmlToPdfFromUrl (String url , boolean inlinePdf , String fileName ) throws IOException {
124
126
HttpURLConnection con = getConnection (API2PDF_WKHTMLTOPDF_URL );
125
- Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
127
+ Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
126
128
model .setFileName (fileName );
127
129
model .setInlinePdf (inlinePdf );
128
130
model .setUrl (url );
129
131
ObjectMapper objectMapper = new ObjectMapper ();
130
132
String payload = objectMapper .writeValueAsString (model );
131
133
return makeRequest (payload , inlinePdf , fileName , con );
132
134
}
133
-
134
- public Api2PdfResponse wkhtmlToPdfFromUrl (String url , boolean inlinePdf , String fileName , Map <String , String > options ) throws IOException {
135
+
136
+ public Api2PdfResponse wkhtmlToPdfFromUrl (String url , boolean inlinePdf , String fileName , Map <String , String > options )
137
+ throws IOException {
135
138
HttpURLConnection con = getConnection (API2PDF_WKHTMLTOPDF_URL );
136
- Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
139
+ Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
137
140
model .setFileName (fileName );
138
141
model .setInlinePdf (inlinePdf );
139
142
model .setUrl (url );
@@ -145,7 +148,7 @@ public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String
145
148
146
149
public Api2PdfResponse headlessChromeFromHtml (String html , boolean inlinePdf , String fileName ) throws IOException {
147
150
HttpURLConnection con = getConnection (API2PDF_CHROME_HTML );
148
- Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
151
+ Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
149
152
model .setFileName (fileName );
150
153
model .setInlinePdf (inlinePdf );
151
154
model .setHtml (html );
@@ -157,7 +160,7 @@ public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, St
157
160
public Api2PdfResponse headlessChromeFromHtml (String html , boolean inlinePdf , String fileName ,
158
161
Map <String , String > options ) throws IOException {
159
162
HttpURLConnection con = getConnection (API2PDF_CHROME_HTML );
160
- Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
163
+ Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel ();
161
164
model .setFileName (fileName );
162
165
model .setInlinePdf (inlinePdf );
163
166
model .setHtml (html );
@@ -169,7 +172,7 @@ public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, St
169
172
170
173
public Api2PdfResponse headlessChromeFromUrl (String url , boolean inlinePdf , String fileName ) throws IOException {
171
174
HttpURLConnection con = getConnection (API2PDF_CHROME_URL );
172
- Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
175
+ Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
173
176
model .setFileName (fileName );
174
177
model .setInlinePdf (inlinePdf );
175
178
model .setUrl (url );
@@ -181,7 +184,7 @@ public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, Stri
181
184
public Api2PdfResponse headlessChromeFromUrl (String url , boolean inlinePdf , String fileName ,
182
185
Map <String , String > options ) throws IOException {
183
186
HttpURLConnection con = getConnection (API2PDF_CHROME_URL );
184
- Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
187
+ Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel ();
185
188
model .setFileName (fileName );
186
189
model .setInlinePdf (inlinePdf );
187
190
model .setUrl (url );
0 commit comments