How to create a Web-Based Document Viewer in PHP

A web-based document viewer can make for an excellent addition to any project. But at what cost! Well, if you follow this tutorial, you’ll be done in 5 minutes. Let’s dive in.

With this command for composer, our API client can be installed without a hitch.

composer require cloudmersive/cloudmersive_document_convert_api_client

And our next step will be calling the function viewerToolsCreateSimple.

<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ViewerToolsApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input file to perform the operation on.try {$result = $apiInstance->viewerToolsCreateSimple($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ViewerToolsApi->viewerToolsCreateSimple: ', $e->getMessage(), PHP_EOL;}?>

The API will proceed to create a viewer for the input file and then return the embed code for said viewer. It’s really just that easy.

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store