Build an online document converter with ONLYOFFICE Conversion API

10 February 2023 By Serge 0 181

Document conversion is a very popular and valuable feature that helps us operate with multiple document types. ONLYOFFICE Editors can easily convert documents to a wide range of formats. This blog post will show you how to build an online converter that runs on ONLYOFFICE Conversion API.

Build an online converter with ONLYOFFICE Conversion API

About ONLYOFFICE Conversion API

Document Conversion service allows converting all kinds of Office documents: texts, sheets, slides, forms, PDFs, and eBooks. It facilitates creating PDFs from documents, and sheets, converting textbooks into eBooks, presentations into image files, and more. ONLYOFFICE supports more than 50 different document types.

How it works

Our conversion API operates through the document conversion service. It serves as a part of the ONLYOFFICE Document Server and allows us to convert various document files into the appropriate formats.

The conversion is performed in several steps:

  1. The user selects a file that is to be uploaded to the document manager.
  2. The document manager uploads the selected file to the document storage service.
  3. The document storage service sends the uploaded file to the document conversion service by using the conversion API.
  4. The document conversion service converts the selected file to the targeted format.
  5. The document storage service downloads the converted document file.

Build an online converter with ONLYOFFICE Conversion API

Document manager and document storage services are client and server side tools that facilitate selecting and storing a document for further conversion. However, our web application will tackle those tasks since we are building a custom converter.


Our converter will be NodeJS based. So for this project, we will need the following:

– Express

– Axios

– Jsonwebtoken

We will use the Axios package to send a post request to the ONLYOFFICE Document Server and the Jsonwebtoken package to sign a JWT token. Starting from version 7.2, JWT authentication is enabled by default.

Alternatively, there is an option to run the ONLYOFFICE Document Server image with JWT authentication disabled. To do that, execute the following command in the terminal:

The post request that our converter is sending to ONLYOFFICE Document Server is the following:

  • In the accept parameter, we specify that we want to receive a response in JSON format.
  • The async parameter indicates that the request is asynchronous.
  • The fileType parameter specifies the type of the original file that we are going to convert.
  • The key parameter represents the UID identifier of the current document.
  • The outputType parameter indicates the format of the converted document.
  • The title parameter contains the name of the converted document.
  • The url parameter includes a link to the file that we want to convert.

The values of the fileType, otputType, and url parameters are fetched from the client side of our application and stored in the variables. The value of the key parameter will be randomly generated and stored in the variable as well.

Project set up

After installing the required packages, we go to the app.js file and initialize them alongside the bodyParser to process the post request data. We also set up a public folder and a view engine:

Then we add routes. Our app will include a get route and a post route.  They will allow us to fetch the input data and pass it into the post request to ONLYOFFICE Document Server:


Now we take a little detour and look into the client side of our converter. That’s where we will input all the required data:

Build an online converter with ONLYOFFICE Conversion API

Build an online converter with ONLYOFFICE Conversion API

The client-side includes two EJS pages::

  • homepage.ejs – allows us to fetch all the values required for the post request.
  • converter.ejs – allows us to download the converted file.

Let’s take a closer look at the homepage.ejs. Here we created a form that sends data to the post route. First, we get a URL to the original file. We will store it in the link variable on the server side:

Then we pick a type of the original file in the combo box. Later on, we will fetch this value and store it in the inputType variable:

Then we choose the desired file type. This value will be stored in the outputType variable on the server side:

And we use a button to send the entire data to the converter post route:

 Building a converter

The fetched data will be parsed on the server side of our application. So now, let’s go to the app.js file and get it:

Now let’s take a look at our second combo box that sends the outputType value to the post route:

The outputType variable is included in the post request to the server. It specifies the format of the converted file. Let`s examine the JavaScript code that allows us to interact with the page elements and incorporate a dynamic list into our UI.

The document conversion service is a very powerful tool, and it’s capable of converting various types of files. So our goal is to utilize a dynamic list that will allow us to select a format for the converted file. This list will display all the options available for the particular type of original file.

To do that, we create a JSON file that stores values in the accordance with each particular type:

Build an online converter with ONLYOFFICE Conversion API

Note! For demonstration purposes, we included only the most common OOXML formats. To learn all the supported conversion options, visit our documentation page.

Then we add a function that will generate this dynamic list:

First, this function gets the value of the inputType combo box, where we selected the format of the original file. We assign it to the type variable to use this value down the line. Then we make an AJAX request to load the JSON data. After that, we use the value of the type variable as an index and iterate through the loaded data to insert its values into the options of the drop-down list.

Now, every time we select the type of the original file, the script executes and shows us the available conversion options in the accordance with the original file format.

And after we have all the required data, we send it to the converter route by clicking the Convert button.

But that’s not it.  To make our app more interactive, we will add functions that will activate the combo box and button elements in the right sequence:

The entire JavaScript code is the following:

Let’s take a look at what’s going on on the server side:

In the converter route, we formed our post request to the ONLYOFFICE Document Server and stored it in the payload variable. We used fileType, outputType, and link variables which store the data that we fetched. However, we also have the key variable that contains the unique identifier of the current document. So we added a little function above to generate it:

Now that we have all the necessary values for our request, we use jwt.sign to wrap them in a token:

The jwt.sign method takes three parameters:

  • Payload that includes all the parameters required for the successful conversion.
  • Header that contains information on the cyphering algorithm and the expiry time frame. We wrapped these parameters in the options variable.
  • Secret that represents a secret key generated by ONLYOFFICE Document Server. This secret can be found in the local.json file, or by executing the following command in the terminal:

After we sign the token, we use an axios post request to send it to the server. Then we render the converter.ejs page which receives the response from ONLYOFFICE Document Server:

Here is a sample of the response in JSON format:

What need here is the fileUrl element. It is a link to the converted file. So we access and send it to the converter.ejs page:

On that page, we created two buttons. The Go back button takes us back to the homepage.ejs page and the Download button opens the link that we sent to the page and downloads the converted file:

Build an online converter with ONLYOFFICE Conversion API

Note!  To learn more about JWT, visit our documentation page.

The entire server-side code is the following:

And now, let’s run our converter and see how it works!

ONLYOFFICE solutions are extremely multifunctional. They provide users and developers with a unique experience and allow them to manipulate ooXML documents in various ways. We hope that you will find the information mentioned above useful and apply it in your future projects. Don’t hesitate to leave comments, ask questions or share your ideas with us. We are open to suggestions and cooperation. Best of luck in your exploratory endeavors!