I'm looking for a free Java library to convert various file types (as many file types as possible) to PDF.

By various file types, I mean at least most images, Microsoft office documents (at least Word and Excel), text files, rich text format (RTF).

I want to convert static files, not video or audio.

  • Are you looking for a pure Java solution? Or are you open to calling command line tools from Java for instance?
    – Ryan
    Commented Aug 22, 2021 at 2:47
  • @Ryan I am looking for java library. Java Solution Commented Aug 27, 2021 at 7:00
  • Why is a pure java solution important for you? Are you under some sort of legal restriction to only run code in the JVM? Is there a specific reason you cannot call a command line tool?
    – Ryan
    Commented Aug 27, 2021 at 15:55
  • @ryan I don't think that this is important. Can you please recommend Java library that can perform this kind of task? Also I will gladly hear about command line tools. Thanks Commented Aug 27, 2021 at 16:20

2 Answers 2


For gratis solutions, you will get the best coverage from the following three applications, which can all convert many different file formats to PDF, and will work on Windows/Linux(/macos?).

LibreOffice : office files/text files


ImageMagick : image formats


Chrome (headless) : html/svg


All have command line options to convert many different file types to PDF, though for Chrome headless you need to use an interface to unlock all the options (e.g. to hide the default header/footer that chrome adds).

From there you can just use Java shell execute to trigger the commands you want. As a bonus, since each call is a separate process, you get process isolation (so your JVM is safe) and easy to scale parallelism/concurrency.


Spire.Office for Java library can be used to convert multiple file types like Word, RTF, Excel, PowerPoint, text and images etc. to PDF. It also provides a free community edition but with some limitations.

The following code example shows how to convert multiple documents (Word, Excel, PowerPoint, PDF and image) within a folder to PDFs and combine the resultant PDFs to a single PDF.

import com.spire.doc.Document;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfDocumentBase;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import com.spire.presentation.Presentation;
import com.spire.xls.Workbook;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.io.*;

public class MergeFilesToOnePDF {
    public static void main(String []args) throws Exception {        

        //Create a File object for directory
        File directoryPath = new File("D:\\Files");
        //Check if the directory exists
            throw new RuntimeException("The directory does not exist.");
        //Get the files in the directory
        File[] filesList = directoryPath.listFiles();

        //Create an InputStream array
        InputStream[] streams = new InputStream[filesList.length];
        int i = 0;
        //Loop through the files
        for (File file : filesList) {
            //Get the file name
            String fileName = file.getName();
            //Get the absolute file path
            String absolutePath = file.getAbsolutePath();
            //Get the file name extension
            String extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());

            //Convert different types of files to PDF and save to streams
            switch (extension) {

                case ".doc":
                case ".docx":
                    //Convert Word to PDF and save to stream
                    Document doc = new Document();
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    doc.saveToStream(bos, com.spire.doc.FileFormat.PDF);
                    streams[i++] = new ByteArrayInputStream(bos.toByteArray());

                case ".xls":
                case ".xlsx":
                    //Convert Excel to PDF and save to stream
                    Workbook workbook = new Workbook();
                    bos = new ByteArrayOutputStream();
                    workbook.saveToStream(bos, com.spire.xls.FileFormat.PDF);
                    streams[i++] = new ByteArrayInputStream(bos.toByteArray());

                case ".ppt":
                case ".pptx":
                    //Convert PowerPoint to PDF and save to stream
                    Presentation ppt = new Presentation();
                    bos = new ByteArrayOutputStream();
                    ppt.saveToFile(bos, com.spire.presentation.FileFormat.PDF);
                    streams[i++] = new ByteArrayInputStream(bos.toByteArray());

                 case ".pdf":
                    //Save PDF to stream
                    PdfDocument pdf = new PdfDocument();
                    bos = new ByteArrayOutputStream();
                    pdf.saveToStream(bos, com.spire.pdf.FileFormat.PDF);
                    streams[i++] = new ByteArrayInputStream(bos.toByteArray());

                case ".png":
                case ".jpg":
                    //Convert image to PDF and save to stream
                    pdf = new PdfDocument();
                    //Convert inches to points
                    PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
                    float width = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
                    float height = unitCvtr.convertUnits(11.0f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
                    Dimension2D size = new Dimension((int)width, (int)height);
                    //Set page size
                    PdfPageBase page = pdf.getPages().add();
                    PdfImage image = PdfImage.fromFile(absolutePath);
                    page.getCanvas().drawImage(image, 100, 50);
                    bos = new ByteArrayOutputStream();
                    pdf.saveToStream(bos, com.spire.pdf.FileFormat.PDF);
                    streams[i++] = new ByteArrayInputStream(bos.toByteArray());

                    System.out.println("Invalid file format.");

        //Merge PDFs in the streams into a single PDF
        PdfDocumentBase pdf = PdfDocument.mergeFiles(streams);
        OutputStream outputStream = new FileOutputStream("D:\\Output\\Combine.pdf");
  • What are the limitations of free community edition?
    – convert
    Commented Jan 13, 2022 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.