How to validate a PDF Document in PHP

PDF validation — nobody’s favorite chore by any means. We have two options in our approach today: we can do it the old-fashioned hard way, or we can take a shortcut that’s going to cut our overall time down to well under ten minutes. I know which method I’m going to choose.

Starting things up, we shall go to the command line and run this command here:

composer require cloudmersive/cloudmersive_document_convert_api_client

That will install our API client and allow us to continue on to calling the validateDocumentPdfValidation. Here’s the code you will need for that:

<?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\ValidateDocumentApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input file to perform the operation on.try {$result = $apiInstance->validateDocumentPdfValidation($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ValidateDocumentApi->validateDocumentPdfValidation: ', $e->getMessage(), PHP_EOL;}?>

Now we can input a PDF file and see what our API sends back as to its validity. That’s right, you are actually all done at this point. I think we made the right call.

Image for post
Image for post

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