Skip to main content

Prerequisites for PDF Mode

For the PDF conversion to work, you must install a PDF rendering library. We will use DomPDF as it’s a popular choice. Run this command in your project root:

Final Code: app/Console/Commands/DocxGenerateCommand.php

First, rename your command file from GenerateInvoiceFromTemplate.php to a more generic DocxGenerateCommand.php to reflect its new capabilities. Then, replace its entire content with the following code.

English Documentation

DOCX Universal Document Generator Guide

This guide explains how to use the versatile docx:generate command to create various document types from a single .docx template.

1. Setup and Installation

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

2. Command Usage

Command Signature:
Options:
  • --template: Path to the input .docx template file.
  • --output: Base path for the output file. The correct extension (.docx, .html, etc.) will be added automatically. Default: storage/app/output/result.
  • --data: Path to the JSON data file. (This is ignored when using blade mode).
  • --mode: The operation mode. Default: merge.
  • merge: Merges JSON data into the template, creating a new .docx file.
  • blade: Converts the .docx template into a basic .blade.php file.
  • html: Merges JSON data into the template, creating an .html file.
  • pdf: Merges JSON 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}, ${block}..${/block}, and table row cloning.)

4. Mode Explanations

  • merge (default): This is the standard operation. It takes your data and template and produces a filled-out .docx file, preserving all formatting.
  • html: This mode first merges the data just like the merge mode, but then converts the resulting document to HTML.
  • Note: The resulting HTML will use extensive inline CSS to mimic the DOCX formatting. It is useful for displaying in a browser but may be complex to edit manually.
  • pdf: This mode performs the data merge and then uses a rendering engine (like DomPDF) to convert the document to PDF.
  • Requirement: You must run composer require dompdf/dompdf first.
  • Note: Conversion quality is best for simpler documents. Complex layouts with floating images or intricate tables may not render perfectly.
  • blade: This mode ignores the data file. It reads your .docx template and converts its structure and placeholders into a .blade.php file.
  • Placeholders like ${variable_name} are converted to {{ $variable_name }}.
  • This is an excellent tool for quickly scaffolding a web view that looks similar to your Word document. The generated HTML/CSS will be complex and is best used as a starting point for further refinement.

Dokumentasi Bahasa Indonesia

Panduan Generator Dokumen Universal DOCX

Panduan ini menjelaskan cara menggunakan perintah serbaguna docx:generate untuk membuat berbagai jenis dokumen dari satu templat .docx.

1. Pengaturan dan Instalasi

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

2. Penggunaan Perintah

Struktur Perintah:
Opsi:
  • --template: Path ke file templat .docx input.
  • --output: Path dasar untuk file output. Ekstensi yang benar (.docx, .html, dll.) akan ditambahkan secara otomatis. Default: storage/app/output/result.
  • --data: Path ke file data JSON. (Opsi ini diabaikan saat menggunakan mode blade).
  • --mode: Mode operasi. Default: merge.
  • merge: Menggabungkan data JSON ke templat, menghasilkan file .docx baru.
  • blade: Mengonversi templat .docx menjadi file .blade.php dasar.
  • html: Menggabungkan data JSON ke templat, menghasilkan file .html.
  • pdf: Menggabungkan data dan mengonversi hasilnya menjadi file .pdf.
Contoh:

3. Referensi Tag Templat

(Bagian ini tetap sama seperti dokumentasi sebelumnya, menjelaskan ${variabel}, ${blok}..${/blok}, dan perulangan baris tabel.)

4. Penjelasan Mode

  • merge (default): Ini adalah operasi standar. Perintah ini mengambil data dan templat Anda, lalu menghasilkan file .docx yang sudah terisi, dengan mempertahankan semua format asli.
  • html: Mode ini pertama-tama menggabungkan data seperti mode merge, tetapi kemudian mengonversi dokumen yang dihasilkan ke format HTML.
  • Catatan: HTML yang dihasilkan akan menggunakan banyak inline CSS untuk meniru format DOCX. Ini berguna untuk ditampilkan di browser tetapi mungkin rumit untuk diedit secara manual.
  • pdf: Mode ini melakukan penggabungan data dan kemudian menggunakan mesin rendering (seperti DomPDF) untuk mengonversi dokumen menjadi PDF.
  • Kewajiban: Anda harus menjalankan composer require dompdf/dompdf terlebih dahulu.
  • Catatan: Kualitas konversi paling baik untuk dokumen yang lebih sederhana. Tata letak yang kompleks dengan gambar melayang atau tabel yang rumit mungkin tidak dirender dengan sempurna.
  • blade: Mode ini mengabaikan file data. Mode ini membaca templat .docx Anda dan mengonversi struktur serta placeholder-nya menjadi file .blade.php.
  • Placeholder seperti ${nama_variabel} diubah menjadi {{ $nama_variabel }}.
  • Ini adalah alat yang sangat baik untuk membuat kerangka (scaffolding) view web dengan cepat yang tampilannya mirip dengan dokumen Word Anda. HTML/CSS yang dihasilkan akan kompleks dan paling baik digunakan sebagai titik awal untuk disempurnakan lebih lanjut.