DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert DOCX to PDF in Java
  • How to Get Plain Text From Common Documents in Java

Trending

  • Debugging Core Dump Files on Linux - A Detailed Guide
  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  • How to Convert Between PDF and TIFF in Java
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How To Convert ODF Files to PDF in Java

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.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Nov. 06, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

When 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:

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:

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:

Java
 
// 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:

Java
 
// 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:

Java
 
// 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.

API PDF Convert (command) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert Common Documents to PNG Image Arrays in Java
  • How to Convert a PDF to Text (TXT) Using Java
  • How to Convert DOCX to PDF in Java
  • How to Get Plain Text From Common Documents in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!