Documentation Index
Fetch the complete documentation index at: https://docs.mejik.web.id/llms.txt
Use this file to discover all available pages before exploring further.
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 versatiledocx:generate command to create various document types from a single .docx template.
1. Setup and Installation
- Prerequisites:
- A working Laravel project.
- Composer installed.
- The
php-zipPHP extension must be enabled.
- Install PHPWord:
- PDF Generation Prerequisite:
To use
pdfmode, you must also install a PDF rendering library.
2. Command Usage
Command Signature:--template: Path to the input.docxtemplate 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 usingblademode).--mode: The operation mode. Default:merge.merge: Merges JSON data into the template, creating a new.docxfile.blade: Converts the.docxtemplate into a basic.blade.phpfile.html: Merges JSON data into the template, creating an.htmlfile.pdf: Merges JSON data and converts the result to a.pdffile.
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.docxfile, preserving all formatting. -
html: This mode first merges the data just like themergemode, 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/dompdffirst. - 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.docxtemplate and converts its structure and placeholders into a.blade.phpfile. -
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 serbagunadocx:generate untuk membuat berbagai jenis dokumen dari satu templat .docx.
1. Pengaturan dan Instalasi
- Prasyarat:
- Proyek Laravel yang sudah berjalan.
- Composer sudah terinstal.
- Ekstensi PHP
php-zipharus diaktifkan.
- Instal PHPWord:
- Prasyarat Mode PDF:
Untuk menggunakan mode
pdf, Anda juga harus menginstal library rendering PDF.
2. Penggunaan Perintah
Struktur Perintah:--template: Path ke file templat.docxinput.--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 modeblade).--mode: Mode operasi. Default:merge.merge: Menggabungkan data JSON ke templat, menghasilkan file.docxbaru.blade: Mengonversi templat.docxmenjadi file.blade.phpdasar.html: Menggabungkan data JSON ke templat, menghasilkan file.html.pdf: Menggabungkan data dan mengonversi hasilnya menjadi file.pdf.
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.docxyang sudah terisi, dengan mempertahankan semua format asli. -
html: Mode ini pertama-tama menggabungkan data seperti modemerge, 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/dompdfterlebih 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.docxAnda 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.
