How to use a REST API to Convert Document formats
In this tutorial, we will create a PHP page that consumes REST API to convert document formats, using Doc Conversion API. REST enables you to communicate with web based services and integrate them in your system, through simple lines of code.
First, we need to have a look at the API tool we’re using, for this tutorial, we will be using Doc Conversion API, A REST API For easily converting document formats, as it allows you to convert between Word, Excel, PowerPoint, HTML, PDF and different image formats with a whole bunch of sensible formatting options.
Prerequisites
Before we start with this tutorial, we need to prepare a few simple prerequisites. We need have an account with Doc Conversion API, so we can use our app credentials in the REST request.
To create your account, you can sign up here for free. You will then be able to login to your dashboard, from there, you will need to take note of two keys that will play an important role in this tutorial, your Application ID, and your Secret Key.
To get these credentials:
- Click on API Applications
- You will see a list of your applications, select the one you would like to use and take note of these two values. (For this tutorial, we’re using the default application, which is automatically created for you you first sign up).
Using the REST API
Doc Conversion API have an API that you can easily connect to and integrate with your service, code or apps. We’re first going to test the connection with the API by retrieving the connection status of the service.
To get the status, we need to link into the following URL:
https://api.docconversionapi.com/status
Which means our post request will consist of the status URL, along with Application ID and Secret Key.
This page should return a status when executed, either OK or not OK.
Now that we’ve tested the connection with the status of the API, we are ready to go ahead and try out a document conversion. For this tutorial, we will convert a Word document to a PDF file.
For this conversion, we need to use a direct URL for your word document (the document you would like to convert),you can also upload documents to the service, but for simplicity we’ll do it this way. We’re going to be using this example:
http://homepages.inf.ed.ac.uk/neilb/TestWordDoc.doc
Once we have that ready, we’re ready to build our PHP page and call in to the REST API.
Executing this page should produce a PDF document, that looks like the following.
You can follow the same steps with different formats conversions as Doc Conversion API offers an extensive list of conversions that you can easily implement.
You can find a list of different format conversion APIs and more examples here.
✉️ Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst on Twitter, view 🗺️ The 2018 Web Developer Roadmap, and 🕸️ Learn Full Stack Web Development.