How to manually install the PHPWord library

Intro

This is a guide for installing the library PHPWord manually or without having to use Composer on your website.

Requirements:

Download packages

To manually install PHPWord you will need to download 2 packages of files: the PHPWord package and the Common package, that contains files needed by PHPWord and other PHPOffice projects.

You can download both packages from the links in the library's Github page. Links to both packages are also copied below:

Once you have downloaded the packages, unzip them each to its own separate folder. You should end up with 2 folders called PHPWord and Common.

(Optional) Create a folder "PhpOffice" and put both the "PHPWord" and "Common" folders inside.

Install locally

The downloaded packages (PHPWord and Common) cannot yet be uploaded to your website and used. They first need to be unpacked by using Composer. And Composer requires PHP to be installed.

PHP

To install PHP locally in Windows, follow this excellent guide from geeksforgeeks.

Composer

Again, there is an excellent guide from geeksforgeeks for installing Composer locally in Windows.

Extensions

PHPWord requires two extensions of PHP: Zip and GD. This may only be required for local use and not necessary to enable if only planning on using PHPWord on your website. However, if these extensions are not enabled Composer will give out warning messages when trying to unpack PHPWord.

Enabling these extensions is simple:

  1. Go to the folder you installed PHP in. If you followed the geeksforgeeks guide, it should be in your Program Folder.
  2. Make a backup copy of the file "php.ini".
  3. Open the file "php.ini" with any text editor.
  4. Look for the line ";extension=gd" (around line number 940) and remove the ";".
  5. Look for the line ";extension=zip" (around line number 970) and remove the ";".
  6. Save the "php.ini" file.

Unpack

Next you will need to separately unpack (or "install", in Composer language) each of the two packages that were downloaded earlier (the PHPWord and Common packages).

Steps:

  1. Go inside the main folder of the package PHPWord. This folder should contain files such as "bootstrap.php" and "composer.json".
  2. Inside this folder, open Windows Command Prompt. A quick way to do so is to write "cmd" in the address bar of the Explorer window.
  3. In the Command Prompt terminal, write the following command:
    composer install --prefer-source
  4. After this process is completed, you should notice a new folder "vendor" inside the main folder of PHPWord. Inside this "vendor" folder there should be a file "autoload.php".
  5. Repeat the same process for the package Common.

Upload

In your website, create a folder called "PhpOffice" with two folders inside: "PHPWord" and "Common".

You only need to upload the "vendor" and "src" folders of each package in order to be able to use the library.

The structure of the folder PhpOffice in your website should look like this:

๐Ÿ“‚ PhpOffice
 โคท ๐Ÿ“‚ Common
    โคท ๐Ÿ“‚ src
    โคท ๐Ÿ“‚ vendor
 โคท ๐Ÿ“‚ PHPWord
    โคท ๐Ÿ“‚ src
    โคท ๐Ÿ“‚ vendor

Use

In order to properly call the required files in your PHP script, use the following PHP code:

// Require PHPWord files
require_once '/PhpOffice/PHPWord/vendor/autoload.php'; // make sure this path is correct

// Require Common files
require_once '/PhpOffice/Common/vendor/autoload.php'; // make sure this path is correct

Make sure that the path to the "PhpOffice" folder in your website is correct. The sample code assumes it is in the same folder as the script.

In order to create a PHPWord object, use the following PHP code:

// Create phpWord object
$phpWord = new PhpOffice\PhpWord\PhpWord(); // do not alter this path