Image to PDF in Java

This page describes how to use our cloud-based API to convert images to PDF in Java. The API is user-friendly and can be integrated into your application with just a few lines of code.

Sample Output

Installation

The Java API client library provides easy access to the Pdfcrowd API. No third-party libraries are required.

Download pdfcrowd-5.12.1-java.zip, unzip it and copy pdfcrowd-5.12.1.jar to your CLASSPATH.

We also offer other installation options such as Maven or GitHub.

Authentication

The credentials to access the API are your Pdfcrowd username and the API key.

API Method Reference

Refer to the Image to PDF Java Reference for a description of all API methods.

Code Examples

Here are a few Java examples to get you started quickly with the API. See more examples.

import com.pdfcrowd.*;
import java.io.*;

public class ApiTest {
    public static void main(String[] args) throws IOException, Pdfcrowd.Error {
        try {
            // create the API client instance
            Pdfcrowd.ImageToPdfClient client =
                new Pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

            // run the conversion and write the result to a file
            client.convertFileToFile("/path/to/logo.png", "logo.pdf");
        }
        catch(Pdfcrowd.Error why) {
            // report the error
            System.err.println("Pdfcrowd Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
        catch(IOException why) {
            // report the error
            System.err.println("IO Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
    }
}
import com.pdfcrowd.*;
import java.io.*;

public class ApiTest {
    public static void main(String[] args) throws IOException, Pdfcrowd.Error {
        try {
            // create the API client instance
            Pdfcrowd.ImageToPdfClient client =
                new Pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

            // run the conversion and write the result to a file
            client.convertUrlToFile("https://pdfcrowd.com/static/images/logo.png", "logo.pdf");
        }
        catch(Pdfcrowd.Error why) {
            // report the error
            System.err.println("Pdfcrowd Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
        catch(IOException why) {
            // report the error
            System.err.println("IO Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
    }
}
import com.pdfcrowd.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ApiTest {
    public static void main(String[] args) throws IOException, Pdfcrowd.Error {
        try {
            // create the API client instance
            Pdfcrowd.ImageToPdfClient client =
                new Pdfcrowd.ImageToPdfClient("your_username", "your_apikey");

            // run the conversion and write the result to a file
            client.convertRawDataToFile(Files.readAllBytes(Paths.get("/path/to/logo.png")), "logo.pdf");
        }
        catch(Pdfcrowd.Error why) {
            // report the error
            System.err.println("Pdfcrowd Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
        catch(IOException why) {
            // report the error
            System.err.println("IO Error: " + why);

            // rethrow or handle the exception
            throw why;
        }
    }
}

Troubleshooting