Use DocsFold PDF generation API with Java

This code makes a POST request to the DocsFold API with the required headers and request body to generate a PDF file from the specified template and overrides.

You need to replace YOUR_API_KEY with your actual API key and YOUR_TEMPLATE_ID with your actual template ID.

This code example includes parsing the JSON response and downloading the PDF file from the specified URL. The PDF file is then saved to disk with the name "invoice.pdf".

Note that you'll need to adjust the file path as needed for the output PDF.

java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DocsFoldExample {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY";
String templateId = "YOUR_TEMPLATE_ID";
String outputFormat = "url";
String requestUrl = "https://app.docsfold.com/api/v1/generate-pdf";
// create request JSON object
String requestBody = "{\n" +
" \"template\": \"" + templateId + "\",\n" +
" \"output_format\": \"url\",\n" +
" \"overrides\": {\n" +
" \"invoice_nr\": \"123\",\n" +
" \"created_date\": \"January 1, 2022\",\n" +
" \"due_date\": \"February 1, 2022\",\n" +
" \"company_name\": \"Acme Corp\",\n" +
" \"company_address\": \"Acme road, 1\",\n" +
" \"company_postal_code\": \"Sunnyville, 1000\",\n" +
" \"client_company_name\": \"Doe Corp\",\n" +
" \"client_name\": \"John Doe\",\n" +
" \"client_email\": \"john@doe.com\",\n" +
" \"payment_method\": \"Check\",\n" +
" \"payment_method_value\": 1000,\n" +
" \"items\": [\n" +
" {\n" +
" \"name\": \"Website design\",\n" +
" \"value\": 300\n" +
" },\n" +
" {\n" +
" \"name\": \"Hosting (3 months)\",\n" +
" \"value\": 75\n" +
" },\n" +
" {\n" +
" \"name\": \"Domain name (1 year)\",\n" +
" \"value\": 10\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
try {
URL url = new URL(requestUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Bearer " + apiKey);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
// send request
byte[] requestBody = requestJson.getBytes();
conn.getOutputStream().write(requestBody);
// read response
int responseCode = conn.getResponseCode();
System.out.println("Response code: " + responseCode + " <-> " + conn.getResponseMessage());
if (responseCode != HttpURLConnection.HTTP_OK) { // success
System.out.println("POST request failed");
} else {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
String responseJson = response.toString();
System.out.println(responseJson);
JSONObject responseObject = new JSONObject(responseJson);
String status = responseObject.getString("status");
if (status.equals("SUCCESS")) {
String pdfUrl = responseObject.getString("pdf_url");
downloadUsingNIO(pdfUrl, "invoice.pdf");
// download PDF file
System.out.println("PDF file saved to disk.");
} else {
String errorMessage = responseObject.getString("message");
System.out.println("Error: " + errorMessage);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
Author Image
About the author

Gustavo Santos

Gustavo is the founder of DocsFold, based in Portugal. He has worked as a software developer for over 15 years. He enjoys exploring new technologies and using them to automate work as much as possible.