|
| 1 | +import io.github.cdimascio.dotenv.Dotenv; |
| 2 | +import java.io.File; |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import okhttp3.*; |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +public class SignedPDF { |
| 10 | + |
| 11 | + // Specify your API key here, or in the environment variable PDFREST_API_KEY. |
| 12 | + // You can also put the environment variable in a .env file. |
| 13 | + private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; |
| 14 | + |
| 15 | + public static void main(String[] args) { |
| 16 | + File inputFile, credentialFile, passphraseFile, logoFile; |
| 17 | + if (args.length > 3) { |
| 18 | + inputFile = new File(args[0]); |
| 19 | + credentialFile = new File(args[1]); |
| 20 | + passphraseFile = new File(args[2]); |
| 21 | + logoFile = new File(args[3]); |
| 22 | + } else { |
| 23 | + inputFile = new File("/path/to/input.pdf"); |
| 24 | + credentialFile = new File("/path/to/credentials.pfx"); |
| 25 | + passphraseFile = new File("/path/to/passphrase.txt"); |
| 26 | + logoFile = new File("/path/to/logo.png"); |
| 27 | + } |
| 28 | + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); |
| 29 | + |
| 30 | + String inputId = uploadFile(inputFile); |
| 31 | + String credentialId = uploadFile(credentialFile); |
| 32 | + String passphraseId = uploadFile(passphraseFile); |
| 33 | + String logoId = uploadFile(logoFile); |
| 34 | + |
| 35 | + JSONObject bottomLeft = new JSONObject(); |
| 36 | + bottomLeft.put("x", "0"); |
| 37 | + bottomLeft.put("y", "0"); |
| 38 | + |
| 39 | + JSONObject topRight = new JSONObject(); |
| 40 | + topRight.put("x", "216"); |
| 41 | + topRight.put("y", "72"); |
| 42 | + |
| 43 | + JSONObject location = new JSONObject(); |
| 44 | + location.put("bottom_left", bottomLeft); |
| 45 | + location.put("top_right", topRight); |
| 46 | + location.put("page", "1"); |
| 47 | + |
| 48 | + JSONObject display = new JSONObject(); |
| 49 | + display.put("include_distinguished_name", "true"); |
| 50 | + display.put("include_datetime", "true"); |
| 51 | + display.put("contact", "My contact information"); |
| 52 | + display.put("location", "My signing location"); |
| 53 | + display.put("name", "John Doe"); |
| 54 | + display.put("reason", "My reason for signing"); |
| 55 | + |
| 56 | + JSONObject signatureConfig = new JSONObject(); |
| 57 | + signatureConfig.put("type", "new"); |
| 58 | + signatureConfig.put("name", "esignature"); |
| 59 | + signatureConfig.put("logo_opacity", "0.5"); |
| 60 | + signatureConfig.put("location", location); |
| 61 | + signatureConfig.put("display", display); |
| 62 | + |
| 63 | + JSONObject requestJson = new JSONObject(); |
| 64 | + requestJson.put("id", inputId); |
| 65 | + requestJson.put("pfx_credential_id", credentialId); |
| 66 | + requestJson.put("pfx_passphrase_id", passphraseId); |
| 67 | + requestJson.put("logo_id", logoId); |
| 68 | + requestJson.put("signature_configuration", signatureConfig.toString()); |
| 69 | + |
| 70 | + final RequestBody requestBody = |
| 71 | + RequestBody.create(requestJson.toString(), MediaType.parse("application/json")); |
| 72 | + |
| 73 | + Request request = |
| 74 | + new Request.Builder() |
| 75 | + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) |
| 76 | + .url("https://api.pdfrest.com/signed-pdf") |
| 77 | + .post(requestBody) |
| 78 | + .build(); |
| 79 | + try { |
| 80 | + OkHttpClient client = |
| 81 | + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); |
| 82 | + |
| 83 | + Response response = client.newCall(request).execute(); |
| 84 | + System.out.println("Processing Result code " + response.code()); |
| 85 | + if (response.body() != null) { |
| 86 | + System.out.println(prettyJson(response.body().string())); |
| 87 | + } |
| 88 | + } catch (IOException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static String prettyJson(String json) { |
| 94 | + // https://stackoverflow.com/a/9583835/11996393 |
| 95 | + return new JSONObject(json).toString(4); |
| 96 | + } |
| 97 | + |
| 98 | + private static String uploadFile(File inputFile) { |
| 99 | + |
| 100 | + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); |
| 101 | + |
| 102 | + final RequestBody requestBody = |
| 103 | + RequestBody.create(inputFile, MediaType.parse("application/pdf")); |
| 104 | + |
| 105 | + Request request = |
| 106 | + new Request.Builder() |
| 107 | + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) |
| 108 | + .header("Content-Filename", inputFile.getName()) |
| 109 | + .url("https://api.pdfrest.com/upload") |
| 110 | + .post(requestBody) |
| 111 | + .build(); |
| 112 | + try { |
| 113 | + OkHttpClient client = new OkHttpClient().newBuilder().build(); |
| 114 | + Response response = client.newCall(request).execute(); |
| 115 | + System.out.println("Upload Result code " + response.code()); |
| 116 | + if (response.body() != null) { |
| 117 | + String inputUploadString = response.body().string(); |
| 118 | + JSONObject inputUploadJSON = new JSONObject(inputUploadString); |
| 119 | + if (inputUploadJSON.has("error")) { |
| 120 | + System.out.println("Error during upload: " + inputUploadString); |
| 121 | + return ""; |
| 122 | + } |
| 123 | + JSONArray fileArray = inputUploadJSON.getJSONArray("files"); |
| 124 | + |
| 125 | + JSONObject fileObject = fileArray.getJSONObject(0); |
| 126 | + |
| 127 | + return fileObject.get("id").toString(); |
| 128 | + } |
| 129 | + } catch (IOException e) { |
| 130 | + throw new RuntimeException(e); |
| 131 | + } |
| 132 | + return ""; |
| 133 | + } |
| 134 | +} |
0 commit comments