Convert Your Local Files Using Doc Conversion REST API

At Doc Conversion API, we want to make all conversions and document operations as easy as possible, so we decided to save you the worry of having to have specific storage configuration, as you can use local files, or files stored on the cloud.

As we’ve explained how to use cloud storage files in previous blogs, this time we’re going to focus on using a local file in your conversion, so you can use your own files that are stored on your local computer.

Using the REST API Calls to Convert Local Files


This method is most suitable for backend conversion and processing of documents, if you have a recurring conversion type or want to convert a large number of files, and would like to automate your conversion process.

As explained in previous blog posts, you can use files stored on the cloud, or files with direct download URL, but in this example, we’re going to use a file that is stored on your local disk.

Since our servers will not have direct access to local storage, we need to send an array of bytes that contains all the information about the file to the end-point.

The first step would be to locate the file you would like to use, and get the full path of the file. For simplicity, we’re going to be a word document file called “sample” and stored in the documents folder, but you can use any file and store it anywhere you would like, as long as you’re able to specify the correct path. So the path for our file will be:
/Users/MacBookPro/Documents/sample.doc

Location of local file

The file looks like this:

Local word file

Now we have the file path, we can go ahead and use it in our code, below is a sample PHP code that will perform a simple doc to HTML conversion, create an HTML file, and return, “operation completed successfully” when the conversion is done.

 <?php
 $ch = curl_init();
 $path = 'fl.html';
 $file_name_with_full_path = "/full/path/to_file/fileName.docx";

curl_setopt($ch, CURLOPT_URL,"https://api.docconversionapi.com/convert?outputFormat=html");
 curl_setopt($ch, CURLOPT_POST, 2);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-ApplicationID: ','X-SecretKey: '));

if (function_exists('curl_file_create')) { // php 5.5+
 $cFile = curl_file_create($file_name_with_full_path);
 } else { //
 $cFile = '@' . realpath($file_name_with_full_path);
 }

$post = array(
 "optionsJson"=>"{}",
 "inputFile"=> $cFile
 );

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $server_output = curl_exec($ch);

if($server_output === false)
 {
 echo 'Curl error occurred: ' . curl_error($ch);
 }
 else
 {
 echo 'Operation completed successfully';
 }

// save PDF buffer
 file_put_contents($path, $server_output);
 ?>

Make sure to replace the path with the valid path to your file, and update your Application ID and Secret Key (you can get those from your Doc Conversion API dashboard).

After executing this code, you should now have a converted HTML file on your local disk. Our output for this example looks like this:

converted local file HTML

 

You can, of course, customize the code sample enough to fit your needs. More code examples can be found on our documentation page, and specific conversion examples can be found on our blog.
If you need any further help, please contact us at [email protected].