diff --git a/.github/workflows/verify-java.yml b/.github/workflows/verify-java.yml index bc53ada..4ae6d9f 100644 --- a/.github/workflows/verify-java.yml +++ b/.github/workflows/verify-java.yml @@ -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 diff --git a/Java/Complex Flow Examples/DecryptAddReencrypt.java b/Java/Complex Flow Examples/DecryptAddReencrypt.java new file mode 100644 index 0000000..4028508 --- /dev/null +++ b/Java/Complex Flow Examples/DecryptAddReencrypt.java @@ -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.jpg"; + + // 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); + } +} diff --git a/Java/Complex Flow Examples/MergeDifferentFileTypes.java b/Java/Complex Flow Examples/MergeDifferentFileTypes.java new file mode 100644 index 0000000..f24ca66 --- /dev/null +++ b/Java/Complex Flow Examples/MergeDifferentFileTypes.java @@ -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); + } +} diff --git a/Java/Complex Flow Examples/PDFA3bWithAttachment.java b/Java/Complex Flow Examples/PDFA3bWithAttachment.java new file mode 100644 index 0000000..c93544a --- /dev/null +++ b/Java/Complex Flow Examples/PDFA3bWithAttachment.java @@ -0,0 +1,112 @@ +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 attach an xml document to a PDF file and then +* convert the file with the attachment to conform to the PDF/A standard, which +* can be useful for invoicing and standards compliance. We will be running the +* input document through /pdf-with-added-attachment to add the attachment and +* then /pdfa to do the PDF/A conversion. + +* Note that there is nothing special about attaching an xml file, and any appropriate +* file may be attached and wrapped into the PDF/A conversion. +*/ + +public class PDFA3bWithAttachment { + + // Specify the path to your 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 file attachment here, or as the second argument when running the + // program. + private static final String DEFAULT_ATTACHMENT_PATH = "/path/to/file.xml"; + + // 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, attachmentFile; + if (args.length > 1) { + inputFile = new File(args[0]); + attachmentFile = new File(args[1]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + attachmentFile = new File(DEFAULT_ATTACHMENT_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody attachmentInputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + final RequestBody attachmentFileRequestBody = + RequestBody.create(attachmentFile, MediaType.parse("application/xml")); + RequestBody attachmentRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), attachmentInputFileRequestBody) + .addFormDataPart("file_to_attach", attachmentFile.getName(), attachmentFileRequestBody) + .addFormDataPart("output", "pdfrest_attachment") + .build(); + Request attachmentRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdf-with-added-attachment") + .post(attachmentRequestBody) + .build(); + try { + OkHttpClient attachmentClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response attachmentResponse = attachmentClient.newCall(attachmentRequest).execute(); + + System.out.println("Result code from attachment call: " + attachmentResponse.code()); + if (attachmentResponse.body() != null) { + String attachmentResponseString = attachmentResponse.body().string(); + + JSONObject attachmentJSON = new JSONObject(attachmentResponseString); + if (attachmentJSON.has("error")) { + System.out.println("Error during attachment call: " + attachmentResponse.body().string()); + return; + } + + String attachmentID = attachmentJSON.get("outputId").toString(); + + RequestBody pdfaRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("id", attachmentID) + .addFormDataPart("output_type", "PDF/A-3b") + .addFormDataPart("output", "pdfrest_pdfa") + .build(); + Request pdfaRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdfa") + .post(pdfaRequestBody) + .build(); + try { + OkHttpClient pdfaClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + Response pdfaResponse = pdfaClient.newCall(pdfaRequest).execute(); + System.out.println("Result code from pdfa call: " + pdfaResponse.code()); + if (pdfaResponse.body() != null) { + System.out.println(prettyJson(pdfaResponse.body().string())); + } + } 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); + } +} diff --git a/Java/Complex Flow Examples/PreserveWordDocument.java b/Java/Complex Flow Examples/PreserveWordDocument.java new file mode 100644 index 0000000..7304c4e --- /dev/null +++ b/Java/Complex Flow Examples/PreserveWordDocument.java @@ -0,0 +1,100 @@ +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 optimize a Word file for long-term preservation + * as discussed in https://pdfrest.com/solutions/optimize-word-excel-and-powerpoint-files-for-long-term-preservation/ + * We will take our Word (or Excel or PowerPoint) document and first convert it to + * a PDF with a call to the /pdf route. Then, we will take that converted PDF + * and convert it to the PDF/A format for long-term storage. + */ + +public class PreserveWordDocument { + + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; + + // 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; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody pdfInputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody pdfRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), pdfInputFileRequestBody) + .addFormDataPart("output", "pdfrest_pdf") + .build(); + Request pdfRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdf") + .post(pdfRequestBody) + .build(); + try { + OkHttpClient pdfClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response pdfResponse = pdfClient.newCall(pdfRequest).execute(); + + System.out.println("Result code from pdf call: " + pdfResponse.code()); + if (pdfResponse.body() != null) { + String pdfResponseString = pdfResponse.body().string(); + + JSONObject pdfJSON = new JSONObject(pdfResponseString); + if (pdfJSON.has("error")) { + System.out.println("Error during pdf call: " + pdfResponse.body().string()); + return; + } + + String pdfID = pdfJSON.get("outputId").toString(); + + RequestBody pdfaRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("id", pdfID) + .addFormDataPart("output_type", "PDF/A-3b") + .addFormDataPart("output", "pdfrest_pdfa") + .build(); + Request pdfaRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/pdfa") + .post(pdfaRequestBody) + .build(); + try { + OkHttpClient pdfaClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + Response pdfaResponse = pdfaClient.newCall(pdfaRequest).execute(); + System.out.println("Result code from pdfa call: " + pdfaResponse.code()); + if (pdfaResponse.body() != null) { + System.out.println(prettyJson(pdfaResponse.body().string())); + } + } 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); + } +} diff --git a/Java/Complex Flow Examples/ProtectedWatermark.java b/Java/Complex Flow Examples/ProtectedWatermark.java new file mode 100644 index 0000000..4080572 --- /dev/null +++ b/Java/Complex Flow Examples/ProtectedWatermark.java @@ -0,0 +1,104 @@ +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 watermark a PDF document and then restrict + * editing on the document so that the watermark cannot be removed, as discussed in + * https://pdfrest.com/solutions/add-pdf-watermarks-that-cannot-be-removed/. + * We will be running the input file through /watermarked-pdf to apply the watermark + * and then /restricted-pdf to lock the watermark in. + */ + +public class ProtectedWatermark { + + // Specify the path to your file here, or as the first argument when running the program. + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; + + // 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; + if (args.length > 0) { + inputFile = new File(args[0]); + } else { + inputFile = new File(DEFAULT_FILE_PATH); + } + + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); + + final RequestBody watermarkInputFileRequestBody = + RequestBody.create(inputFile, MediaType.parse("application/pdf")); + RequestBody watermarkRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("file", inputFile.getName(), watermarkInputFileRequestBody) + .addFormDataPart("watermark_text", "WATERMARK") + .addFormDataPart("output", "pdfrest_watermarked") + .build(); + Request watermarkRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/watermarked-pdf") + .post(watermarkRequestBody) + .build(); + try { + OkHttpClient watermarkClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + + Response watermarkResponse = watermarkClient.newCall(watermarkRequest).execute(); + + System.out.println("Result code from watermark call: " + watermarkResponse.code()); + if (watermarkResponse.body() != null) { + String watermarkResponseString = watermarkResponse.body().string(); + + JSONObject watermarkJSON = new JSONObject(watermarkResponseString); + if (watermarkJSON.has("error")) { + System.out.println("Error during watermark call: " + watermarkResponse.body().string()); + return; + } + + String watermarkID = watermarkJSON.get("outputId").toString(); + + RequestBody restrictRequestBody = + new MultipartBody.Builder() + .setType(MultipartBody.FORM) + .addFormDataPart("id", watermarkID) + .addFormDataPart("new_permissions_password", "password") + .addFormDataPart("restrictions[]", "copy_content") + .addFormDataPart("restrictions[]", "edit_annotations") + .addFormDataPart("restrictions[]", "edit_content") + .addFormDataPart("output", "pdfrest_restricted") + .build(); + Request restrictRequest = + new Request.Builder() + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) + .url("https://api.pdfrest.com/restricted-pdf") + .post(restrictRequestBody) + .build(); + try { + OkHttpClient restrictClient = + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); + Response restrictResponse = restrictClient.newCall(restrictRequest).execute(); + System.out.println("Result code from restrict call: " + restrictResponse.code()); + if (restrictResponse.body() != null) { + System.out.println(prettyJson(restrictResponse.body().string())); + } + } 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); + } +} diff --git a/Java/Complex Flow Examples/README.md b/Java/Complex Flow Examples/README.md new file mode 100644 index 0000000..b8cca32 --- /dev/null +++ b/Java/Complex Flow Examples/README.md @@ -0,0 +1,74 @@ +# Java + +The Java samples are all set up to: + +- Take input paths from either the program source or the command line. +- Take credential information from a `.env` file (recommended) or the + environment, but the key can also be set in the source. + +## Requirements + +- A Java SE 17 (LTS) compatible installation of the JDK +- Maven 3.9.4 + +## Preparation + +Place your pdfRest API Key in a variable named `PDFREST_API_KEY` in a file +named `.env`: + +```shell +PDFREST_API_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx +``` + +## Build/run from the command line + +To build the Java samples, run Maven: + +```shell +mvn package +``` + +To run a sample, place the jar-with-dependencies on the classpath, specify the +class name of the sample, and then follow that with the input files. + +```shell +java -cp target/pdf-rest-api-samples-1.0-SNAPSHOT-jar-with-dependencies.jar CompressedPdf input.pdf +``` + +## Build/run from IntelliJ IDEA + +- Open the project directory in IntelliJ IDEA. IDEA should recognize the project + to be a Maven project. If it asks any questions about importing the project as + Maven, answer affirmatively. +- Open the Maven tool window (View->Tool Windows->Maven) and click the **Reload all Maven Projects** button (it looks like a pair of chasing arrows). This will make sure all the dependencies are downloaded. +- Open the source file for the sample you want to run. +- Click the green, triangular Run button next to the name of the main class. +- Pick **Modify Run Configuration...** +- Add your desired input file(s) to the **Program arguments** field. +- Click **OK** +- Click the run button next to the run configuration in the toolbar. + +See: [IntelliJ IDEA Maven documentation](https://www.jetbrains.com/help/idea/maven-support.html) + +## Do linting and formatting on files + +This project uses [Spotless](https://github.com/diffplug/spotless) to apply +canonical formatting to the Java source code and `pom.xml`. + +To check the files, either do + +```shell +mvn verify +``` + +or + +```shell +mvn spotless:check +``` + +To correct problems and format the files, do + +```shell +mvn spotless:apply +``` \ No newline at end of file diff --git a/Java/Complex Flow Examples/pom.xml b/Java/Complex Flow Examples/pom.xml new file mode 100644 index 0000000..a0eaa62 --- /dev/null +++ b/Java/Complex Flow Examples/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + org.example + pdf-rest-api-samples + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + com.squareup.okhttp3 + okhttp + 4.10.0 + + + org.json + json + 20230618 + + + io.github.cdimascio + dotenv-java + 3.0.0 + + + + + + + + maven-assembly-plugin + + + jar-with-dependencies + + + + + make-assembly + + + single + + + package + + + + + com.diffplug.spotless + spotless-maven-plugin + 2.39.0 + + + + **/*.java + + + + + + + + + + + + + check + + + + + + . + +