Template Input
Sample Download
Template Guideline Collapse All
- Make a new Excel or Spreadsheet document, rename the first worksheet to "template", then start your template design.
- Prepare your data pool in Associative Array :
Named keys (Variable) format : ${<variable_name>}[
'${var_mydate}' => '01-01-2023',
'${var_myname}' => 'My Name',
] - Fill in the Variable into the templete where needed. PhpOfficeTemplate will replace the variable with its
respective value :
In Template Date : ${var_mydate} Name : ${var_myname} Output Result Date : 01-01-2023 Name : My Name - Upload your template document and preview the output result.
- Make a new Word or Document document and start your template design.
- Same as Excel/Spreadsheet Step (2), (3) and (4).
Advance Usage
Merge Into PDF from Excel/Word/PDF
Processing Template
Processing Template
Please wait patiently while system is generating the template result ...
Introduction
PhpOfficeTemplate is a PHP wrapper library for
PhpSpreadsheet and
PhpWord that searches and replaces search-patterns (macros):
${search-pattern}
found in your document content (Word, Document, Excel or Spreadsheet) with dynamic data and outputs the result in different document formats such as PDF, XLSX, DOCX, etc.
The data can be retrieved from a text file stored on your server or retrieved by querying from a database. The dataset provided must be in associative array form.
Roadmap
- image value substitution
- integrate Ace Editor in code sampling, refer Random Data API
- Prepare for user to download sample template and data
-
multi lines value substitution
- excel ok
- word need OfficeConverter, if not will result in single line -
image size settings
- word need OfficeConverter
- word replacement image how to update size
- excel need support size settings like word in template - excel/word convert to html
Word/Document
Utilise PHPWord > TemplateProcessor to fill in data.
Conversion Supported
Browser Display | Downloadable | Save In Server | Remark |
---|
Excel/Spreadsheet
Conversion Supported
Browser Display | Downloadable | Save In Server | Remark |
---|
Installation
Via Composer
# composer require zeikman/phpofficetemplate
Via Git
# cd <path-to-your-project>
# git clone https://github.com/zeikman/PhpOfficeTemplate
# cd PhpOfficeTemplate
# composer install
Note!
You need to install all dependencies manually :
# cd <path-to-your-project>/phpofficetemplate
# composer install
If you facing any issue during dependencies Installation, you can try command below :
# composer install --ignore-platform-reqs
Dependencies
PhpOfficeTemplate depends on following libraries. Please install all of them using composer.
-
PhpSpreadsheet 1.25+
(main dependency)
latest dev version : 1.27.0
# composer require phpoffice/phpspreadsheet
-
PHPWord 1.0+
(main dependency)
latest dev version : 1.0.0
# composer require phpoffice/phpword
-
mPDF 8.1+
latest dev version : 8.1.4
# composer require mpdf/mpdf
-
TCPDF 6.6+
latest dev version : 6.6.2
# composer require tecnickcom/tcpdf
-
Dompdf 2.0+
latest dev version : 2.0.2
# composer require dompdf/dompdf
-
Office Converter 1.0+
latest dev version : 1.0.3
# composer require ncjoes/office-converter
- PDFMerger (Optional - but Required for Advance usage)
Enable Office Converter!
- Office Converter is a PHP Warpper for LibreOffice, in order to use it, you need to install its main dependency, LibreOffice.
- If OfficeConverter does not output any result after install LibreOffice, please try following updates on OfficeConverter lib :
- Go to vendor/ncjoes/office-converter/src,
- Open OfficeConverter.php source file using any file editor,
- Change line 245
from$cmd = 'export HOME=/tmp && '.$cmd;
to$cmd = 'HOME='.getcwd().' && export HOME && '.$cmd;
, - Now try to load your Word/Document template again.
Tips to install LibreOffice in Linux
- Download LibreOffice 7.4.3 from the official page. (Note: You can download any version that you prefer)
- To install LibreOffice, you are advised to install via the Installation methods recommended by your particular Linux distributon (such as Ubuntu, Centos, and etc). Detailed information is available on the wiki.
Usage
Basic Usage
This would be the simplest way to use PhpOfficeTemplate :
Configuration
There are two ways of passing the template file into PhpOfficeTemplate :
- Passing the $_FILES directly into PhpOfficeTemplate using POST method as shown above, and
- Passing directory path in your server that storing the template file as shown below.
Passing Data
To pass the data for variable substitution :
Options
Option | Description | Default |
---|---|---|
enable_empty_space | Substitute variable with empty-space if not found. | false |
enable_office_convertor |
Using OfficeConverter lib for Word/Document output result.
You must have OfficeConverter and LibreOffice installed. |
false |
file_dir |
Server directory where template file stored.
Needed ONLY when you are reading template file from server. |
|
output_dir |
Server directory to output result.
Needed ONLY when you want to save output in server or using OfficeConverter library. |
|
file_post |
Temporary filename of the file in which the uploaded file was stored on server, for example
<?php
$config = [
'file_post' => $_FILES['upload_file']['tmp_name'],
];
?>
Specified this argument ONLY when you prefer template to be processed without being uploaded to server. |
|
file_name |
Template filename stored in server, or the original name of the uploaded file, for example
<?php
$config = [
'file_name' => $_FILES['upload_file']['name'],
];
?>
Specified this argument with original name of the uploaded file ONLY when you prefer template to be processed without being uploaded to server. |
|
file_prefix | File name prefix during output. | 'Template' |
sheet_name | Excel/Spreadsheet sheet name that to be convert. | 'template' |
data | Associative array data which will be used for variable substitution in [variable => value] format. | [ ] |
Methods
Method | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getPhpOfficeObject() | Return PhpOfficeTemplate object. | ||||||||||||||||||||||||
setPhpOfficeObject() | Set/Update PhpOfficeTemplate object. | ||||||||||||||||||||||||
setPdfRenderer($pdf_renderer) | Change PDF renderer.
Available options :
|
||||||||||||||||||||||||
setOrientation($orientation) | Change page orientation. (ONLY for Excel/Spreadsheet)
Available options : (Default follow file orientation)
|
||||||||||||||||||||||||
output([$method, $type, $link]) |
Output the result.
$method options :
$link options :
|
Advance Usage
Merge Files [Excel/Word/PDF]
You can enhance the usage to support multiple files (different file types) upload and output as one PDF file (combine into one PDF file). In order to do that, you need to include "PhpOfficeMerger.php" and write some file processing coding (convert non-PDF to PDF) before merging :
Caution!
PDFMerger comes with TCPDF by default, so does PhpSpreadsheet. As a result, it may cause a conflict and result in merging failure. To resolve this issue, kindly follow the instructions below :
- Go to you PDFMerger source file : path-fo-PDFMerger-folder/src
- Open PDFMerger.php using any editor.
- Comment line 48 :
require_once('tcpdf/tcpdf.php');
to become// require_once('tcpdf/tcpdf.php');
to prevent fatal error of duplicate require of TCPDF library issue.
Running Demo
Before running the demo through your server, kindly follow the instructions below step by step :
- Once you had installed the library, please go to demo/ folder within the library and create a folder
named 'uploaded_template' :
# cd <path-to-your-project>/phpofficetemplate/demo
# mkdir uploaded_template - Change owner of 'uploaded_template' folder :
# chown <your-user-group>:apache upload_template
Note!
You may need Admin User Privilege in order to run this command.
- Change mod of 'uploaded_template' folder :
# mod 775 upload_template
Note!
You may need Admin User Privilege in order to run this command.
- Enter the URL into your browser : '<path-to-your-project>/phpofficetemplate/demo'.