diff --git a/GETTING-STARTED.md b/GETTING-STARTED.md deleted file mode 100644 index 1bb8945..0000000 --- a/GETTING-STARTED.md +++ /dev/null @@ -1,161 +0,0 @@ -#### 1. Install the ConvertAPI library - -Go to the [ConvertAPI Java client](https://github.com/ConvertAPI/convertapi-java) page and download the JAR file. -Place JAR into your project library directory. - -The library is also available in Maven. -Add the following dependency to your pom.xml: -```xml - - com.convertapi.client - convertapi - 2.2 - -``` - -#### 2.a. Simple conversion methods - -```java -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -// Simplified file to file conversion example -ConvertApi.convertFile("test.docx", "/tmp/result.pdf"); - -// Simplified web site to pdf conversion example -ConvertApi.convertUrl("http://example.com", "/tmp/example.pdf"); - -// Simplified remote file to local file conversion example -ConvertApi.convertRemoteFile("https://cdn.convertapi.com/cara/testfiles/document.docx", "/tmp/demo.pdf"); -``` - - -#### 2.b. Convert a local file - -```java -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -//Set input and output formats and pass file parameter. -//Word to PDF API. Read more https://www.convertapi.com/docx-to-pdf -ConvertApi.convert("docx", "pdf", - new Param("file", Paths.get("test.docx")) -).get().saveFile(Paths.get("/tmp")).get(); -``` - - -#### 2.c. Convert a remote file and set additional parameters - -```java -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -//Set input and output formats and pass file parameter. -//PowerPoint to PNG API. Read more https://www.convertapi.com/pptx-to-png -ConvertApi.convert("pdf", "png", - new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx"), - new Param("scaleimage", "true"), - new Param("scaleproportions", "true"), - new Param("imageheight", 300) -).get().saveFile(Paths.get("/tmp")).get(); -``` - - -#### 2.d. Convert from a stream - -```java -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -// Create stream from HTML string -String streamContent = "

My First Heading

My first paragraph.

