How to output data from a form in word?

How to output data from a form in word?


Let’s begin. The first thing we will need to do is configure the local Open Server (or analogs). Well, either you can immediately perform these actions on the hosting, it’s so convenient.

Create in the folder domains project folder, make sure PHP version is 7.1 or higher, and everything will be fine.

Create two files in the folder – index.html, word.php and add a file-template in docx format (you can find it by the link to the github at the bottom of the post).

Installing the library

We will be using the PHPWord library for our implementation. You cannot simply download it with the download button, you will need to install a special composer utility. In fact, this is the same as npm, but only for PHP.

When installing composer you will be asked to select a special php file that corresponds to the selected language version. Go to OSPanel / modules / php / your_version and find the php.exe file there, and select it.

Well, after installation composer you can use the OSPanel console to install this utility. Everything happens the same as with npm – composer require phpoffice/phpword… Do not forget to navigate through the console to the desired folder.

The installation process is shown in more detail and clearly in the video at the beginning of the post.

Writing HTML

<form action="word.php" method="POST" enctype="multipart/form-data">
<input type="date" name="birth">
<input type="text" name="name" placeholder="Введите ФИО">
<input type="tel" name="tel" placeholder="Введите телефон" required>
<input type="text" name="city" placeholder="Введите город">
<input type="text" name="purpose" placeholder="Введите цель">
<input type="number" name="startYear" placeholder="Введите начальный год обучения">
<input type="number" name="lastYear" placeholder="Введите конечный год обучения">
<input type="text" name="university" placeholder="Введите университет">
<input type="file" name="file">
<textarea name="about" id="" cols="30" rows="10" placeholder="О себе"></textarea>
<button type="submit">Отправить</button>

It’s just a form that has multiple fields, each with a name that will link our markup and php code.

Setting up a template

It is better to watch this process in a video than to describe it in words.

PHP handler

require_once 'vendor/autoload.php';

$document = new PhpOfficePhpWordTemplateProcessor('./review.docx');

$uploadDir = __DIR__;
$outputFile = 'review_full.docx';

$uploadFile = $uploadDir . '\' . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile);

$birthdate = $_POST['birth'];
$name = $_POST['name'];
$tel = $_POST['tel'];
$city = $_POST['city'];
$purpose = $_POST['purpose'];
$startYear = $_POST['startYear'];
$lastYear = $_POST['lastYear'];
$university = $_POST['university'];
$file = $_POST['file'];
$about = $_POST['about'];

$document->setValue('name', $name);
$document->setValue('birthdate', $birth);
$document->setValue('tel', $tel);
$document->setValue('city', $city);
$document->setValue('purpose', $purpose);
$document->setValue('startYear', $startYear);
$document->setValue('lastYear', $lastYear);
$document->setValue('university', $university);
$document->setValue('about', $about);
$document->setImageValue('image', array('path' => $uploadFile, 'width' => 120, 'height' => 120, 'ratio' => false));


// Имя скачиваемого файла
$downloadFile = $outputFile;

// Контент-тип означающий скачивание
header("Content-Type: application/octet-stream");

// Размер в байтах
header("Accept-Ranges: bytes");

// Размер файла
header("Content-Length: ".filesize($downloadFile));

// Расположение скачиваемого файла
header("Content-Disposition: attachment; filename=".$downloadFile);

// Прочитать файл


It may seem like a lot of code, but in fact it is very simple.

  1. First we use TemplateProcessorto get started with the template
  2. Then we create variables in which we receive data from the superarray $_POST – that is, just our form data
  3. Further, using the method setValue we match our variables and data from the template. That is, we just use the template and insert data into it.
  4. The next step is to save everything via the command saveAs
  5. And then – downloading the file through readfile and deleting it through unlink

Hope this article helps you. If not, watch the video, nevertheless the video format is more descriptive.

Good luck!

Leave a Reply

Your email address will not be published.