ConvertAPI helps in converting various file formats. Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files and many other file manipulations. You can integrate it into your application in just a few minutes and use it easily.
PHP 8.1.0 and later.
The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.
Once composer is installed, execute the following command in your project root to install this library:
composer require convertapi/convertapi-phpIf you do not wish to use the Composer, you must require ConvertApi autoloader:
require_once('/path/to/convertapi-php/lib/ConvertApi/autoload.php');Library requires the following extensions in order to work properly:
If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.
You can get your API token at https://www.convertapi.com/a/authentication
use \ConvertApi\ConvertApi;
ConvertApi::setApiCredentials('api-token');Convert file to PDF example. All supported formats and options can be found here.
$result = ConvertApi::convert('pdf', ['File' => '/path/to/my_file.docx']);
# save to file
$result->getFile()->save('/path/to/save/file.pdf');
# get file contents (without saving the file locally)
$contents = $result->getFile()->getContents();Other result operations:
# save all result files to folder
$result->saveFiles('/path/to/save/files');
# get conversion cost
$cost = $result->getConversionCost();$result = ConvertApi::convert('pdf', ['File' => 'https://website/my_file.docx']);$result = ConvertApi::convert(
'pdf',
['File' => '/path/to/my_file'],
'docx'
);ConvertAPI accepts additional conversion parameters depending on selected formats. All conversion parameters and explanations can be found here.
$result = ConvertApi::convert(
'pdf',
[
'File' => '/path/to/my_file.docx',
'PageRange' => '1-10',
'Pdfa' => true,
]
);You can always check your usage by fetching user information.
$info = ConvertApi::getUser();
echo $info['ConversionsConsumed'];Use setApiBase method to set alternative service domain. Dedicated to the region domain list.
ConvertApi::setApiBase('https://eu-v2.convertapi.com/');Find more advanced examples in the examples/ folder.
- conversions_chaining.php - Chain two conversions: PDF to JPG images, then the images to a ZIP
- convert_stream.php - Convert text from an in-memory stream to PDF
- convert_url_to_pdf.php - Convert a web page URL to PDF
- convert_word_to_pdf_and_png.php - Convert one Word DOCX file to PDF and to PNG
- create_pdf_thumbnail.php - Turn the first page of a PDF into a JPG thumbnail
- error_handling.php - Catch an API error and print its code and message
- retrieve_user_information.php - Retrieve your ConvertAPI account information
- split_and_merge_pdf.php - Split a PDF and merge its first and last pages into a new PDF
- vanilla_php.php - Convert DOCX to PDF with plain PHP and cURL, without this library
Testing is done with PHPUnit:
CONVERT_API_SECRET=your-api-token ./bin/phpunitBug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-library-php. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
ConvertAPI PHP Client is available as open source under the terms of the MIT License.