How to Apply TDM Reservations to PDF with Java
Why Use TDM Reserve PDF with Java?
The pdfRest TDM Reserve PDF API Tool provides a vital mechanism for developers to protect intellectual property from unauthorized artificial intelligence ingestion. This tutorial demonstrates how to send an API call to the TDM Reserve PDF endpoint using Java. By implementing this request within your Java applications, you can automatically embed machine-readable metadata into your documents to explicitly opt out of automated text and data mining (TDM), ensuring your rights are legally asserted.
Consider a digital publishing platform that hosts premium academic journals or proprietary market research. To prevent commercial web crawlers from scraping these valuable PDF documents to train Large Language Models (LLMs) for free, the platform can automate the TDM Reserve PDF API. This applies a universal "No-AI" policy to all outgoing files, guaranteeing that the content remains protected and compliant with international copyright directives without requiring manual intervention.
TDM Reserve PDF with Java Code Example
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;
public class TDMReservedPDF {
// 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) {
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();
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("policy", "https://example.com/tdm-policy")
.addFormDataPart("output", "pdfrest_tdm_reserved_pdf")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/tdm-reserved-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("TDM metadata 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);
}
}
Source: GitHub
Breaking Down the Code
The code begins by importing necessary libraries such as Dotenv for environment variable management, OkHttp for HTTP requests, and JSONObject for JSON manipulation.
private static final String API_URL = "https://api.pdfrest.com";
This line sets the API endpoint URL. You have the option to switch to an EU-based endpoint for GDPR compliance by uncommenting the alternative URL.
private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf";
This specifies the default path to the PDF file that you will be sending in the API call. You can modify this path or pass it as an argument when running the program.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
Your API key is specified here. It can also be stored in an environment variable named PDFREST_API_KEY.
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("policy", "https://example.com/tdm-policy")
.addFormDataPart("output", "pdfrest_tdm_reserved_pdf")
.build();
This snippet creates a multipart form request body, which includes the PDF file, a policy URL, and an output specification. The 'file' part attaches the PDF file, 'policy' specifies the TDM policy URL, and 'output' defines the desired output format.
Request request = new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/tdm-reserved-pdf")
.post(requestBody)
.build();
This block constructs the HTTP request, setting the API key in the header and specifying the endpoint URL. The request body is attached using the POST method.
Beyond the Tutorial
In this tutorial, you learned how to send an API call to the TDM Reserve PDF endpoint using Java. This process allows you to reserve PDFs for TDM purposes, ensuring compliance with policies and legal requirements. To further explore the capabilities of the pdfRest API Tools, try out the API Lab. For more detailed information, refer to the API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found here.