How to change the file type of a document programmatically

This article covers the main reasons why you might have to change the file type of a document, and the best methods to do so.

Imagine you have a simple front-end user interface set up which allows your customers to upload files. You can restrict the file types that users are allowed to upload, or you can convert these files to your desired format after the upload takes place. This article will cover the best ways to automate changing file extensions for your project.

Why change the file type of a document?

Here are some common situations developers may encounter, where converting the file type of a document would prove useful.

Customers expect PDF and DOC (Microsoft Word) file types

This is one of the main use cases for changing the file extension of a document. If you are developing an application that imports or exports text files, customers will expect that they can upload and download either DOC or PDF files without encountering any issues. Your software application should be able to manipulate and extract the data seamlessly from both of these file types, and easily convert between the two. This will allow users to download documents in their preferred format.

Legacy systems only accept a certain file type

When maintaining or updating a legacy system, you may encounter a situation where this antiquated application only accepts a single obscure file type. At this point you have two options:

  • Update the legacy system to accept more file types, which can be a painful process;
  • Integrate a ready-made solution that converts your files programmatically before they are fed into the system.

For example, enterprise systems often expect Excel files in either CSV or XLS file types, and you may need an efficient method of converting these files.

Saving data to a database

Your software application may contain functionality where it ingests a document, parses the data, and writes it to a database. This is a common use case for many different types of software applications. You may need to convert a Microsoft Excel file (XLS or XLSX) to CSV to effectively parse the data programmatically and put it in the format that your database expects it to be in.

What are your options for file type conversion?

There are a lot of options available for users when it comes to programmatically changing the format of files. These methods should be considered on a case-by-case basis, depending on the scale and type of software application you are working on. Each solution tends to offer distinct advantages and potential drawbacks, which we will discuss in detail.

Install packages and command-line utilities

Packages, extensions, and command-line utilities can easily be installed and integrated into your existing software projects. Many of these tools are open source, free to use, and maintained by the community. They offer fantastic functionality, but sometimes they lack specific features that you may need. A prime example of such a tool is pdftotext, which is an open-source command-line utility for converting PDF files to plain text files. Additionally, incorporating a PDF editor like Adobe Acrobat can provide advanced editing capabilities for manipulating PDF documents according to your project requirements.

Use existing code solutions

No matter what coding language you are using, it is likely you will find a software solution that someone has shared on the web. The potential drawbacks of using one of these solutions are that you will probably have to change some of the code to make it work perfectly with your project. In addition to this, some of the code you find online may be out of date or require other packages that are no longer supported.

Use an existing file management platform

File management platforms offer services such as file conversion and editing, and even allow developers to change file extensions programmatically.

Uploadcare is an example of such an all-in-one file platform. With a completely free subscription available, it can cater to your needs whatever your budget and scope of your project might be. On completing registration with Uploadcare you will have immediate access to a robust file uploader, AI tools for automated file processing, and the ability to transform your files and images using their impressive toolkit. Here are some of the advantages of using Uploadcare for your document conversion needs:

  • You can interact with documents manually or programmatically, offering you unparalleled control and functionality over your files.
  • They offer excellent customer service and support to assist you with any issues you may face.
  • They offer fantastic security options. You can control who is allowed to upload and view certain files, ensuring the security of your sensitive data.

The next section dives into how exactly you can start converting your documents using the Uploadcare platform. Uploadcare is capable of converting documents to the following formats: DOC, DOCX, XLS, XLSX, ODT, ODS, RTF, TXT, PDF, JPG, ENHANCED JPG, and PNG. This wide array of conversion options makes Uploadcare useful for different types of software projects.

Document conversion with Uploadcare

Before attempting to convert a document, you need to sign up for an Uploadcare account. There is a completely free 14-day trial period that will allow you to figure out whether Uploadcare is the right platform for you. After successfully signing up, there will be a new project available for you to use.

In order to convert a file, we first need to add a file to our project. Navigate to Files and upload a file of your choice. For this demonstration, we will be using a DOC file.

View of dashboard with files section selectedYou can upload a file here in the "Files" section.

After your file has uploaded successfully, go to the file and click Copy UUID. We will need this later.

How to copy the UUID for a fileFind the UUID for your file here.

Now we need to configure our project so it can send API requests. Navigate to API keys.

View of project dashboardClick on "API keys", located on the bottom left of the project dashboard.

Now it’s time to retrieve the public and private keys for your project.

The content on the API keys pageYou can clearly see your public key, and the option to generate a secret key.

Click Add secret key to generate a secret, or private, key.

Image showing how to show your secret keyFind your key by clicking "Show key".

Now that you have your keys, it’s time to send your API request. You can accomplish this by creating a simple piece of code like this guide on converting DOCs to PDFs, or you can use your favorite API testing tools such as Postman or Swagger UI. For the purposes of this demonstration, we will send a cURL request. If you do not have a cURL executable installed on your machine, you can use this free online API testing tool to send the request.

We are now ready to send our API request. It will take the following structure:

$ curl -X POST \
       -H "Content-Type: application/json" \
       -H "Accept: application/vnd.uploadcare-v0.5+json" \
       -H "Authorization: Uploadcare.Simple your_public_key:your_secret_key" \
       -d '{"paths": [":uuid/document/-/format/:target-format/"], "store": "1"}' \
       "https://api.uploadcare.com/convert/document/"

You will have to change the values of the highlighted sections of this request:

  • your_public_key
  • your_secret_key
  • :uuid
  • :target-format

After doing this, your request may look as follows:

$ curl -X POST \
       -H "Content-Type: application/json" \
       -H "Accept: application/vnd.uploadcare-v0.5+json" \
       -H "Authorization: Uploadcare.Simple b964a563c47ee45d7f03:9cf431865a1df3fg4cc5" \
       -d '{"paths": ["fff39a91-3923-4e0e-b9cf-5273ffb4661a/document/-/format/pdf/"], "store": "1"}' \
       "https://api.uploadcare.com/convert/document/"

If your request is successful, the response will look like the following snippet:

{
  "problems": {},
  "result": [{
    "original_source": "fff39a91-3923-4e0e-b9cf-5273ffb4661a/document/-/format/pdf/",
    "token": 16867058,
    "uuid": "99f70171-e30c-43af-872b-fc1a8b5133d5"
  }]
}

Now it’s time to see the result of our hard work. Return to the Files section of your dashboard:

View of files showing original and converted fileThe "Files" section should now contain two files.

You should now see two files: the original document, and the newly converted document.

On the Uploadcare documentation page, you can view more examples and actions you can take, such as how to convert multiple files at once programmatically.

I hope this article has cleared up the mystery surrounding the different options developers have for effectively converting documents from one file type to another. The best option often depends on your unique situation, but I hope you were able to extract some useful ideas to integrate into current or future software applications. If Uploadcare sounds like a good solution for your task, feel free to learn more and test-drive it.

Infrastructure for user images, videos & documents