Skip to content

Commit 5a7d690

Browse files
committed
support utf-8 charset by default #1
1 parent 577433b commit 5a7d690

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.api2pdf.client;
22

33
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
45
import java.io.DataOutputStream;
56
import java.io.IOException;
67
import java.io.InputStreamReader;
8+
import java.io.OutputStreamWriter;
79
import java.net.HttpURLConnection;
8-
import java.net.ProtocolException;
910
import java.net.URL;
11+
import java.nio.charset.StandardCharsets;
1012
import java.util.Map;
1113

1214
import com.api2pdf.models.Api2PdfFromHtmlRequestModel;
@@ -51,10 +53,9 @@ private Api2PdfResponse makeRequest(String payload, boolean inlinePdf, String fi
5153
// For POST only - START
5254
con.setDoOutput(true);
5355
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();
5859

5960
int responseCode = con.getResponseCode();
6061
System.out.println("POST Response Code :: " + responseCode);
@@ -77,7 +78,7 @@ private Api2PdfResponse makeRequest(String payload, boolean inlinePdf, String fi
7778
public Api2PdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePdf, String fileName)
7879
throws IOException {
7980
HttpURLConnection con = getConnection(API2PDF_LIBREOFFICE_CONVERT);
80-
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
81+
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
8182
model.setUrl(officeFileUrl);
8283
model.setInlinePdf(inlinePdf);
8384
model.setFileName(fileName);
@@ -88,7 +89,7 @@ public Api2PdfResponse libreofficeConvert(String officeFileUrl, boolean inlinePd
8889

8990
public Api2PdfResponse merge(String[] pdfUrls, boolean inlinePdf, String fileName) throws IOException {
9091
HttpURLConnection con = getConnection(API2PDF_MERGE);
91-
Api2PdfMergeRequestModel model = new Api2PdfMergeRequestModel();
92+
Api2PdfMergeRequestModel model = new Api2PdfMergeRequestModel();
9293
model.setFileName(fileName);
9394
model.setInlinePdf(inlinePdf);
9495
model.setUrls(pdfUrls);
@@ -99,41 +100,43 @@ public Api2PdfResponse merge(String[] pdfUrls, boolean inlinePdf, String fileNam
99100

100101
public Api2PdfResponse wkhtmlToPdfFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
101102
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_HTML);
102-
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
103+
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
103104
model.setFileName(fileName);
104105
model.setInlinePdf(inlinePdf);
105106
model.setHtml(html);
106107
ObjectMapper objectMapper = new ObjectMapper();
107108
String payload = objectMapper.writeValueAsString(model);
108109
return makeRequest(payload, inlinePdf, fileName, con);
109110
}
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 {
112114
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_HTML);
113-
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
115+
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
114116
model.setFileName(fileName);
115117
model.setInlinePdf(inlinePdf);
116118
model.setHtml(html);
117119
model.setOptions(options);
118120
ObjectMapper objectMapper = new ObjectMapper();
119121
String payload = objectMapper.writeValueAsString(model);
120122
return makeRequest(payload, inlinePdf, fileName, con);
121-
}
123+
}
122124

123125
public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
124126
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_URL);
125-
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
127+
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
126128
model.setFileName(fileName);
127129
model.setInlinePdf(inlinePdf);
128130
model.setUrl(url);
129131
ObjectMapper objectMapper = new ObjectMapper();
130132
String payload = objectMapper.writeValueAsString(model);
131133
return makeRequest(payload, inlinePdf, fileName, con);
132134
}
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 {
135138
HttpURLConnection con = getConnection(API2PDF_WKHTMLTOPDF_URL);
136-
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
139+
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
137140
model.setFileName(fileName);
138141
model.setInlinePdf(inlinePdf);
139142
model.setUrl(url);
@@ -145,7 +148,7 @@ public Api2PdfResponse wkhtmlToPdfFromUrl(String url, boolean inlinePdf, String
145148

146149
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName) throws IOException {
147150
HttpURLConnection con = getConnection(API2PDF_CHROME_HTML);
148-
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
151+
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
149152
model.setFileName(fileName);
150153
model.setInlinePdf(inlinePdf);
151154
model.setHtml(html);
@@ -157,7 +160,7 @@ public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, St
157160
public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, String fileName,
158161
Map<String, String> options) throws IOException {
159162
HttpURLConnection con = getConnection(API2PDF_CHROME_HTML);
160-
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
163+
Api2PdfFromHtmlRequestModel model = new Api2PdfFromHtmlRequestModel();
161164
model.setFileName(fileName);
162165
model.setInlinePdf(inlinePdf);
163166
model.setHtml(html);
@@ -169,7 +172,7 @@ public Api2PdfResponse headlessChromeFromHtml(String html, boolean inlinePdf, St
169172

170173
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName) throws IOException {
171174
HttpURLConnection con = getConnection(API2PDF_CHROME_URL);
172-
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
175+
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
173176
model.setFileName(fileName);
174177
model.setInlinePdf(inlinePdf);
175178
model.setUrl(url);
@@ -181,7 +184,7 @@ public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, Stri
181184
public Api2PdfResponse headlessChromeFromUrl(String url, boolean inlinePdf, String fileName,
182185
Map<String, String> options) throws IOException {
183186
HttpURLConnection con = getConnection(API2PDF_CHROME_URL);
184-
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
187+
Api2PdfFromUrlRequestModel model = new Api2PdfFromUrlRequestModel();
185188
model.setFileName(fileName);
186189
model.setInlinePdf(inlinePdf);
187190
model.setUrl(url);

0 commit comments

Comments
 (0)