Getting Started
The following section will walk you through how to best make use of all the functionality our Image to PDF API provides.
Converting a Single Image to PDF
Let’s start with the basics. The Image to PDF API allows you to convert any supported image file to a PDF.
The easiest way to use this is to convert a single image to a PDF. Place a
page1.jpg
file in the same folder as your code. You can use any image or use our
provided
sample page.
Run the code, and you’ll get a result.pdf
with your image converted to a PDF.
Code to convert an image to PDF:
package com.example.pspdfkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PspdfkitApiExample {
public static void main(final String[] args) throws IOException {
final RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"page1.jpg",
"/path/to/page1.jpg",
RequestBody.create(
MediaType.parse("image/jpeg"),
new File("/path/to/page1.jpg")
)
)
.addFormDataPart(
"instructions",
new JSONObject()
.put("parts", new JSONArray()
.put(new JSONObject()
.put("file", "page1.jpg")
)
).toString()
)
.build();
final Request request = new Request.Builder()
.url("https://api.nutrient.io/build")
.method("POST", body)
.addHeader("Authorization", "Bearer your_api_key_here")
.build();
final OkHttpClient client = new OkHttpClient()
.newBuilder()
.build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Files.copy(
response.body().byteStream(),
FileSystems.getDefault().getPath("result.pdf"),
StandardCopyOption.REPLACE_EXISTING
);
} else {
// Handle the error
throw new IOException(response.body().string());
}
}
}
Combining Multiple Images into a PDF
While converting a single image to a PDF is quite useful, often you’ll have a folder of images — for example, of a scanned document — that you want to combine into a single PDF.
Pass in multiple images — one for each page in your request — and Nutrient DWS API will merge all of them into a single PDF.
Add more files in the same folder as your code and run the updated code. You can duplicate and rename your existing file, or you can add some other images.
Code to convert multiple images to PDF:
package com.example.pspdfkit;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.json.JSONArray;
import org.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public final class PspdfkitApiExample {
public static void main(final String[] args) throws IOException {
final RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"page1.jpg",
"/path/to/page1.jpg",
RequestBody.create(
MediaType.parse("image/jpeg"),
new File("/path/to/page1.jpg")
)
)
.addFormDataPart(
"page2.jpg",
"/path/to/page2.jpg",
RequestBody.create(
MediaType.parse("image/jpeg"),
new File("/path/to/page2.jpg")
)
)
.addFormDataPart(
"page3.jpg",
"/path/to/page3.jpg",
RequestBody.create(
MediaType.parse("image/jpeg"),
new File("/path/to/page3.jpg")
)
)
.addFormDataPart(
"page4.jpg",
"/path/to/page4.jpg",
RequestBody.create(
MediaType.parse("image/jpeg"),
new File("/path/to/page4.jpg")
)
)
.addFormDataPart(
"instructions",
new JSONObject()
.put("parts", new JSONArray()
.put(new JSONObject()
.put("file", "page1.jpg")
)
.put(new JSONObject()
.put("file", "page2.jpg")
)
.put(new JSONObject()
.put("file", "page3.jpg")
)
.put(new JSONObject()
.put("file", "page4.jpg")
)
).toString()
)
.build();
final Request request = new Request.Builder()
.url("https://api.nutrient.io/build")
.method("POST", body)
.addHeader("Authorization", "Bearer your_api_key_here")
.build();
final OkHttpClient client = new OkHttpClient()
.newBuilder()
.build();
final Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
Files.copy(
response.body().byteStream(),
FileSystems.getDefault().getPath("result.pdf"),
StandardCopyOption.REPLACE_EXISTING
);
} else {
// Handle the error
throw new IOException(response.body().string());
}
}
}