The pdfRest Convert to PDF/X API Tool is a powerful resource for developers who need to ensure their PDF documents meet the rigorous standards of the PDF/X format, which is a subset of the PDF specification designed for the exchange of print-ready PDF files. This tutorial will guide you through the process of using Java to send an API call to the Convert to PDF/X endpoint provided by pdfRest, enabling you to automate the conversion of standard PDF files to the PDF/X format for reliable printing and publishing.
In a business scenario, for example, a printing company may need to convert a batch of customer PDFs to the PDF/X-1a standard to ensure compatibility with their printing equipment and to maintain color fidelity. By using the Convert to PDF/X API, the company can automate this process, reducing manual effort and the potential for errors, thus streamlining their workflow and improving customer satisfaction.
The following code is an example of how to call the pdfRest Convert to PDF/X API using Java:
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 PDFX {
// 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("output_type", "PDF/X-4")
.addFormDataPart("output", "pdfrest_pdfx")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/pdfx")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).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);
}
}
The source code for this example can be found on GitHub at pdf-rest-api-samples.
The provided Java code makes an API call to the pdfRest Convert to PDF/X endpoint. Let's break down the key parts of the code:
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
This line initializes the Dotenv library, which is used to load environment variables from a .env file. This allows you to keep your API key secure and not hard-coded into the program.
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/pdf"));
This snippet creates a request body for the file that needs to be compressed. The file is treated as 'application/pdf' media type.
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("output_type", "PDF/X-4")
.addFormDataPart("output", "pdfrest_pdfx")
.build();
Here, a multipart request body is built with the PDF file, the desired PDF/X output type, and the output name for the converted file.
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url("https://api.pdfrest.com/compressed-pdf")
.post(requestBody)
.build();
The request is constructed with the API key (retrieved from environment variables), the endpoint URL, and the previously created request body.
Response response = client.newCall(request).execute();
This sends a request to the pdfRest API endpoint.
By following the steps in this tutorial, you have learned how to use Java to make an API call to the pdfRest Convert to PDF/X endpoint. This capability can be integrated into your applications to automate the process of converting PDFs to the PDF/X standard, which is essential for printing and publishing workflows.
To explore and demo all of the pdfRest API Tools, visit the API Lab. For a comprehensive guide to the API, including endpoints, parameters, and examples, refer to the API Reference Guide.
Note: This is an example of a multipart API call. For code samples using JSON payloads, you can find them at pdf-rest-api-samples.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.