Laravel Packages

Generate Documents in PHP With an Excel Template

Published
Generate Documents in PHP With an Excel Template image

The AnourValar/office package is a PHP +8.1 library to generate documents from XLSX templates. Given a template like the following example template (from the readme):

Using the following code you can generate a document using the template values:

$data = [
// scalar
'vat' => 'No',
'total' => [
'price' => 2004.14,
'qty' => 3,
],
 
// one-dimensional table
'products' => [
[
'name' => 'Product #1',
'price' => 989,
'qty' => 1,
'date' => new \DateTime('2022-03-30'),
],
[
'name' => 'Product #2',
'price' => 1015.14,
'qty' => 2,
'date' => new \DateTime('2022-03-31'),
],
],
];
 
// Save as XLSX (Excel)
(new \AnourValar\Office\TemplateService())
->generate(
'template1.xlsx', // path to template
$data // input data
)
->saveAs(
'generated_document.xlsx', // path to save
\AnourValar\Office\Format::Xlsx // save format
);

Which produces the following based on the template1.xlsx file:

When saving the document, this package supports multiple save formats, including:

  • XLSX
  • PDF
  • HTML
  • ODS

Apart from generating files from XLSX templates, this package also supports:

  • Two-dimensional tables
  • Image insertion
  • Dynamic templates
  • Merge multiple documents into a single file
  • Export Table (grid service)

See the README for further details on how to use this package. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article