Skip to content

Commit 572dec9

Browse files
committed
Refact examples to a maven project
1 parent 5d8ab5c commit 572dec9

File tree

14 files changed

+114
-33
lines changed

14 files changed

+114
-33
lines changed

examples/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.convertapi.client</groupId>
4+
<artifactId>examples</artifactId>
5+
<packaging>jar</packaging>
6+
7+
<version>1.0-SNAPSHOT</version>
8+
<name>Examples for ConvertAPI Java Client</name>
9+
<properties>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.convertapi.client</groupId>
17+
<artifactId>convertapi</artifactId>
18+
<version>2.0</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<resources>
24+
<resource>
25+
<directory>src/main/resources</directory>
26+
<includes>
27+
<include>test.docx</include>
28+
<include>test.pdf</include>
29+
</includes>
30+
</resource>
31+
</resources>
32+
</build>
33+
34+
</project>

src/main/resources/examples/Advanced.java renamed to examples/src/main/java/com/convertapi/examples/Advanced.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;
@@ -19,17 +19,17 @@
1919
/**
2020
* Example of HTTP client setup to use HTTP proxy server.
2121
*/
22-
2322
public class Advanced {
23+
2424
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
2525
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
2626

2727
// Advanced HTTP client setup
2828
Config.setDefaultHttpBuilder(builder -> {
2929
return builder
30-
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8888))) // Setting Proxy server
31-
.connectTimeout(3, TimeUnit.SECONDS); // Setting connect timeout
32-
// More settings can be tuned here
30+
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8888))) // Setting Proxy server
31+
.connectTimeout(3, TimeUnit.SECONDS); // Setting connect timeout
32+
// More settings can be tuned here
3333
});
3434

3535
// Conversion
@@ -47,4 +47,4 @@ public static void main(String[] args) throws IOException, ExecutionException, I
4747

4848
System.out.println("PDF file saved to: " + pdfFile.toString());
4949
}
50-
}
50+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.client.Config;
4+
import com.convertapi.client.ConversionResult;
5+
import com.convertapi.client.ConvertApi;
6+
import com.convertapi.client.Param;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
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+
public class AlternativeConverter {
24+
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
25+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
26+
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
27+
28+
System.out.println("Converting DOCX to PDF with OpenOffice converter");
29+
Param docxFileParam = new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.docx").getFile()).toPath());
30+
Param converterParam = new Param("converter", "openofficetopdf");
31+
32+
CompletableFuture<ConversionResult> pdfResult = ConvertApi.convert("docx", "pdf", docxFileParam, converterParam);
33+
34+
System.out.println("PDF file saved to: " + pdfResult.get().saveFile(tempDir).get());
35+
}
36+
}

src/main/resources/examples/ConversionChaining.java renamed to examples/src/main/java/com/convertapi/examples/ConversionChaining.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;
55
import com.convertapi.client.ConvertApi;
66
import com.convertapi.client.Param;
77

8+
import java.io.File;
89
import java.io.IOException;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
@@ -23,7 +24,7 @@ public static void main(String[] args) throws IOException, ExecutionException, I
2324
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
2425

2526
System.out.println("Converting PDF to JPG and compressing result files with ZIP");
26-
CompletableFuture<ConversionResult> jpgResult = ConvertApi.convert("docx", "jpg", new Param("file", Paths.get("test-files/test.docx")));
27+
CompletableFuture<ConversionResult> jpgResult = ConvertApi.convert("docx", "jpg", new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.docx").getFile()).toPath()));
2728
System.out.println("ConvertApi.convert is not blocking method, proceeding to ZIP conversion");
2829

2930
CompletableFuture<ConversionResult> zipResult = ConvertApi.convert("jpg", "zip", new Param("files", jpgResult));

src/main/resources/examples/ConvertRemoteFile.java renamed to examples/src/main/java/com/convertapi/examples/ConvertRemoteFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;

src/main/resources/examples/ConvertWebToPdf.java renamed to examples/src/main/java/com/convertapi/examples/ConvertWebToPdf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;

src/main/resources/examples/ConvertWordToPdfAndPng.java renamed to examples/src/main/java/com/convertapi/examples/ConvertWordToPdfAndPng.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;
55
import com.convertapi.client.ConvertApi;
66
import com.convertapi.client.Param;
77

8+
import java.io.File;
89
import java.io.IOException;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
@@ -28,7 +29,7 @@ public static void main(String[] args) throws IOException, ExecutionException, I
2829

2930
System.out.println("Converting DOCX to PDF and JPG in parallel");
3031

31-
Param docxFileParam = new Param("file", Paths.get("test-files/test.docx"));
32+
Param docxFileParam = new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.docx").getFile()).toPath());
3233

3334
CompletableFuture<ConversionResult> pdfResult = ConvertApi.convert("docx", "pdf", docxFileParam);
3435
CompletableFuture<ConversionResult> jpgResult = ConvertApi.convert("docx", "jpg", docxFileParam);

src/main/resources/examples/CreatePdfThumbnail.java renamed to examples/src/main/java/com/convertapi/examples/CreatePdfThumbnail.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;
55
import com.convertapi.client.ConvertApi;
66
import com.convertapi.client.Param;
77

8+
import java.io.File;
89
import java.io.IOException;
910
import java.nio.file.Path;
1011
import java.nio.file.Paths;
@@ -21,13 +22,13 @@
2122

2223
public class CreatePdfThumbnail {
2324
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
24-
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
25+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a
2526
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
2627

2728
System.out.println("Creating PDF thumbnail");
2829

2930
CompletableFuture<ConversionResult> pdfFirstPageResult = ConvertApi.convert("pdf", "extract",
30-
new Param("file", Paths.get("test-files/test.pdf")),
31+
new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.pdf").getFile()).toPath()),
3132
new Param("pagerange", "1")
3233
);
3334

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.client.Config;
4+
import com.convertapi.client.ConvertApi;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import static java.lang.System.getenv;
9+
10+
/**
11+
* Most simple conversion example
12+
*/
13+
public class SimpleConversion {
14+
public static void main(String[] args) throws IOException {
15+
Config.setDefaultSecret(getenv("CONVERTAPI_SECRET"));
16+
String resourcePath = new File(AlternativeConverter.class.getClassLoader().getResource("test.docx").getFile()).getCanonicalPath();
17+
String tmpDir = System.getProperty("java.io.tmpdir") + "/";
18+
19+
// Simplified file to file conversion example
20+
ConvertApi.convert(resourcePath, tmpDir + "/result.pdf");
21+
}
22+
}

src/main/resources/examples/SplitAndMergePdf.java renamed to examples/src/main/java/com/convertapi/examples/SplitAndMergePdf.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
package com.convertapi.client.examples;
1+
package com.convertapi.examples;
22

33
import com.convertapi.client.Config;
44
import com.convertapi.client.ConversionResult;
55
import com.convertapi.client.ConvertApi;
66
import com.convertapi.client.Param;
7+
import java.io.File;
78

89
import java.io.IOException;
910
import java.nio.file.Path;
@@ -27,7 +28,7 @@ public static void main(String[] args) throws IOException, ExecutionException, I
2728
System.out.println("Creating PDF with the first and the last pages");
2829

2930
CompletableFuture<ConversionResult> splitResult = ConvertApi.convert("pdf", "split",
30-
new Param("file", Paths.get("test-files/test.pdf"))
31+
new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.pdf").getFile()).toPath())
3132
);
3233

3334
CompletableFuture<ConversionResult> mergeResult = ConvertApi.convert("pdf", "merge",

0 commit comments

Comments
 (0)