Java SDK
The official Java SDK for PDFik enables you to integrate PDF generation into your Java applications with minimal effort.
It is built on top of java.net.http.HttpClient (Java 11+ built-in client) with zero extra HTTP library dependencies, exposing both synchronous and asynchronous methods.
Installation
Maven
Add the following dependency to your pom.xml:
<dependency>
<groupId>net.pdfik</groupId>
<artifactId>pdfik-client</artifactId>
<version>0.1.0</version>
</dependency>
Gradle
Add the following to your build.gradle:
implementation 'net.pdfik:pdfik-client:0.1.0'
Quick Start
Convert Public URL to PDF (Sync)
import net.pdfik.PdfikClient;
import net.pdfik.models.*;
import java.io.FileOutputStream;
import java.time.Duration;
public class Main {
public static void main(String[] args) {
// Initialize client using Builder
try (PdfikClient client = PdfikClient.builder()
.apiKey("sk_live_...") // Retrieve from your dashboard
.build()) {
// Configure PDF styling and margins
PdfOptions options = PdfOptions.builder()
.format(PaperFormat.A4)
.landscape(false)
.printBackground(true)
.margin(new MarginOptions("10mm", "10mm", "10mm", "10mm"))
.build();
// 1. Submit the URL to be rendered
JobCreatedResponse job = client.urlToPdf("https://example.com", options);
System.out.println("Job created: " + job.getJobId() + ". Processing...");
// 2. Poll until the job completes
JobStatusResponse result = client.waitForJob(job.getJobId());
System.out.println("Job finished! Pages: " + result.getPagesCount());
// 3. Download the PDF bytes
byte[] pdfBytes = client.downloadPdf(job.getJobId());
try (FileOutputStream fos = new FileOutputStream("output.pdf")) {
fos.write(pdfBytes);
}
System.out.println("PDF saved to output.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Convert HTML to PDF (Async)
import net.pdfik.PdfikClient;
import net.pdfik.models.*;
public class Main {
public static void main(String[] args) {
PdfikClient client = PdfikClient.builder()
.apiKey("sk_live_...")
.build();
client.htmlToPdfAsync("<h1>Hello World</h1><p>Sent from Java Async SDK</p>")
.thenCompose(job -> {
System.out.println("Job created async: " + job.getJobId());
return client.waitForJobAsync(job.getJobId())
.thenCompose(status -> {
System.out.println("Job complete. Downloading...");
return client.downloadPdfAsync(job.getJobId());
});
})
.thenAccept(pdfBytes -> {
System.out.println("Downloaded " + pdfBytes.length + " bytes.");
client.close();
})
.exceptionally(ex -> {
ex.printStackTrace();
client.close();
return null;
});
}
}
Get the direct download URL
Get the direct API download URL for the generated PDF (requires the "X-API-Key" header to download):
JobFileResponse fileInfo = client.getFileUrl(job.getJobId());
System.out.println("Direct Download URL: " + fileInfo.getDownloadUrl());
Best Practices
[!TIP] Use try-with-resources: The
PdfikClientclass implementsAutoCloseable. Always wrap it in atry-with-resourcesstatement or callclient.close()explicitly when done to clean up HTTP resources.
[!NOTE] Rate Limiting & Retries: The SDK automatically handles
429(Rate Limit) and5xxserver errors with an exponential backoff retry mechanism.