Download the PHP package santwer/exporter without Composer
On this page you can find all versions of the php package santwer/exporter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download santwer/exporter
More information about santwer/exporter
Files in santwer/exporter
Package exporter
Short Description Laravel Package for easy Word exports in Laravel on given Templates. This package is based on PHPWord.
License MIT
Informations about the package exporter
Word Template Exporter
Package for easy Word exports in Laravel on given Templates. This package is based on phpoffice/phpword.
Table of Contents
- Installation
- Configuration
- How to use with ExportClasses
- Usage
- Creating a New Export
- Example
- Charts
- Images
- Tables
- How to use in Query
- Basic Export
- Export as PDF
- Autoloading Relations
- Variables
- Template Variables/Blocks
- Relation Variable with Condition
- Template
Installation
Exporter is installed via Composer. To add a dependency to Exporter in your project, either
Run the following to use the latest stable version
or if you want the latest master version
You can of course also manually edit your composer.json file
Configuration (optional)
To use pdf export it is needed to install LibreOffice. WordTemplateExporter is using the soffice command to convert the docx file to pdf.
Windows
Download and install LibreOffice from here Also add the path to the soffice command to the system environment variables.
How to use with ExportClasses
Usage
You can use the WordExporter Facade as follows. The format of the exported file is determined by the file extension. Supported formats are .docx and .pdf.
Creating a New Export
You can create a new export using the following Artisan command:
Replace {className} with the name of the new export class.
Interfaces
The object $export can be implemented with the following interfaces:
Interface | Description | Example |
---|---|---|
FromWordTemplate |
Required. Interface indicating the export is from a Word template. | class MyExportClass implements FromWordTemplate |
GlobalTokens |
Interface for providing global tokens for replacement in Word template. | class MyGlobalTokens implements GlobalTokens |
TokensFromArray |
Interface for providing tokens from an array for replacement in Word template. | class MyArrayTokens implements TokensFromArray |
TokensFromCollection |
Interface for providing tokens from a collection for replacement in Word template. | class MyCollectionTokens implements TokensFromCollection |
TokensArray |
Interface for providing tokens from an array without any block data | |
TokensFromObject |
Interface for providing tokens from an object/class without any block data | |
TokensFromModel |
Interface for prodiding tokens from a model without any block data | |
WithCharts |
Interface that allows you to replace text charts as array | |
WithCheckboxes |
Interfaces that allows you to replace text with Checkboxes, either checked or not checked | |
WithImages |
Interface that allows you to replace text with Images |
Each of these interfaces defines methods that need to be implemented according to the specific requirements of the export process. These methods typically involve returning an array of key-value pairs where keys represent placeholders in the Word template and values are the data to replace those placeholders with.
Example
Word file:
Controller:
Export Class:
Charts
To replace a chart in a Word template, you can use the WithCharts
interface. This interface requires the implementation of the charts
method, which should return an array of key-value pairs where keys represent placeholders in the Word template and values are the data to replace those placeholders with.
You can find all infos about the charts here
Possible types are 'pie', 'doughnut', 'line', 'bar', 'stacked_bar', 'percent_stacked_bar', 'column', 'stacked_column', 'percent_stacked_column', 'area', 'radar', 'scatter'
Images
To replace an image in a Word template, you can use the WithImages
interface. This interface requires the implementation of the images
method, which should return an array of key-value pairs where keys represent placeholders in the Word template and values are the data to replace those placeholders with.
For more Details how to set Images you can find here
Tables
To replace a table in a Word template, you can use the WithTables
interface. This interface requires the implementation of the tables
method, which should return an array of key-value pairs where keys represent placeholders in the Word template and values are the data to replace those placeholders with.
Note: For export in pdf at least the headers need the width of the columns. Not settet column widths might not be shown.
How to use in Query
Add Trait Exportable
As default all Variables are available of the Model. Add a Concern for special export fields.
You can add a fixed Template for each Model.
You can also define own Blocknames for the use of the Model in a template.
Basic Export
export() gives download response back.
If you defined export template in Model.
It's also possible to set Export-Command after Executing the query or on a Model after Find-Command
Export as PDF
Generally with the option format = pdf it is possible to export pdf. It is important that libreOffice is installed for that actions.
For short terms it is possible to call Export functions as PDF
Autoloading Relations
Before exporting, the Package is checking for defined Relations. If there is no related Variable it will automatically remove unneeded relations. This behavior can be changed within the config. For that it is needed to set up a config File exporter.php in config/
Also is the Package checking for relations that are not loaded yet. It will automatically load the Relations before exporting. Therefore, it is possible to reduce the Export-Code from
to
If the Relation is already loaded it will not be affected.
Variables
It is possible to set up variables which are not affected by the Model or Query.
Template Variables/Blocks
In the template the package always looks for loops/Blocks (except for Global Variables). By Default the Blockname is the name of the table. It is also possible to use an own name for that.
To export Customers with Name and e-mail addresses, it is needed to add the Block.
If there is a Relation within the customer.
If there is a Relation with a collection of Entries.
For each Relation it will add up its relation block name.
Relation Variable with Condition
It is possible to define Variables which are Related to many Entries. Therefore, you can reduce it to one relation and get a certain Value in the relation.
It will only select one entry.
However, you can set up one where condition to get the entry.
If the Entry is not found the Values of the Model will be null.
Template
The Template should be DOCX or DOC. The File will be cloned and saved in the sys_temp_folder as long it has no store option. For PDF exports it is needed to use LibreOffice. Therefore, the soffice command needs to be executable.
For the Templateprocessing it uses phpoffice/phpword More Infos you can find here
All versions of exporter with dependencies
php Version ^7.4|^8.0|^8.2|^8.2
phpoffice/phpword Version ^v1.2