Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDFCLOUD-2509 Add Complex Flow Java samples #64

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/verify-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ jobs:
run: mvn -f 'Java/Endpoint Examples/Multipart Payload' --show-version --batch-mode --update-snapshots verify
- name: Build JSON Payload examples with Maven
run: mvn -f 'Java/Endpoint Examples/JSON Payload' --show-version --batch-mode --update-snapshots verify
- name: Build Complex Flow examples with Maven
run: mvn -f 'Java/Complex Flow Examples' --show-version --batch-mode --update-snapshots verify
150 changes: 150 additions & 0 deletions Java/Complex Flow Examples/DecryptAddReencrypt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONObject;

/* In this sample, we will show how to take an encrypted file and decrypt, modify
* and re-encrypt it to create an encryption-at-rest solution as discussed in
* https://pdfrest.com/solutions/create-secure-document-workflows-with-pdf-password-protection/
* We will be running the document through /decrypted-pdf to open the document
* to modification, running the decrypted result through /pdf-with-added-image,
* and then sending the output with the new image through /encrypted-pdf to
* lock it up again.
*/

public class DecryptAddReencrypt {

// Specify the path to your PDF file here, or as the first argument when running the program.
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";

// Specify the path to your image file here, or as the second argument when running the
// program.
private static final String DEFAULT_IMAGE_PATH = "/path/to/file.pnf";
datalogics-tsmith marked this conversation as resolved.
Show resolved Hide resolved

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

public static void main(String[] args) {
File inputFile, imageFile;
if (args.length > 1) {
inputFile = new File(args[0]);
imageFile = new File(args[1]);
} else {
inputFile = new File(DEFAULT_FILE_PATH);
imageFile = new File(DEFAULT_IMAGE_PATH);
}

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
RequestBody decryptRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("current_open_password", "password")
.addFormDataPart("output", "pdfrest_decrypted")
.build();
Request decryptRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/decrypted-pdf")
.post(decryptRequestBody)
.build();
try {
OkHttpClient decryptClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response decryptResponse = decryptClient.newCall(decryptRequest).execute();

System.out.println("Result code from decrypt call: " + decryptResponse.code());
if (decryptResponse.body() != null) {
String decryptResponseString = decryptResponse.body().string();

JSONObject decryptJSON = new JSONObject(decryptResponseString);
if (decryptJSON.has("error")) {
System.out.println("Error during decrypt call: " + decryptResponse.body().string());
return;
}

String decryptID = decryptJSON.get("outputId").toString();

final RequestBody imageFileRequestBody =
RequestBody.create(imageFile, MediaType.parse("application/png"));
RequestBody addImageRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", decryptID)
.addFormDataPart("image_file", imageFile.getName(), imageFileRequestBody)
.addFormDataPart("page", "1")
.addFormDataPart("x", "0")
.addFormDataPart("y", "0")
.addFormDataPart("output", "pdfrest_added_image")
.build();
Request addImageRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf-with-added-image")
.post(addImageRequestBody)
.build();
try {
OkHttpClient addImageClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response addImageResponse = addImageClient.newCall(addImageRequest).execute();

System.out.println("Result code from add image call: " + addImageResponse.code());
if (addImageResponse.body() != null) {
String addImageResponseString = addImageResponse.body().string();

JSONObject addImageJSON = new JSONObject(addImageResponseString);
if (addImageJSON.has("error")) {
System.out.println(
"Error during add image call: " + addImageResponse.body().string());
return;
}

String addImageID = addImageJSON.get("outputId").toString();

RequestBody encryptRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", addImageID)
.addFormDataPart("new_open_password", "password")
.addFormDataPart("output", "pdfrest_encrypted")
.build();
Request encryptRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/encrypted-pdf")
.post(encryptRequestBody)
.build();
try {
OkHttpClient encryptClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response encryptResponse = encryptClient.newCall(encryptRequest).execute();
System.out.println("Result code from encrypt call: " + encryptResponse.code());
if (encryptResponse.body() != null) {
System.out.println(prettyJson(encryptResponse.body().string()));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String prettyJson(String json) {
// https://stackoverflow.com/a/9583835/11996393
return new JSONObject(json).toString(4);
}
}
152 changes: 152 additions & 0 deletions Java/Complex Flow Examples/MergeDifferentFileTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONObject;

/* In this sample, we will show how to merge different file types together as
* discussed in https://pdfrest.com/solutions/merge-multiple-types-of-files-together/.
First, we will upload an image file to the /pdf route and capture the output ID.
* Next, we will upload a PowerPoint file to the /pdf route and capture its output
* ID. Finally, we will pass both IDs to the /merged-pdf route to combine both inputs
* into a single PDF.
*
* Note that there is nothing special about an image and a PowerPoint file, and
* this sample could be easily used to convert and combine any two file types
* that the /pdf route takes as inputs.
*/

public class MergeDifferentFileTypes {

// Specify the path to your first file here, or as the first argument when running the program.
private static final String DEFAULT_FIRST_FILE_PATH = "/path/to/file.png";

// Specify the path to your second file here, or as the second argument when running the
// program.
private static final String DEFAULT_SECOND_FILE_PATH = "/path/to/file.ppt";

// Specify your API key here, or in the environment variable PDFREST_API_KEY.
// You can also put the environment variable in a .env file.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

public static void main(String[] args) {
File firstFile, secondFile;
if (args.length > 1) {
firstFile = new File(args[0]);
secondFile = new File(args[1]);
} else {
firstFile = new File(DEFAULT_FIRST_FILE_PATH);
secondFile = new File(DEFAULT_SECOND_FILE_PATH);
}

final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

final RequestBody firstFileInputFileRequestBody =
RequestBody.create(firstFile, MediaType.parse("application/png"));
RequestBody firstFileRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", firstFile.getName(), firstFileInputFileRequestBody)
.addFormDataPart("output", "pdfrest_pdf")
.build();
Request firstFileRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf")
.post(firstFileRequestBody)
.build();
try {
OkHttpClient firstFileClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response firstFileResponse = firstFileClient.newCall(firstFileRequest).execute();

System.out.println("Result code from first PDF call: " + firstFileResponse.code());
if (firstFileResponse.body() != null) {
String firstFileResponseString = firstFileResponse.body().string();

JSONObject firstFileJSON = new JSONObject(firstFileResponseString);
if (firstFileJSON.has("error")) {
System.out.println("Error during first PDF call: " + firstFileResponse.body().string());
return;
}

String firstFileID = firstFileJSON.get("outputId").toString();

final RequestBody secondFileInputFileRequestBody =
RequestBody.create(secondFile, MediaType.parse("application/powerpoint"));
RequestBody secondFileRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", secondFile.getName(), secondFileInputFileRequestBody)
.addFormDataPart("output", "pdfrest_pdf")
.build();
Request secondFileRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf")
.post(secondFileRequestBody)
.build();
try {
OkHttpClient secondFileClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

Response secondFileResponse = secondFileClient.newCall(secondFileRequest).execute();

System.out.println("Result code from second PDF call: " + secondFileResponse.code());
if (secondFileResponse.body() != null) {
String secondFileResponseString = secondFileResponse.body().string();

JSONObject secondFileJSON = new JSONObject(secondFileResponseString);
if (secondFileJSON.has("error")) {
System.out.println(
"Error during second PDF call: " + secondFileResponse.body().string());
return;
}

String secondFileID = secondFileJSON.get("outputId").toString();

RequestBody mergeRequestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id[]", firstFileID)
.addFormDataPart("id[]", secondFileID)
.addFormDataPart("type[]", "id")
.addFormDataPart("type[]", "id")
.addFormDataPart("pages[]", "1-last")
.addFormDataPart("pages[]", "1-last")
.addFormDataPart("output", "pdfrest_merged")
.build();
Request mergeRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/merged-pdf")
.post(mergeRequestBody)
.build();
try {
OkHttpClient mergeClient =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response mergeResponse = mergeClient.newCall(mergeRequest).execute();
System.out.println("Result code from merge call: " + mergeResponse.code());
if (mergeResponse.body() != null) {
System.out.println(prettyJson(mergeResponse.body().string()));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String prettyJson(String json) {
// https://stackoverflow.com/a/9583835/11996393
return new JSONObject(json).toString(4);
}
}
Loading