How to convert PDF to Text TXT in PHP
2 min readMar 31, 2020
Sometimes simplicity is best. This is certainly true of TXT format, which can get you out of all sorts of jams with potential incompatibility. Today I’m going to show you how to convert the often clunky PDF format into simple text.
Install the client first with this command.
composer require cloudmersive/cloudmersive_document_convert_api_client
Following the installation, we can call our function:
<?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\ConvertDocumentApi(// 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.$text_formatting_mode = "text_formatting_mode_example"; // string | Optional; specify how whitespace should be handled when converting PDF to text. Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases. Default is 'preserveWhitespace'.try {$result = $apiInstance->convertDocumentPdfToTxt($input_file, $text_formatting_mode);print_r($result);} catch (Exception $e) {echo 'Exception when calling ConvertDocumentApi->convertDocumentPdfToTxt: ', $e->getMessage(), PHP_EOL;}?>
Select a formatting mode for your whitespace, then input a file and, voila! Your PDF is now TXT. Super easy.