The pdfRest Convert PDF Colors API Tool is a powerful utility that allows developers to programmatically change the color profile of a PDF document. This tutorial will guide you through the process of sending an API call to convert a PDF from RGB to CMYK using Java. By following along, you'll learn how to integrate the pdfRest API into your Java applications, enabling you to automate the conversion of PDF color profiles with ease.
Imagine you're a graphic designer working on a project that requires printing high-quality images. You've created your design in RGB color mode, which is ideal for digital screens. However, to ensure accurate color reproduction on print, you need to convert the document to CMYK color mode. By using the Convert PDF Colors API, you can automate this conversion process, saving time and ensuring that your design is printed with the desired colors.
import io.github.cdimascio.dotenv.Dotenv; import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import okhttp3.MediaType; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import org.json.JSONObject; public class PDFWithConvertedColors { // 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"; private static final String color_profile = "acrobat9-cmyk"; 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("color_profile", color_profile) .addFormDataPart("output", "pdfrest_pdf_with_converted_colors") .build(); Request request = new Request.Builder() .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) .url("https://api.pdfrest.com/pdf-with-converted-colors") .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); } }
Source: GitHub Repository
The code begins by importing necessary libraries, including OkHttp for HTTP requests and Dotenv for environment variable management. The DEFAULT_FILE_PATH
and DEFAULT_API_KEY
are placeholders for your PDF file path and API key, respectively.
File inputFile; if (args.length > 0) { inputFile = new File(args[0]); } else { inputFile = new File(DEFAULT_FILE_PATH); }
This snippet determines the PDF file to be processed. If a file path is provided as a command-line argument, it uses that; otherwise, it defaults to the specified DEFAULT_FILE_PATH
.
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
The Dotenv
library is used to load environment variables from a .env
file, allowing for secure storage of sensitive data like API keys.
RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", inputFile.getName(), inputFileRequestBody) .addFormDataPart("color_profile", color_profile) .addFormDataPart("output", "pdfrest_pdf_with_converted_colors") .build();
Here, a MultipartBody
is constructed to include the PDF file and additional form data. The color_profile
is set to "srgb", and the output is specified as "pdfrest_pdf_with_converted_colors".
Request request = new Request.Builder() .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) .url("https://api.pdfrest.com/pdf-with-converted-colors") .post(requestBody) .build();
This snippet constructs the HTTP request, setting the API key in the header and specifying the endpoint URL. The request method is POST, and the previously built requestBody
is attached.
In this tutorial, you learned how to use Java to call the pdfRest Convert PDF Colors API, enabling you to automate the conversion of PDF color profiles. This can be particularly useful for ensuring consistent document quality across various platforms.
To explore more functionalities, try out all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.
Note: This example demonstrates a multipart API call. For code samples using JSON payloads, please visit this GitHub repository.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.