"; -InputStream stream = new ByteArrayInputStream(streamContent.getBytes()); - -// Html to PDF API. Read more https://www.convertapi.com/html-to-pdf -CompletableFuture result = ConvertApi.convert("html", "pdf", - new Param("file", stream, "test.html") -); - -// PDF as stream -InputStream resultStream = result.get().asStream().get(); -``` - - -#### 2.e. Conversions chaining - -```java -// Split PDF document and merge first and last pages to new PDF -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -// Set input and output formats and pass file parameter. -// Split PDF API. Read more https://www.convertapi.com/pdf-to-split -CompletableFuture splitResult = ConvertApi.convert("pdf", "split", - new Param("file", Paths.get("test.pdf")) -); - -// Get result of the first conversion and use it in Merge conversion. -// Chains are executed on server without moving files. -// Merge PDF API. Read more https://www.convertapi.com/pdf-to-merge -CompletableFuture mergeResult = ConvertApi.convert("pdf", "merge", - new Param("files", splitResult, 0), - new Param("files", splitResult, -1) -); - -mergeResult.get().saveFile(Paths.get("/tmp")).get(); -``` - - -#### 3. Read account status - -```java -import com.convertapi.*; -import com.convertapi.model.*; - -Config.setDefaultSecret("your-api-secret"); - -// Read full account data -User user = ConvertApi.getUser(); - -// Find out how much seconds left -int secondsLeft = user.SecondsLeft; -``` - - -#### 4. Exception handling (asynchronous) - -```java -import com.convertapi.*; - -Config.setDefaultSecret("your-api-secret"); - -// Creating an exception -CompletableFuture resultFuture = ConvertApi.convert("pdf", "pptx", - new Param("file", Paths.get("test-files/test.pdf")), - new Param("AutoRotate","WrongParameter") -).exceptionally(t -> { - // Handling and exception - System.out.println("Error message: " + t.getMessage()); - return null; -}); - -ConversionResult result = resultFuture.get(); -// Saving file if there was no exception -if (result != null) { - result.saveFile(Paths.get("/tmp")).get(); -} -``` - - -#### 5. Supported file formats, conversions and actions - -https://www.convertapi.com/doc/supported-formats - -#### 6. GitHub - -https://github.com/ConvertAPI/convertapi-java diff --git a/README.md b/README.md index 3c3466d..feb2fe4 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Add the following dependency to your pom.xml: com.convertapi.client convertapi - 2.8 + 2.9 ``` @@ -69,7 +69,7 @@ CompletableFuture result = ConvertApi.convert("pptx", "pdf", #### Additional conversion parameters ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion -parameters and explanations can be found [here](https://www.convertapi.com). +parameters and explanations can be found [here](https://www.convertapi.com/conversions). ```java CompletableFuture result = ConvertApi.convert("pdf", "jpg", @@ -82,11 +82,20 @@ CompletableFuture result = ConvertApi.convert("pdf", "jpg", ### User information -You can always check remaining seconds amount by fetching [user information](https://www.convertapi.com/doc/user). +You can always check the remaining conversions amount and other account information by fetching [user information](https://www.convertapi.com/doc/user). ```java User user = ConvertApi.getUser(); -int secondsLeft = user.SecondsLeft; +int conversionsTotal = user.ConversionsTotal; +int conversionsConsumed = user.ConversionsConsumed; +``` + +### Alternative domain + +Create `Config` instance with the alternative domain and provide it in `convert` method. Dedicated to the region [domain list](https://www.convertapi.com/doc/servers-location). + +```java +Config config = new Config(secret, "https", "eu-v2.convertapi.com", 0, httpClientBuilder); ``` ### More examples diff --git a/examples/pom.xml b/examples/pom.xml index 142c235..1880299 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -15,7 +15,7 @@ com.convertapi.client convertapi - 2.8 + 2.9 diff --git a/examples/src/main/java/com/convertapi/examples/UserInformation.java b/examples/src/main/java/com/convertapi/examples/UserInformation.java index 96a0568..80095fd 100644 --- a/examples/src/main/java/com/convertapi/examples/UserInformation.java +++ b/examples/src/main/java/com/convertapi/examples/UserInformation.java @@ -22,6 +22,7 @@ public static void main(String[] args) { System.out.println("Name: " + user.FullName); System.out.println("Status: " + user.Status); System.out.println("Active: " + user.Active); - System.out.println("Seconds left: " + user.SecondsLeft); + System.out.println("Total Conversions: " + user.ConversionsTotal); + System.out.println("Conversions Consumed: " + user.ConversionsConsumed); } } diff --git a/pom.xml b/pom.xml index e5c02e1..e037ae0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ convertapi jar - 2.9 + 2.10 ConvertAPI Java Client The ConvertAPI helps converting various file formats. @@ -48,7 +48,7 @@ scm:git:git://github.com/ConvertAPI/convertapi-java.git scm:git:git@github.com:ConvertAPI/convertapi-java.git https://github.com/ConvertAPI/convertapi-java - v2.9 + v2.10 diff --git a/src/main/java/com/convertapi/client/ConvertApi.java b/src/main/java/com/convertapi/client/ConvertApi.java index 2393292..f85bc20 100644 --- a/src/main/java/com/convertapi/client/ConvertApi.java +++ b/src/main/java/com/convertapi/client/ConvertApi.java @@ -71,6 +71,7 @@ public static CompletableFuture convert(String fromFormat, Str Request request = Http.getRequestBuilder() .url(url) .addHeader("Accept", "application/json") + .addHeader("Content-Type", "multipart/form-data") .post(multipartBuilder.build()) .build(); diff --git a/src/main/java/com/convertapi/client/Http.java b/src/main/java/com/convertapi/client/Http.java index aedd690..bca3562 100644 --- a/src/main/java/com/convertapi/client/Http.java +++ b/src/main/java/com/convertapi/client/Http.java @@ -62,12 +62,11 @@ static CompletableFuture requestGet(String url) { static CompletableFuture requestDelete(String url) { return CompletableFuture.supplyAsync(() -> { Request request = getRequestBuilder().delete().url(url).build(); - try { - getClient().newCall(request).execute().close(); - return null; + try (Response response = getClient().newCall(request).execute()) { } catch (IOException e) { throw new RuntimeException(e); } + return null; }); } diff --git a/src/main/java/com/convertapi/client/model/User.java b/src/main/java/com/convertapi/client/model/User.java index 9c385d3..ae8bb46 100644 --- a/src/main/java/com/convertapi/client/model/User.java +++ b/src/main/java/com/convertapi/client/model/User.java @@ -8,6 +8,7 @@ public class User { public boolean Active; public String FullName; public String Email; - public int SecondsLeft; + public int ConversionsTotal; + public int ConversionsConsumed; public String Status; }