How to Convert TIFF to PDF with Java
In the following tutorial, you'll use Java and the Convert to PDF API Tool to convert TIFF to PDF. The Java request submits a TIFF image to the shared conversion endpoint and reads the resulting PDF resource information for downstream document workflows. The example is designed to clarify both the API-specific fields and the surrounding HTTP request used to convert TIFF to PDF.
Why Convert TIFF to PDF with Java?
Document-imaging systems often deliver TIFF files while case-management and collaboration systems expect PDFs. Conversion normalizes incoming scans before OCR, merging, or distribution.
The adapted request uploads a .tif or .tiff file with the image/tiff media type. Convert to PDF returns a new PDF resource and leaves the source resource unchanged.
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 ConvertTIFFToPDF {
// 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.tif";
// 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("image/tiff"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("output", "pdfrest_tiff_to_pdf")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/pdf")
.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: Java sample on GitHub. This version adjusts the general Java example to make Convert TIFF to PDF explicit.
Breaking Down the Code
Load the Java HTTP and JSON dependencies
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;
This client uses OkHttp for transport, dotenv for loading PDFREST_API_KEY, and org.json for structured request or response data.
Choose the input and load credentials
// 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.tif";
// 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 =
The sample configuration uses runtime arguments when supplied and otherwise falls back to the documented input path and environment-based credential.
Build the multipart request body
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("image/tiff"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("output", "pdfrest_tiff_to_pdf")
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
The adapted request uploads a .tif or .tiff file with the image/tiff media type. Convert to PDF returns a new PDF resource and leaves the source resource unchanged. This request body uses the form multipart type required for combining uploaded content with operation parameters.
Send the request to pdfRest
.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
The HTTP request ties the body to its endpoint and applies the required Api-Key header before OkHttp sends it.
Read the API response
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 example output logic uses both status and body to distinguish a successful resource from validation or processing failures.
Beyond the Tutorial
We have now covered the request and response flow for using Java to convert TIFF to PDF. With that foundation, you can use TIFF-to-PDF conversion for scan intake, fax archives, medical records, and legacy imaging migrations.
Before relying on the result in an automated workflow, keep in mind that tIFF inputs may contain one or several pages and can vary widely in resolution and color model. Confirm the resulting page count, order, orientation, and readability, then apply OCR separately when scanned pages also need searchable text.
Continue testing representative documents with the Convert TIFF to PDF operation in API Lab. The Convert to PDF API Tool documentation provides authoritative defaults, accepted fields, and limitations for the Convert TIFF to PDF operation.
Note: The Convert TIFF to PDF example above uses multipart form data; when the input is already stored in pdfRest, compare the multipart call with the JSON payload example.