How to Create a Blank PDF with Java
Why Create Blank PDF with Java?
The pdfRest Create Blank PDF API Tool allows developers to programmatically generate blank PDF files with specific attributes such as page size, page count, and orientation. This tutorial will guide you through the process of sending an API call to create a blank PDF using Java, demonstrating how to integrate this capability into your Java applications.
Imagine a scenario where a user needs to generate a series of blank PDFs for a document management system. These PDFs might serve as placeholders or templates for future content. By using the Create Blank PDF API, users can automate this task, saving time and reducing the potential for human error in manual PDF creation.
Create Blank PDF with Java Code Example
import io.github.cdimascio.dotenv.Dotenv;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.json.JSONObject;
public class BlankPDF {
// 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 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) {
final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load();
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addFormDataPart("page_size", "letter");
builder.addFormDataPart("page_count", "3");
builder.addFormDataPart("page_orientation", "portrait");
RequestBody requestBody = builder.build();
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/blank-pdf")
.post(requestBody)
.build();
try {
OkHttpClient client =
new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build();
Response response = client.newCall(request).execute();
System.out.println("blank-pdf 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) {
return new JSONObject(json).toString(4);
}
}
Source: GitHub Repository
Breaking Down the Code
The code begins by importing necessary libraries, including OkHttp for HTTP requests and Dotenv for managing environment variables. The API_URL is set to the US-based endpoint, but there's an option to switch to the EU-based endpoint for GDPR compliance.
private static final String API_URL = "https://api.pdfrest.com";
Next, the API key is retrieved from environment variables or set to a default value. This key is essential for authenticating API requests.
private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
The MultipartBody.Builder is used to construct the request body, specifying the page size, page count, and orientation for the blank PDF.
builder.addFormDataPart("page_size", "letter");
builder.addFormDataPart("page_count", "3");
builder.addFormDataPart("page_orientation", "portrait");
A Request object is then created, including the API key in the header and setting the URL to the blank PDF endpoint. The request is executed using an OkHttpClient, and the response is printed.
Request request =
new Request.Builder()
.header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY))
.url(API_URL + "/blank-pdf")
.post(requestBody)
.build();
Beyond the Tutorial
In this tutorial, you've learned how to create a blank PDF using the pdfRest API with Java. This example demonstrates how to automate PDF creation, which can be a valuable tool in various applications. To further explore the capabilities of pdfRest, try out all the API tools in the API Lab. For more detailed information, refer to the API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub Repository.