Skip to content

Commit b066939

Browse files
Merge pull request #91 from datalogics-cgreen/redact-complex-flow
Add Redact PDF complex flow samples
2 parents f570271 + 4a7df9f commit b066939

6 files changed

Lines changed: 404 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System.Text;
4+
5+
/*
6+
* This sample demonstrates the workflow from unredacted document to fully
7+
* redacted document. The output file from the preview tool is immediately
8+
* forwarded to the finalization stage. We recommend inspecting the output from
9+
* the preview stage before utilizing this workflow to ensure that content is
10+
* redacted as intended.
11+
*/
12+
13+
var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Your API key here
14+
15+
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
16+
{
17+
18+
// Begin redaction preview
19+
using var previewRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-redacted-text-preview");
20+
21+
previewRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
22+
previewRequest.Headers.Accept.Add(new("application/json"));
23+
var previewMultipartContent = new MultipartFormDataContent();
24+
25+
var byteArray = File.ReadAllBytes("/path/to/file.pdf");
26+
var byteAryContent = new ByteArrayContent(byteArray);
27+
previewMultipartContent.Add(byteAryContent, "file", "file_name.pdf");
28+
var redactionArray = new JArray();
29+
var redaction = new JObject
30+
{
31+
["type"] = "regex",
32+
["value"] = "[Tt]he"
33+
};
34+
redactionArray.Add(redaction);
35+
var byteArrayRedOption = new ByteArrayContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(redactionArray)));
36+
previewMultipartContent.Add(byteArrayRedOption, "redactions");
37+
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
38+
39+
previewRequest.Content = previewMultipartContent;
40+
var pdfResponse = await httpClient.SendAsync(previewRequest);
41+
42+
var pdfResult = await pdfResponse.Content.ReadAsStringAsync();
43+
Console.WriteLine("Redaction preview response received.");
44+
Console.WriteLine(pdfResult);
45+
46+
dynamic responseData = JObject.Parse(pdfResult);
47+
string pdfID = responseData.outputId;
48+
49+
// Apply the previewed redactions
50+
using var finalizeRequest = new HttpRequestMessage(HttpMethod.Post, "pdf-with-redacted-text-applied");
51+
52+
finalizeRequest.Headers.TryAddWithoutValidation("Api-Key", apiKey);
53+
finalizeRequest.Headers.Accept.Add(new("application/json"));
54+
var finalMultipartContent = new MultipartFormDataContent();
55+
56+
57+
var byteArrayIdOption = new ByteArrayContent(Encoding.UTF8.GetBytes(pdfID));
58+
finalMultipartContent.Add(byteArrayIdOption, "id");
59+
60+
finalizeRequest.Content = finalMultipartContent;
61+
var response = await httpClient.SendAsync(finalizeRequest);
62+
63+
var apiResult = await response.Content.ReadAsStringAsync();
64+
65+
Console.WriteLine("Finalized redaction response received.");
66+
Console.WriteLine(apiResult);
67+
68+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.JSONObject;
7+
8+
/*
9+
* This sample demonstrates the workflow from unredacted document to fully
10+
* redacted document. The output file from the preview tool is immediately
11+
* forwarded to the finalization stage. We recommend inspecting the output from
12+
* the preview stage before utilizing this workflow to ensure that content is
13+
* redacted as intended.
14+
*/
15+
16+
public class RedactPreviewAndFinalize {
17+
18+
// Specify the path to your file here, or as the first argument when running the program.
19+
private static final String DEFAULT_FILE_PATH = "/path/to/file";
20+
21+
// Specify your API key here, or in the environment variable PDFREST_API_KEY.
22+
// You can also put the environment variable in a .env file.
23+
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
24+
25+
//
26+
private static final String REDACTION_OBJECTS = "[{\"type\":\"regex\",\"value\":\"[Tt]he\"}]";
27+
28+
public static void main(String[] args) {
29+
File inputFile;
30+
if (args.length > 0) {
31+
inputFile = new File(args[0]);
32+
} else {
33+
inputFile = new File(DEFAULT_FILE_PATH);
34+
}
35+
36+
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
37+
38+
final RequestBody previewInputFileRequestBody =
39+
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
40+
RequestBody previewRequestBody =
41+
new MultipartBody.Builder()
42+
.setType(MultipartBody.FORM)
43+
.addFormDataPart("file", inputFile.getName(), previewInputFileRequestBody)
44+
.addFormDataPart("redactions", REDACTION_OBJECTS)
45+
.addFormDataPart("output", "pdfrest_preview")
46+
.build();
47+
Request previewRequest =
48+
new Request.Builder()
49+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
50+
.url("https://api.pdfrest.com/pdf-with-redacted-text-preview")
51+
.post(previewRequestBody)
52+
.build();
53+
try {
54+
OkHttpClient pdfClient =
55+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
56+
57+
Response previewResponse = pdfClient.newCall(previewRequest).execute();
58+
59+
System.out.println("Result code from preview call: " + previewResponse.code());
60+
if (previewResponse.body() != null) {
61+
String pdfResponseString = previewResponse.body().string();
62+
63+
JSONObject pdfJSON = new JSONObject(pdfResponseString);
64+
if (pdfJSON.has("error")) {
65+
System.out.println("Error during pdf call: " + previewResponse.body().string());
66+
return;
67+
}
68+
69+
String pdfID = pdfJSON.get("outputId").toString();
70+
71+
RequestBody appliedRequestBody =
72+
new MultipartBody.Builder()
73+
.setType(MultipartBody.FORM)
74+
.addFormDataPart("id", pdfID)
75+
.addFormDataPart("output", "pdfrest_applied")
76+
.build();
77+
Request appliedRequest =
78+
new Request.Builder()
79+
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
80+
.url("https://api.pdfrest.com/pdf-with-redacted-text-applied")
81+
.post(appliedRequestBody)
82+
.build();
83+
try {
84+
OkHttpClient appliedClient =
85+
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
86+
Response appliedResponse = appliedClient.newCall(appliedRequest).execute();
87+
System.out.println("Result code from applied call: " + appliedResponse.code());
88+
if (appliedResponse.body() != null) {
89+
System.out.println(prettyJson(appliedResponse.body().string()));
90+
}
91+
} catch (IOException e) {
92+
throw new RuntimeException(e);
93+
}
94+
}
95+
} catch (IOException e) {
96+
throw new RuntimeException(e);
97+
}
98+
}
99+
100+
private static String prettyJson(String json) {
101+
// https://stackoverflow.com/a/9583835/11996393
102+
return new JSONObject(json).toString(4);
103+
}
104+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var axios = require("axios");
2+
var FormData = require("form-data");
3+
var fs = require("fs");
4+
5+
/*
6+
* This sample demonstrates the workflow from unredacted document to fully
7+
* redacted document. The output file from the preview tool is immediately
8+
* forwarded to the finalization stage. We recommend inspecting the output from
9+
* the preview stage before utilizing this workflow to ensure that content is
10+
* redacted as intended.
11+
*/
12+
13+
var apiKey = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // Replace with your API key
14+
15+
var previewData = new FormData();
16+
previewData.append("file", fs.createReadStream("/path/to/file.pdf"));
17+
18+
var redaction_option_array = [];
19+
var redaction_options = {
20+
"type": "regex",
21+
"value": "[Tt]he",
22+
};
23+
redaction_option_array.push(redaction_options);
24+
previewData.append('redactions', JSON.stringify(redaction_option_array));
25+
26+
var previewConfig = {
27+
method: "post",
28+
maxBodyLength: Infinity,
29+
url: "https://api.pdfrest.com/pdf-with-redacted-text-preview",
30+
headers: {
31+
"Api-Key": apiKey,
32+
...previewData.getHeaders(),
33+
},
34+
data: previewData,
35+
};
36+
37+
axios(previewConfig)
38+
.then(function (response) {
39+
var pdfID = response.data.outputId;
40+
41+
var appliedData = new FormData();
42+
appliedData.append("id", pdfID);
43+
appliedData.append("output", "pdfrest_applied_redaction");
44+
45+
var appliedConfig = {
46+
method: "post",
47+
maxBodyLength: Infinity,
48+
url: "https://api.pdfrest.com/pdf-with-redacted-text-applied",
49+
headers: {
50+
"Api-Key": apiKey,
51+
...appliedData.getHeaders(),
52+
},
53+
data: appliedData,
54+
};
55+
56+
axios(appliedConfig)
57+
.then(function (response) {
58+
console.log(JSON.stringify(response.data)); // If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
59+
})
60+
.catch(function (error) {
61+
console.log(error);
62+
});
63+
})
64+
.catch(function (error) {
65+
console.log(error);
66+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Utils;
8+
9+
/*
10+
* This sample demonstrates the workflow from unredacted document to fully
11+
* redacted document. The output file from the preview tool is immediately
12+
* forwarded to the finalization stage. We recommend inspecting the output from
13+
* the preview stage before utilizing this workflow to ensure that content is
14+
* redacted as intended.
15+
*/
16+
17+
18+
$client = new Client();
19+
20+
$headers = [
21+
'Api-Key' => 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
22+
];
23+
24+
$redactionPreviewOptions = [
25+
'multipart' => [
26+
[
27+
'name' => 'file',
28+
'contents' => Utils::tryFopen('/path/to/file.pdf', 'r'),
29+
'filename' => 'file.pdf',
30+
'headers' => [
31+
'Content-Type' => '<Content-type header>'
32+
]
33+
],
34+
[
35+
'name' => 'redactions',
36+
'contents' => '[{"type":"regex","value":"[Tt]he"}]'
37+
]
38+
]
39+
];
40+
41+
$redactionPreviewRequest = new Request('POST', 'https://api.pdfrest.com/pdf-with-redacted-text-preview', $headers);
42+
43+
$redactionPreviewResponse = $client->sendAsync($redactionPreviewRequest, $redactionPreviewOptions)->wait();
44+
45+
$redactionPreviewedFileID = json_decode($redactionPreviewResponse->getBody())->{'outputId'};
46+
47+
48+
49+
$redactionAppliedOptions = [
50+
'multipart' => [
51+
[
52+
'name' => 'id',
53+
'contents' => $redactionPreviewedFileID
54+
],
55+
[
56+
'name' => 'output',
57+
'contents' => 'pdfrest_redactionApplied'
58+
]
59+
]
60+
];
61+
62+
$redactionAppliedRequest = new Request('POST', 'https://api.pdfrest.com/pdf-with-redacted-text-applied', $headers);
63+
64+
$redactionAppliedResponse = $client->sendAsync($redactionAppliedRequest, $redactionAppliedOptions)->wait();
65+
66+
echo $redactionAppliedResponse->getBody();
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from requests_toolbelt import MultipartEncoder
2+
import requests
3+
import json
4+
5+
# This sample demonstrates the workflow from unredacted document to fully
6+
# redacted document. The output file from the preview tool is immediately
7+
# forwarded to the finalization stage. We recommend inspecting the output from
8+
# the preview stage before utilizing this workflow to ensure that content is
9+
# redacted as intended.
10+
11+
api_key = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
12+
13+
pdf_endpoint_url = 'https://api.pdfrest.com/pdf-with-redacted-text-preview'
14+
15+
redaction_options = [
16+
{
17+
"type": "regex",
18+
"value": "[Tt]he",
19+
}
20+
]
21+
22+
mp_encoder_preview = MultipartEncoder(
23+
fields={
24+
'file': ('file.pdf', open('/path/to/file.pdf', 'rb'), 'application/pdf'),
25+
'redactions': json.dumps(redaction_options),
26+
'output' : 'example_out'
27+
}
28+
)
29+
30+
pdf_headers = {
31+
'Accept': 'application/json',
32+
'Content-Type': mp_encoder_preview.content_type,
33+
'Api-Key': api_key
34+
}
35+
36+
print("Sending POST request to redaction preview endpoint...")
37+
response = requests.post(pdf_endpoint_url, data=mp_encoder_preview, headers=pdf_headers)
38+
39+
print("Response status code: " + str(response.status_code))
40+
41+
if response.ok:
42+
response_json = response.json()
43+
pdf_id = response_json["outputId"]
44+
45+
46+
applied_endpoint_url = 'https://api.pdfrest.com/pdf-with-redacted-text-applied'
47+
48+
mp_encoder_applied = MultipartEncoder(
49+
fields={
50+
'id': pdf_id,
51+
'output' : 'redacted_final',
52+
}
53+
)
54+
55+
headers = {
56+
'Accept': 'application/json',
57+
'Content-Type': mp_encoder_applied.content_type,
58+
'Api-Key': api_key
59+
}
60+
61+
print("Sending POST request to applied redaction endpoint...")
62+
response = requests.post(applied_endpoint_url, data=mp_encoder_applied, headers=headers)
63+
64+
print("Response status code: " + str(response.status_code))
65+
66+
if response.ok:
67+
response_json = response.json()
68+
print(json.dumps(response_json, indent = 2))
69+
else:
70+
print(response.text)
71+
else:
72+
print(response.text)
73+
74+
# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

0 commit comments

Comments
 (0)