forked from pdfrest/pdfrest-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryptedPDF.java
More file actions
140 lines (120 loc) · 5.37 KB
/
Copy pathDecryptedPDF.java
File metadata and controls
140 lines (120 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
public class DecryptedPDF {
// By default, we use the US-based API service. This is the primary endpoint for global use.
private static final String API_URL = "https://api.pdfrest.com";
// For GDPR compliance and enhanced performance for European users, you can switch to the EU-based
// service by commenting out the URL above and uncommenting the URL below.
// For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
// private static final String API_URL = "https://eu-api.pdfrest.com";
// 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) {
final boolean DELETE_SENSITIVE_FILES = false; // toggle deletion (default: false)
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();
String uploadString = uploadFile(inputFile);
JSONObject uploadJSON = new JSONObject(uploadString);
if (uploadJSON.has("error")) {
System.out.println("Error during upload: " + uploadString);
return;
}
JSONArray fileArray = uploadJSON.getJSONArray("files");
JSONObject fileObject = fileArray.getJSONObject(0);
String uploadedID = fileObject.get("id").toString();
String JSONString =
String.format("{\"id\":\"%s\",\"current_open_password\":\"password\" }", uploadedID);
final RequestBody requestBody =
RequestBody.create(JSONString, MediaType.parse("application/json"));
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/decrypted-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("Processing Result code " + response.code());
if (response.body() != null) {
String respStr = response.body().string();
System.out.println(prettyJson(respStr));
// All files uploaded or generated are automatically deleted based on the
// File Retention Period as shown on https://pdfrest.com/pricing.
// For immediate deletion of files, particularly when sensitive data
// is involved, an explicit delete call can be made to the API.
//
// Deletes all files in the workflow, including outputs. Save all desired files before
// enabling this step.
if (DELETE_SENSITIVE_FILES) {
org.json.JSONObject parsed = new org.json.JSONObject(respStr);
String outputId = parsed.getString("outputId");
String deleteJson = String.format("{ \"ids\":\"%s, %s\" }", uploadedID, outputId);
RequestBody deleteBody =
RequestBody.create(deleteJson, MediaType.parse("application/json"));
Request deleteRequest =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/delete")
.post(deleteBody)
.build();
try (Response deleteResp =
new OkHttpClient()
.newBuilder()
.readTimeout(60, TimeUnit.SECONDS)
.build()
.newCall(deleteRequest)
.execute()) {
if (deleteResp.body() != null) {
System.out.println(prettyJson(deleteResp.body().string()));
}
}
}
}
} 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);
}
// This function is just a copy of the 'Upload.java' file to upload a binary file
private static String uploadFile(File inputFile) {
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
final RequestBody requestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.header("Content-Filename", "File.pdf")
.url(API_URL + "/upload")
.post(requestBody)
.build();
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
Response response = client.newCall(request).execute();
System.out.println("Upload Result code " + response.code());
if (response.body() != null) {
return response.body().string();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return "";
}
}