Skip to content

Commit

Permalink
Add /pdf-with-added-attachment Java sample
Browse files Browse the repository at this point in the history
  • Loading branch information
datalogics-cgreen committed Oct 31, 2023
1 parent a44c8b9 commit cbbc094
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Java/Single Calls/PdfWithAddedAttachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import io.github.cdimascio.dotenv.Dotenv;
import java.io.File;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONObject;

public class PdfWithAddedAttachment {

// 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 inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
final RequestBody attachmentFileRequestBody =
RequestBody.create(attachmentFile, MediaType.parse("application/xml"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("file_to_attach", attachmentFile.getName(), attachmentFileRequestBody)
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdf-with-added-attachment")
.post(requestBody)
.build();
try {
OkHttpClient client = new OkHttpClient().newBuilder().build();
Response response = client.newCall(request).execute();
System.out.println("Result code " + response.code());
if (response.body() != null) {
System.out.println(prettyJson(response.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);
}
}

0 comments on commit cbbc094

Please sign in to comment.