How to Merge Different File Formats Together as a PDF with Java

Convert different source formats and merge the resulting PDFs in one Java workflow.
Share this page

By the end of this tutorial, you'll know how to merge different file formats together as a PDF with Java and the Merge PDFs API Tool. The multi-step code converts each non-PDF source, collects the resulting resource IDs, and sends them to Merge PDFs in the intended order. We'll explain the important request pieces so you can use the sample as the foundation for an integration that can merge different file formats together as a PDF.

Why Merge Different File Formats Together as a PDF with Java?

A project package may combine a diagram image, presentation, and other source documents. Converting each input and passing their resource IDs into Merge PDFs produces one ordered deliverable.

The Complex Flow example calls /pdf for each non-PDF input, reads each outputId, then supplies those IDs and page ranges to /merged-pdf.

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;

/* In this sample, we will show how to merge different file types together as
* discussed in https://pdfrest.com/solutions/merge-multiple-types-of-files-together/.
First, we will upload an image file to the /pdf route and capture the output ID.
* Next, we will upload a PowerPoint file to the /pdf route and capture its output
* ID. Finally, we will pass both IDs to the /merged-pdf route to combine both inputs
* into a single PDF.
*
* Note that there is nothing special about an image and a PowerPoint file, and
* this sample could be easily used to convert and combine any two file types
* that the /pdf route takes as inputs.
*/

public class MergeDifferentFileTypes {

  // 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 first file here, or as the first argument when running the program.
  private static final String DEFAULT_FIRST_FILE_PATH = "/path/to/file.png";

  // Specify the path to your second file here, or as the second argument when running the
  // program.
  private static final String DEFAULT_SECOND_FILE_PATH = "/path/to/file.ppt";

  // 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 firstFile, secondFile;
    if (args.length > 1) {
      firstFile = new File(args[0]);
      secondFile = new File(args[1]);
    } else {
      firstFile = new File(DEFAULT_FIRST_FILE_PATH);
      secondFile = new File(DEFAULT_SECOND_FILE_PATH);
    }

    final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();

