Skip to main content

Quick Start for Java

Java

Integrate PDFBolt's REST API in Java to generate PDFs from URLs, HTML, or templates. The examples below cover all three conversion modes (Direct, Sync, Async).

1. Get Your API Key

Find your API key on the API Keys page in your Dashboard. If you don't have an account, sign up – the free plan includes 100 document conversions per month.

2. Make Your First Request

Any HTTP client works – adjust the request structure to match your library.

Examples require Java 17+ and use the built-in java.net.http.HttpClient and Jackson for JSON parsing. Add Jackson to your project:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.21.3</version>
</dependency>

Choose your endpoint:

The Direct endpoint provides immediate PDF generation and returns the raw PDF file in the response.

Choose your source:

Convert a webpage to PDF:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Paths;

public class DirectUrl {
public static void main(String[] args) throws Exception {
String jsonBody = """
{
"url": "https://example.com",
"format": "A4",
"printBackground": true
}
""";

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.pdfbolt.com/v1/direct"))
.header("API-KEY", "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();

var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());

if (response.statusCode() == 200) {
Files.write(Paths.get("webpage.pdf"), response.body());
System.out.println("PDF generated successfully");
} else {
System.err.println("HTTP " + response.statusCode());
System.err.println("Error Message: " + new String(response.body()));
}
}
}

Next Steps



Related reading