Convert MS Office documents to PDF using PHP

Kostas, Developer

ConvertAPI helps convert many various file types from one format to another. A common use case is converting Microsoft Office documents into PDF using PHP programming language. You don't need to suffer learning all the complexity of each file conversion - we do the hard work for you. In this article, we will discuss several examples of converting DOCX, XLSX, and PPTX documents to PDF files.

Prerequisites

ConvertAPI is an API based service, but you don't need to write plain HTTP requests - we've got it covered. Simply use our PHP SDK library. It can be installed via Composer composer require convertapi/convertapi-php or manually, by requiring the ConvertAPI autoloader require_once('/path/to/convertapi-php/src/ConvertApi/autoload.php');. When using a manual installation, please make sure that curl and json dependencies are available.

You will also want to sign up for a free account and grab your API Secret from your dashboard. Once this step is completed, we can start coding!

Convert DOCX to PDF

image

Converting an MS Word document to PDF using PHP is really simple. You can set up your conversion using our DOCX to PDF interactive demo tool. There are a handful of options like page range, orientation, size, font options, and multiple PDF settings that you can set during your conversion. Once you set up your conversion on our website, you will find an auto-generated code snippet at the bottom of the page. In general, this code snippet is all it takes to convert an MS Word to PDF using our PHP library with some properties specifying the page range, page orientation, and page size:

ConvertApi::setApiSecret('your-api-secret');
$result = ConvertApi::convert('pdf', [
        'File' => '/path/to/my_file.docx',
        'PageRange' => '1-10',
        'PageOrientation' => 'portrait',
        'PageSize' => 'a4',
    ], 'docx'
);
$result->saveFiles('/path/to/result/dir');

Convert XLSX to PDF

image

Another popular conversion is converting Microsoft Excel sheets to PDF. Similarly to the conversion above, we built a live demo tool where you can set up your conversion without coding. This conversion allows you to select the sheet to convert by it's name, index, or the active sheet (default). You can also convert a password protected document, set the page size, orientation, scale it, and use the auto-fit layout. The output PDF also has a bunch of valuable properties, so you can customize the conversion to your needs quickly. Once the conversion is set up, you will find an auto-generated code snippet at the bottom of the page.

Here is a quick code example of an XLSX to PDF conversion with some properties, specifying the active sheet conversion with a landscape orientation and auto-fit columns and rows property:

ConvertApi::setApiSecret('your-api-secret');
$result = ConvertApi::convert('pdf', [
        'File' => '/path/to/my_file.xlsx',
        'WorksheetActive' => 'true',
        'PageOrientation' => 'landscape',
        'AutoFit' => 'true',
    ], 'xlsx'
);
$result->saveFiles('/path/to/result/dir');

Convert PPTX to PDF

image

Another big member of the MS Office family is PowerPoint. In many cases, you might need to convert a PowerPoint file to a PDF document. Our service will help you do this with ease. As for every conversion, we have prepared an interactive tool to set up your conversion online and provide an auto-generated code snippet. Our PPTX to PDF converter allows you to convert password protected files, select whether you want to include hidden slides while converting, convert document metadata like Title, Author, Keywords to PDF metadata, etc.

Set up the conversion parameters, and the code needed to convert your PowerPoint slideshow to PDF will look something like this:

ConvertApi::setApiSecret('your-api-secret');
$result = ConvertApi::convert('pdf', [
        'File' => '/path/to/my_file.pptx',
        'Password' => '123456',
        'ConvertHiddenSlides' => 'true',
        'ConvertMetadata' => 'false',
    ], 'pptx'
);
$result->saveFiles('/path/to/result/dir');

Takeaway

Converting MS Office suite documents to PDF using PHP is simpler than it sounds. Our service supports many more MS Office document types than described in this article.

What is common among these conversions, there are multiple converters capable of handling the conversion. We call them alternative converters. For example, DOCX, XLSX, and PPTX can all be converted to PDF using MS Office, OpenOffice, Printer, or Direct converters. It is useful to try an alternative converter if the default one fails. Some MS Office files might be corrupt, so it may be worthy to set up a fallback option using an alternative converter in advance.

Otherwise, please relax and enjoy your time while our service does the hard part for you. Happy coding!