Skip to content

Commit 09b199b

Browse files
committed
Description
1 parent c2b91f1 commit 09b199b

File tree

10 files changed

+188
-16
lines changed

10 files changed

+188
-16
lines changed

LICENSE.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ConvertAPI
2+
https://www.convertapi.com
3+
(c) 2011-2018 Baltsoft
4+
5+
ConvertAPI-Java
6+
https://github.com/ConvertAPI/convertapi-java
7+
Copyright (c) 2018 Tomas Rutkauskas
8+
9+
The MIT License
10+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# ConvertAPI Java Client
2+
## Convert your files with our online file conversion API
3+
4+
The ConvertAPI helps converting various file formats.
5+
Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes.
6+
Merge, Encrypt, Split, Repair and Decrypt PDF files.
7+
And many others files manipulations.
8+
In just few minutes you can integrate it into your application and use it easily.
9+
10+
The ConvertAPI-Java library makes it easier to use the Convert API from your Java 8 projects without having to build your own API calls.
11+
You can get your free API secret at https://www.convertapi.com/a
12+
13+
## Installation
14+
15+
Download [latest JAR file](https://www.convertapi.com) and place it in your project library directory.
16+
17+
## Usage
18+
19+
### Configuration
20+
21+
You can get your secret at https://www.convertapi.com/a
22+
23+
```java
24+
Config.setDefaultSecret("YOUR API SECRET");
25+
```
26+
27+
### File conversion
28+
29+
Example to convert file to PDF. All supported formats and options can be found
30+
[here](https://www.convertapi.com).
31+
32+
```java
33+
CompletableFuture<ConversionResult> result = ConvertApi.convert("docx", "pdf", new Param[]{
34+
new Param("file", Paths.get("test-files/test.docx"))
35+
});
36+
37+
// save to file
38+
result.get().saveFile(Paths.get("/tmp/my_file.pdf"));
39+
```
40+
41+
Other result operations:
42+
43+
```java
44+
// save all result files to folder
45+
result.get().saveFiles(Paths.get("/tmp"));
46+
47+
// get result file
48+
ConversionResultFile file = result.get().getFile(0);
49+
50+
// get conversion cost
51+
Integer cost = result.get().conversionCost();
52+
```
53+
54+
#### Convert file url
55+
56+
```java
57+
CompletableFuture<ConversionResult> result = ConvertApi.convert("pptx", "pdf", new Param[]{
58+
new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx")
59+
});
60+
```
61+
62+
#### Additional conversion parameters
63+
64+
ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion
65+
parameters and explanations can be found [here](https://www.convertapi.com).
66+
67+
```java
68+
CompletableFuture<ConversionResult> result = ConvertApi.convert("pdf", "jpg", new Param[]{
69+
new Param("file", Paths.get("test-files/test.pdf")),
70+
new Param("scaleimage", "true"),
71+
new Param("scaleproportions", "true"),
72+
new Param("imageheight", 300)
73+
});
74+
```
75+
76+
### User information
77+
78+
You can always check remaining seconds amount by fetching [user information](https://www.convertapi.com/doc/user).
79+
80+
```java
81+
User user = ConvertApi.getUser();
82+
int secondsLeft = user.SecondsLeft;
83+
```
84+
85+
### More examples
86+
87+
You can find more advanced examples in the [examples](https://github.com/ConvertAPI/convertapi-java/tree/master/src/main/java/com/convertapi/examples) folder.
88+
89+
#### Converting your first file, full example:
90+
91+
ConvertAPI is designed to make converting file super easy, the following snippet shows how easy it is to get started. Let's convert WORD DOCX file to PDF:
92+
93+
```java
94+
package com.convertapi.examples;
95+
96+
import com.convertapi.ConvertApi;
97+
98+
public class SimpleConversion {
99+
public static void main(String[] args) {
100+
ConvertApi.convert("test-files/test.docx", "result.pdf", "YOUR API SECRET");
101+
}
102+
}
103+
```
104+
105+
This is the bare-minimum to convert a file using the ConvertAPI client, but you can do a great deal more with the ConvertAPI Java library.
106+
Take special note that you should replace `YOUR API SECRET` with the secret you obtained in item two of the pre-requisites.
107+
108+
### Issues &amp; Comments
109+
Please leave all comments, bugs, requests, and issues on the Issues page. We'll respond to your request ASAP!
110+
111+
### License
112+
The ConvertAPI Java Library is licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php "Read more about the MIT license form") license.
113+
Refere to the [LICENSE](https://github.com/ConvertAPI/convertapi-java/blob/master/LICENSE) file for more information.

convertapi-java.iml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
99
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
1010
<excludeFolder url="file://$MODULE_DIR$/.idea" />
11+
<excludeFolder url="file://$MODULE_DIR$/src/main/java/com/convertapi/examples" />
12+
<excludeFolder url="file://$MODULE_DIR$/src/main/java/com/convertapi/examples/test-files" />
1113
<excludeFolder url="file://$MODULE_DIR$/src/test" />
1214
<excludeFolder url="file://$MODULE_DIR$/target" />
13-
<excludeFolder url="file://$MODULE_DIR$/test-files" />
1415
</content>
1516
<orderEntry type="inheritedJdk" />
1617
<orderEntry type="sourceFolder" forTests="false" />

src/main/java/com/convertapi/Config.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static Config defaults() {
1818
return new Config(Config.defaultSecret, SCHEME, HOST, TIMEOUT);
1919
}
2020

21+
public static Config defaults(String secret) {
22+
return new Config(secret, SCHEME, HOST, TIMEOUT);
23+
}
24+
2125
public Config(String secret, String scheme, String host, int timeout) {
2226
this.scheme = scheme;
2327
this.host = host;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.convertapi;
22

33
public class ConversionException extends RuntimeException{
4-
public ConversionException(String message){
4+
private int httpStatusCode;
5+
6+
public ConversionException(String message, int httpStatusCode){
57
super(message);
8+
this.httpStatusCode = httpStatusCode;
9+
}
10+
11+
public int getHttpStatusCode() {
12+
return httpStatusCode;
613
}
714
}

src/main/java/com/convertapi/ConversionResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ConversionResult(ConversionResponse responseFuture) {
1818
this.response = responseFuture;
1919
}
2020

21-
public Integer fileCount() throws ExecutionException, InterruptedException {
21+
public Integer fileCount() {
2222
return response.Files.length;
2323
}
2424

@@ -28,7 +28,7 @@ public List<String> urls() {
2828
return valueList;
2929
}
3030

31-
public Integer conversionCost() throws ExecutionException, InterruptedException {
31+
public Integer conversionCost() {
3232
return response.ConversionCost;
3333
}
3434

src/main/java/com/convertapi/ConvertApi.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import okhttp3.Response;
1010

1111
import java.io.IOException;
12+
import java.nio.file.Path;
13+
import java.nio.file.Paths;
1214
import java.util.Arrays;
1315
import java.util.List;
1416
import java.util.concurrent.CompletableFuture;
@@ -58,7 +60,7 @@ public static CompletableFuture<ConversionResult> convert(String fromFormat, Str
5860
Response response = Http.getClient().newCall(request).execute();
5961
bodyString = response.body().string();
6062
if (response.code() != 200) {
61-
throw new ConversionException(bodyString);
63+
throw new ConversionException(bodyString, response.code());
6264
}
6365
} catch (IOException e) {
6466
throw new RuntimeException(e);
@@ -86,12 +88,39 @@ public static User getUser(Config config) {
8688
Response response = Http.getClient().newCall(request).execute();
8789
bodyString = response.body().string();
8890
if (response.code() != 200) {
89-
throw new ConversionException(bodyString);
91+
throw new ConversionException(bodyString, response.code());
9092
}
9193
} catch (IOException e) {
9294
throw new RuntimeException(e);
9395
}
9496

9597
return new Gson().fromJson(bodyString, User.class);
9698
}
99+
100+
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat) throws IOException {
101+
return convert(fromFile, toFormat, Config.defaults().getSecret());
102+
}
103+
104+
public static CompletableFuture<ConversionResult> convert(Path fromFile, String toFormat, String secret) throws IOException {
105+
return convert(getFileExtension(fromFile), toFormat, new Param[]{new Param("file", fromFile)}, Config.defaults(secret));
106+
}
107+
108+
public static void convert(String fromPathToFile, String toPathToFile) {
109+
convert(fromPathToFile, toPathToFile, Config.defaults().getSecret());
110+
}
111+
112+
public static void convert(String fromPathToFile, String toPathToFile, String secret) {
113+
try {
114+
Path fromPath = Paths.get(fromPathToFile);
115+
Path toPath = Paths.get(toPathToFile);
116+
convert(fromPath, getFileExtension(toPath), secret).get().saveFile(toPath).get();
117+
} catch (IOException | ExecutionException | InterruptedException e) {
118+
throw new RuntimeException(e);
119+
}
120+
}
121+
122+
private static String getFileExtension(Path path) {
123+
String name = path.getFileName().toString();
124+
return name.substring(name.lastIndexOf(".") + 1);
125+
}
97126
}

src/main/java/com/convertapi/examples/CreatePdfThumbnail.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.io.IOException;
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
11-
import java.util.List;
1211
import java.util.concurrent.CompletableFuture;
1312
import java.util.concurrent.ExecutionException;
1413

@@ -32,9 +31,9 @@ public static void main(String[] args) throws IOException, ExecutionException, I
3231

3332
CompletableFuture<ConversionResult> thumbnailResult = ConvertApi.convert("pdf", "jpg", new Param[]{
3433
new Param("file", pdfFirstPageResult),
35-
new Param("scaleimage","true"),
36-
new Param("scaleproportions","true"),
37-
new Param("imageheight",300)
34+
new Param("scaleimage", "true"),
35+
new Param("scaleproportions", "true"),
36+
new Param("imageheight", 300)
3837
});
3938

4039
System.out.println("JPG thumbnail file saved to: " + thumbnailResult.get().saveFile(tempDir).get());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.convertapi.examples;
2+
3+
import com.convertapi.ConvertApi;
4+
5+
/**
6+
* Most simple conversion example
7+
*/
8+
9+
public class SimpleConversion {
10+
public static void main(String[] args) {
11+
ConvertApi.convert("test-files/test.docx", "result.pdf", "YOUR API SECRET");
12+
}
13+
}

src/main/java/com/convertapi/examples/UserInformation.java

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

33
import com.convertapi.Config;
4-
import com.convertapi.ConversionResult;
54
import com.convertapi.ConvertApi;
6-
import com.convertapi.Param;
75
import com.convertapi.model.User;
86

97
import java.io.IOException;
10-
import java.nio.file.Path;
11-
import java.nio.file.Paths;
12-
import java.util.List;
13-
import java.util.concurrent.CompletableFuture;
148
import java.util.concurrent.ExecutionException;
159

1610
/**

0 commit comments

Comments
 (0)