Skip to content

Commit 6d11ae9

Browse files
committed
Alternative converter
1 parent b41544d commit 6d11ae9

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/com/convertapi/ConvertApi.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,26 @@ public static CompletableFuture<ConversionResult> convert(String fromFormat, Str
3232
public static CompletableFuture<ConversionResult> convert(String fromFormat, String toFormat, Param[] params, Config config) {
3333
CompletableFuture<ConversionResponse> completableResponse = CompletableFuture.supplyAsync(() -> {
3434
@SuppressWarnings("SpellCheckingInspection")
35-
HttpUrl url = Http.getUrlBuilder(config)
35+
HttpUrl.Builder urlBuilder = Http.getUrlBuilder(config)
3636
.addPathSegment("convert")
3737
.addPathSegment(fromFormat)
3838
.addPathSegment("to")
39-
.addPathSegment(toFormat)
40-
.addQueryParameter("storefile", "true")
39+
.addPathSegment(toFormat);
40+
41+
for (Param param : params) {
42+
if (param.getName() == "converter") {
43+
try {
44+
urlBuilder = urlBuilder
45+
.addPathSegment("converter")
46+
.addPathSegment(param.getValue().get(0));
47+
} catch (InterruptedException | ExecutionException e) {
48+
throw new RuntimeException(e);
49+
}
50+
51+
}
52+
}
53+
54+
HttpUrl url = urlBuilder.addQueryParameter("storefile", "true")
4155
.build();
4256

4357
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.Config;
4+
import com.convertapi.ConversionResult;
5+
import com.convertapi.ConvertApi;
6+
import com.convertapi.Param;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.List;
12+
import java.util.concurrent.CompletableFuture;
13+
import java.util.concurrent.ExecutionException;
14+
15+
import static java.lang.System.getenv;
16+
17+
/**
18+
* Example of saving Word docx to PDF using alternative OpenOffice converter
19+
* Conversion is made by using same file parameter and processing two conversions simultaneously
20+
* https://www.convertapi.com/docx-to-pdf
21+
* https://www.convertapi.com/docx-to-png
22+
*/
23+
24+
public class AlternativeConverter {
25+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
26+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
27+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
28+
29+
System.out.println("Converting DOCX to PDF with OpenOffice converter");
30+
31+
Param docxFileParam = new Param("file", Paths.get("test-files/test.docx"));
32+
Param converterParam = new Param("converter", "openofficetopdf");
33+
34+
CompletableFuture<ConversionResult> pdfResult = ConvertApi.convert("docx", "pdf", docxFileParam, converterParam);
35+
36+
System.out.println("PDF file saved to: " + pdfResult.get().saveFile(tempDir).get());
37+
}
38+
}

0 commit comments

Comments
 (0)