Hey there, fellow code wranglers! Ready to supercharge your Java app with some serious document handling capabilities? Look no further than the pdfFiller API. This nifty tool lets you upload, create, fill, and download PDF forms with ease. In this guide, we'll walk through integrating this powerhouse into your Java project. Buckle up!
Before we dive in, make sure you've got:
First things first, let's get our project set up:
pom.xml
(if you're using Maven):<dependencies> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.10.0</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> </dependencies>
pdfFiller uses OAuth 2.0 for authentication. Here's a quick implementation:
public class PdfFillerAuth { private static final String TOKEN_URL = "https://api.pdffiller.com/v1/oauth/access_token"; private static final String CLIENT_ID = "your_client_id"; private static final String CLIENT_SECRET = "your_client_secret"; public static String getAccessToken() throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("grant_type", "client_credentials") .add("client_id", CLIENT_ID) .add("client_secret", CLIENT_SECRET) .build(); Request request = new Request.Builder() .url(TOKEN_URL) .post(formBody) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String responseBody = response.body().string(); JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject(); return jsonObject.get("access_token").getAsString(); } } }
Pro tip: Store your access token securely and refresh it when needed!
Let's get that PDF up to pdfFiller:
public static String uploadDocument(String filePath, String accessToken) throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", "document.pdf", RequestBody.create(MediaType.parse("application/pdf"), new File(filePath))) .build(); Request request = new Request.Builder() .url("https://api.pdffiller.com/v1/document") .post(requestBody) .addHeader("Authorization", "Bearer " + accessToken) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String responseBody = response.body().string(); JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject(); return jsonObject.get("id").getAsString(); } }
Now, let's make that PDF fillable:
public static void createFillableForm(String documentId, String accessToken) throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), "{}"); Request request = new Request.Builder() .url("https://api.pdffiller.com/v1/fillable_template/" + documentId) .post(requestBody) .addHeader("Authorization", "Bearer " + accessToken) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); // Form created successfully } }
Time to populate those fields:
public static void fillForm(String documentId, Map<String, String> fieldValues, String accessToken) throws IOException { OkHttpClient client = new OkHttpClient(); Gson gson = new Gson(); String jsonBody = gson.toJson(fieldValues); RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonBody); Request request = new Request.Builder() .url("https://api.pdffiller.com/v1/document/" + documentId + "/fill") .post(requestBody) .addHeader("Authorization", "Bearer " + accessToken) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); // Form filled successfully } }
Finally, let's grab that filled PDF:
public static void downloadDocument(String documentId, String outputPath, String accessToken) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://api.pdffiller.com/v1/document/" + documentId + "/download") .get() .addHeader("Authorization", "Bearer " + accessToken) .build(); try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); try (InputStream inputStream = response.body().byteStream(); OutputStream outputStream = new FileOutputStream(outputPath)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } } }
Don't forget to test! Write unit tests for each method and integration tests for the full workflow. Your future self will thank you.
And there you have it! You've just built a robust pdfFiller API integration in Java. With this power, you can automate document workflows, create dynamic forms, and much more. The possibilities are endless!
Now go forth and conquer those PDFs! Happy coding!