Skip to main content
For PDF conversion from XLSX, PhpSpreadsheet recommends using libraries like mPDF or TCPDF because they are better at handling the grid-like structure of a spreadsheet. We will use mPDF.

Step 1: Install Additional Dependencies

You already have phpoffice/phpspreadsheet. Now, add the PDF rendering library.
This is only required if you intend to use the --mode=pdf option.

Step 2: The Final Command Code

Replace the entire content of app/Console/Commands/XlsxGenerateCommand.php with this updated code. It now includes the mode-switching logic.

English Documentation (Updated)

XLSX Universal Spreadsheet Generator Guide

This guide explains how to use the xlsx:generate command to create .xlsx, .html, or .pdf files from a single XLSX template and a JSON data source.

1. Setup and Installation

  1. Prerequisites:
  • A working Laravel project.
  • Composer installed.
  • The php-zip PHP extension must be enabled.
  1. Install PhpSpreadsheet:
  1. PDF Generation Prerequisite: To use the pdf mode, you must also install the mPDF rendering library.

2. Command Usage

Command Signature:
Options:
  • --template: Path to the input .xlsx template file.
  • --output: Base path for the output file. The correct extension (.xlsx, .html, .pdf) will be added automatically.
  • --data: Path to the JSON data file.
  • --mode: The operation mode. Default: merge.
  • merge: Merges JSON data into the template, creating a new .xlsx file.
  • html: Merges data and converts the result to a single .html file.
  • pdf: Merges data and converts the result to a .pdf file.
Examples:

3. Template Tag Reference

(This section remains the same as the previous documentation, explaining ${variable} and the ${foreach:key} / ${item.key} syntax for loops.)

4. Mode Explanations

  • merge (default): This is the standard operation. It takes your data and template and produces a fully functional .xlsx file, preserving all cell styles, formulas, and formatting.
  • html: This mode first merges the data and then converts the resulting spreadsheet into a single HTML file with an HTML table. It’s excellent for embedding in web pages or for quick browser previews. All cell styling (colors, borders) is converted to inline CSS.
  • pdf: This mode performs the data merge and then uses the mPDF rendering engine to convert the spreadsheet to a PDF. This is ideal for generating printable reports.
  • Requirement: You must run composer require mpdf/mpdf first.
  • Note: The conversion works best for standard report layouts. Extremely wide spreadsheets may be scaled to fit the page, which could affect readability.

Dokumentasi Bahasa Indonesia (Diperbarui)

Panduan Generator Spreadsheet Universal XLSX

Panduan ini menjelaskan cara menggunakan perintah xlsx:generate untuk membuat file .xlsx, .html, atau .pdf dari satu templat XLSX dan sumber data JSON.

1. Pengaturan dan Instalasi

  1. Prasyarat:
  • Proyek Laravel yang sudah berjalan.
  • Composer sudah terinstal.
  • Ekstensi PHP php-zip harus diaktifkan.
  1. Instal PhpSpreadsheet:
  1. Prasyarat Mode PDF: Untuk menggunakan mode pdf, Anda juga harus menginstal library rendering mPDF.

2. Penggunaan Perintah

Struktur Perintah:
Opsi:
  • --template: Path ke file templat .xlsx input.
  • --output: Path dasar untuk file output. Ekstensi yang benar (.xlsx, .html, .pdf) akan ditambahkan secara otomatis.
  • --data: Path ke file data JSON.
  • --mode: Mode operasi. Default: merge.
  • merge: Menggabungkan data JSON ke templat, menghasilkan file .xlsx baru.
  • html: Menggabungkan data dan mengonversi hasilnya menjadi satu file .html.
  • pdf: Menggabungkan data dan mengonversi hasilnya menjadi file .pdf.
Contoh:

3. Referensi Tag Templat

(Bagian ini tetap sama seperti dokumentasi sebelumnya, menjelaskan sintaks ${variabel} dan ${foreach:key} / ${item.key} untuk perulangan.)

4. Penjelasan Mode

  • merge (default): Ini adalah operasi standar. Perintah ini mengambil data dan templat Anda dan menghasilkan file .xlsx yang fungsional, dengan mempertahankan semua gaya sel, formula, dan format.
  • html: Mode ini pertama-tama menggabungkan data, lalu mengonversi spreadsheet yang dihasilkan menjadi satu file HTML dengan tabel HTML. Mode ini sangat baik untuk disematkan di halaman web atau untuk pratinjau cepat di browser. Semua gaya sel (warna, border) diubah menjadi inline CSS.
  • pdf: Mode ini melakukan penggabungan data dan kemudian menggunakan mesin rendering mPDF untuk mengonversi spreadsheet menjadi PDF. Ini ideal untuk menghasilkan laporan yang dapat dicetak.
  • Kewajiban: Anda harus menjalankan composer require mpdf/mpdf terlebih dahulu.
  • Catatan: Konversi ini bekerja paling baik untuk tata letak laporan standar. Spreadsheet yang sangat lebar mungkin akan diskalakan agar sesuai dengan halaman, yang dapat memengaruhi keterbacaan.