How To Convert ODF Files to PDF in Java
Discover how it is easier to convert ODF content to PDF with free API solutions at our disposal like ODF, an alternative to licensed office application suites.
Join the DZone community and get the full member experience.
Join For FreeWhen the time comes to pick and download a suite of office applications, we can expect that the MS Office suite will dominate the conversation. Each application included in the MS Office package comes equipped with dozens of powerful, intuitive features, and all the OpenXML file formats – .DOCX, .XLSX, PPTX, etc. – can shoulder the burden of immense, data-intensive edits and developer customizations while retaining a relatively small file size through efficient compartmentalization and lossless compression.
However, OpenXML files aren’t the only ones represented in XML and zipped into convenient lossless file containers. The Apache OpenOffice suite – which originated in 2006, right around the timeframe when OpenXML format became standard for MS Office files – also offers XML-based file structure with lossless Zip compression, and this option makes for a nice alternative to OpenXML files when cost considerations come into play.
For the average business user, Open Office files offer mostly the same basic content-creation functionality as MS Office files, and the key tradeoffs are only typically noticeable when the myriad built-in connections and compatibility features MS Office provides (with its much broader suite of business applications) come into consideration. On the other hand, for the average developer building applications and workflows that automatically interact with and make changes to office documents in one shape or another, the XML-based file structure is all that really counts. It’s easy to leverage a standardized, existing set of developer tools to unzip and manipulate XML-based file content within the logic of any file-processing application. Just like a developer in an MS Office environment can, for example, use custom code or an API solution to unzip a DOCX file and extract copies of all the image files nested within the document, a developer in an OpenOffice environment can do essentially the same thing with an ODT file using similar knowledge of XML data structures.
Ironically, perhaps, one of the most practical similarities between OpenOffice files and MS Office files is their inherent lack of presentability as a final product. When OpenOffice users wrap up their data analysis, content writing, or presentation projects, it’s unlikely anyone reviewing their finalized content will be opening an .ODT, .ODS, or .ODP file to do so – just like they wouldn’t view MS Office content in .DOCX, XLSX, or .PPTX format either. Chances are extremely high that they’ll open that finalized content in a vector or raster PDF iteration of those files – and there’s good reason for that, of course. Once converted to PDF, that content can’t be easily altered or stolen, and on top of that, the presentability of PDF – whether viewed in a browser client or within a PDF reader – far outclasses the noisy, work-in-progress look of content opened within its original processing application.
Just like developers working in MS Office environments, it’s important that developers working in OpenOffice environments have access to quick, secure, and easy-to-use solutions for converting finalized OpenOffice content to PDF when users require it. To that end, I’ll demonstrate three free-to-use APIs designed to convert all major Open Document Format file types to PDF. Also, this tutorial will provide ready-to-run code examples further down the page to make the process of structuring API calls easy.
Demonstration
Each of the below API solutions will allow developers to convert .ODT, .ODS, and .ODP files to standard PDFs using minimal Java code examples in secure, in-memory requests with all document data released upon completion. Each request can be authorized with a single free-tier API key, and these will allow up to 800 API calls per month.
To begin structuring our API request, we can start by installing the Java SDK. To install with Maven, let’s first add a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add a reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
With SDK installation complete, we can use the below code to convert our ODT files to PDF:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentOdtToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdtToPdf");
e.printStackTrace();
}
And we can use the below code to convert our ODS files to PDF:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentOdsToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdsToPdf");
e.printStackTrace();
}
Lastly, we can use the below code to convert our ODP presentations to PDF:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
try {
byte[] result = apiInstance.convertDocumentOdpToPdf(inputFile);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentOdpToPdf");
e.printStackTrace();
}
We’ll receive the encoding for our new PDF file in the API response, and we can go ahead and create our new file with that encoding.
Opinions expressed by DZone contributors are their own.
Comments