    final RequestBody firstFileInputFileRequestBody =
        RequestBody.create(firstFile, MediaType.parse("application/png"));
    RequestBody firstFileRequestBody =
        new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", firstFile.getName(), firstFileInputFileRequestBody)
            .addFormDataPart("output", "pdfrest_pdf")
            .build();
    Request firstFileRequest =
        new Request.Builder()
            .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
            .url(API_URL + "/pdf")
            .post(firstFileRequestBody)
            .build();
    try {
      OkHttpClient firstFileClient =
          new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

      Response firstFileResponse = firstFileClient.newCall(firstFileRequest).execute();

      System.out.println("Result code from first PDF call: " + firstFileResponse.code());
      if (firstFileResponse.body() != null) {
        String firstFileResponseString = firstFileResponse.body().string();

        JSONObject firstFileJSON = new JSONObject(firstFileResponseString);
        if (firstFileJSON.has("error")) {
          System.out.println("Error during first PDF call: " + firstFileResponse.body().string());
          return;
        }

        String firstFileID = firstFileJSON.get("outputId").toString();

        final RequestBody secondFileInputFileRequestBody =
            RequestBody.create(secondFile, MediaType.parse("application/powerpoint"));
        RequestBody secondFileRequestBody =
            new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", secondFile.getName(), secondFileInputFileRequestBody)
                .addFormDataPart("output", "pdfrest_pdf")
                .build();
        Request secondFileRequest =
            new Request.Builder()
                .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
                .url(API_URL + "/pdf")
                .post(secondFileRequestBody)
                .build();
        try {
          OkHttpClient secondFileClient =
              new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

          Response secondFileResponse = secondFileClient.newCall(secondFileRequest).execute();

          System.out.println("Result code from second PDF call: " + secondFileResponse.code());
          if (secondFileResponse.body() != null) {
            String secondFileResponseString = secondFileResponse.body().string();

            JSONObject secondFileJSON = new JSONObject(secondFileResponseString);
            if (secondFileJSON.has("error")) {
              System.out.println(
                  "Error during second PDF call: " + secondFileResponse.body().string());
              return;
            }

            String secondFileID = secondFileJSON.get("outputId").toString();

            RequestBody mergeRequestBody =
                new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("id[]", firstFileID)
                    .addFormDataPart("id[]", secondFileID)
                    .addFormDataPart("type[]", "id")
                    .addFormDataPart("type[]", "id")
                    .addFormDataPart("pages[]", "1-last")
                    .addFormDataPart("pages[]", "1-last")
                    .addFormDataPart("output", "pdfrest_merged")
                    .build();
            Request mergeRequest =
                new Request.Builder()
                    .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
                    .url(API_URL + "/merged-pdf")
                    .post(mergeRequestBody)
                    .build();
            try {
              OkHttpClient mergeClient =
                  new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
              Response mergeResponse = mergeClient.newCall(mergeRequest).execute();
              System.out.println("Result code from merge call: " + mergeResponse.code());
              if (mergeResponse.body() != null) {
                System.out.println(prettyJson(mergeResponse.body().string()));
              }
            } catch (IOException e) {
              throw new RuntimeException(e);
            }
          }
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    } 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.

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 supporting code 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 first file here, or as the first argument when running the program.
  private static final String DEFAULT_FIRST_FILE_PATH = "/path/to/file.png";

  // Specify the path to your second file here, or as the second argument when running the
  // program.
  private static final String DEFAULT_SECOND_FILE_PATH = "/path/to/file.ppt";

  // 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 firstFile, secondFile;
    if (args.length > 1) {
      firstFile = new File(args[0]);
      secondFile = new File(args[1]);
    } else {
      firstFile = new File(DEFAULT_FIRST_FILE_PATH);

The complex-flow setup accepts several source paths, then loads one API key that is reused across every conversion and merge request.

Build the multipart request body

final RequestBody firstFileInputFileRequestBody =
        RequestBody.create(firstFile, MediaType.parse("application/png"));
    RequestBody firstFileRequestBody =
        new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", firstFile.getName(), firstFileInputFileRequestBody)
            .addFormDataPart("output", "pdfrest_pdf")
            .build();
    Request firstFileRequest =
        new Request.Builder()
            .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
            .url(API_URL + "/pdf")
            .post(firstFileRequestBody)
            .build();
    try {
      OkHttpClient firstFileClient =
          new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

The Complex Flow example calls /pdf for each non-PDF input, reads each outputId, then supplies those IDs and page ranges to /merged-pdf. The body construction uses the form multipart type required for combining uploaded content with operation parameters.

Send the request to pdfRest

.build();
    Request firstFileRequest =
        new Request.Builder()
            .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
            .url(API_URL + "/pdf")
            .post(firstFileRequestBody)
            .build();
    try {
      OkHttpClient firstFileClient =
          new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

This builder chain ties the body to its endpoint and applies the required Api-Key header before OkHttp sends it.

Pass resources between workflow stages

if (firstFileJSON.has("error")) {
          System.out.println("Error during first PDF call: " + firstFileResponse.body().string());
          return;
        }

        String firstFileID = firstFileJSON.get("outputId").toString();

        final RequestBody secondFileInputFileRequestBody =
            RequestBody.create(secondFile, MediaType.parse("application/powerpoint"));
        RequestBody secondFileRequestBody =
            new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", secondFile.getName(), secondFileInputFileRequestBody)
                .addFormDataPart("output", "pdfrest_pdf")
                .build();
        Request secondFileRequest =

In the Merge Different Formats Together as a PDF workflow, Java reads the generated resource ID from one response and supplies it to the next endpoint, avoiding a download and another upload of the intermediate file.

Read the API response

OkHttpClient firstFileClient =
          new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();

      Response firstFileResponse = firstFileClient.newCall(firstFileRequest).execute();

      System.out.println("Result code from first PDF call: " + firstFileResponse.code());
      if (firstFileResponse.body() != null) {
        String firstFileResponseString = firstFileResponse.body().string();

        JSONObject firstFileJSON = new JSONObject(firstFileResponseString);
        if (firstFileJSON.has("error")) {
          System.out.println("Error during first PDF call: " + firstFileResponse.body().string());
          return;
        }

        String firstFileID = firstFileJSON.get("outputId").toString();

This completion path uses both status and body to distinguish a successful resource from validation or processing failures.

Beyond the Tutorial

By working through this example, you now have a Java pattern you can use to merge different file formats together as a PDF. With that foundation, you can extend the workflow to assemble submissions, onboarding packets, reports, and mixed-format evidence collections.

When this becomes part of an application, make source order and page ranges explicit rather than relying on filesystem or upload order. Track intermediate IDs so failed conversions can be diagnosed and successful resources can be reused or deleted according to the application’s retention policy.

API Lab lets you try the Merge Different Formats Together as a PDF operation without first building an application. When you are ready to implement it, see the Merge PDFs API Tool documentation for the complete API contract.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.