forked from pdfrest/pdfrest-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFA3bWithAttachment.java
More file actions
120 lines (103 loc) · 5.1 KB
/
Copy pathPDFA3bWithAttachment.java
File metadata and controls
120 lines (103 loc) · 5.1 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
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 {
// 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 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(API_URL + "/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(API_URL + "/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);
}
}