How to Convert Microsoft Excel to PDF with Java
This step-by-step tutorial explains how a Java application can convert Microsoft Excel to PDF through the Convert to PDF API Tool. The sample sends an XLSX workbook through the common /pdf endpoint and captures the resulting fixed-layout document resource for downstream review. After reviewing the example, you'll be ready to convert Microsoft Excel to PDF with your own input.
Why Convert Microsoft Excel to PDF with Java?
A finance service may distribute a monthly workbook to reviewers who should see a stable report rather than editable formulas and worksheets. PDF conversion preserves a consistent presentation for approval and sharing.
The adapted Java request uploads an .xlsx file with the Office Open XML spreadsheet media type. Convert to PDF identifies the workbook from its multipart filename and processes it through the shared /pdf endpoint.
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 ConvertExcelToPDF {
// 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.xlsx";
// 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/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("output", "pdfrest_excel_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. The code is adapted from the repository's general example to demonstrate Convert Microsoft Excel to PDF specifically.
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;
The sample 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.xlsx";
// 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 configuration block separates file selection from authentication so either value can change without rewriting the request.
Build the multipart request body
final RequestBody inputFileRequestBody =
RequestBody.create(inputFile, MediaType.parse("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
RequestBody requestBody =
new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", inputFile.getName(), inputFileRequestBody)
.addFormDataPart("output", "pdfrest_excel_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 Java request uploads an .xlsx file with the Office Open XML spreadsheet media type. Convert to PDF identifies the workbook from its multipart filename and processes it through the shared /pdf endpoint. The form-data setup 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 client 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 response check uses both status and body to distinguish a successful resource from validation or processing failures.
Beyond the Tutorial
The completed example gives you a practical Java starting point for learning how to convert Microsoft Excel to PDF. With that foundation, this workflow suits financial reports, inventory exports, schedules, and operational spreadsheets that need a dependable review copy.
As you apply the pattern to real documents, keep in mind that workbook print areas, page breaks, fonts, and wide worksheets all influence the fixed PDF presentation. Test realistic workbooks rather than a single simple sheet, and review pagination before distributing the result as an approved report or record.
Before integrating this workflow, test representative documents with the Convert Microsoft Excel to PDF operation in API Lab. Then use the Convert to PDF API Tool documentation to review every available parameter and response field.
Note: The Convert Microsoft Excel to PDF example above uses multipart form data; to reuse a resource ID, compare the multipart call with the JSON payload example.