Skip to main content

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

This is the complete and final code for your Artisan command. It includes logic to handle images from both URLs and local file paths.

English Documentation

DOCX Invoice Generator Guide

This guide explains how to use the Laravel Artisan command to generate .docx invoices from a template and a JSON data file.

1. Setup and Installation

  1. Prerequisites:
  • A working Laravel project.
  • Composer installed.
  • The php-zip PHP extension must be enabled on your server.
  1. Install PHPWord: Navigate to your project’s root and run:
  1. File Structure:
  • Command: app/Console/Commands/GenerateInvoiceFromTemplate.php (place the code above here).
  • Template: storage/app/templates/invoice_template.docx
  • Data: invoice_data.json (in the project root, or specify a path).
  • Output: storage/app/invoices/ (will be created automatically).

2. Command Usage

The command signature defines how you run the command and what options are available. Command Signature:
Options:
  • --data: Path to the JSON data file. Default: invoice_data.json.
  • --template: Path to the .docx template file. Default: storage/app/templates/invoice_template.docx.
  • --output: Path where the final .docx will be saved. Default: storage/app/invoices/invoice_output.docx.
Examples:
  • Run with defaults:
  • Specify custom paths:

3. Template Tag Reference

Use these tags inside your .docx template file. Important: All formatting (bold, color, size) applied to a tag in the template will be inherited by the replacement data. 3.1. Simple Variables Replaces a tag with a string value.
  • Syntax: ${variable_name}
  • Example Template: Invoice Number: ${invoice_number}
  • Example JSON:
3.2. Image Insertion Replaces a tag with an image.
  • Syntax: ${image_placeholder}
  • Example Template: Place a ${logo} tag at the top of your document.
  • Usage (from URL): The system downloads the image and places it.
  • Usage (from Local File): The system uses an image from your server’s filesystem. Use an absolute path.
3.3. Conditional Blocks Shows or hides a block of content based on a boolean value.
  • Syntax:
  • Example Template:
  • Example JSON: If bill_present is true, the block is shown and its internal tags are processed. If false or missing, the entire block is removed.
3.4. Table Row Looping Dynamically creates table rows from an array of objects.
  • Syntax: Create a table in your .docx file with a single row meant for templating. Place tags like ${item.name} inside its cells.
  • Example Template Table Row:
  • How it Works: The code finds the row containing ${item.no} (or any variable you choose as the anchor) and duplicates it for every object in the item_list array.
  • Example JSON:
3.5. Simple Lists (from Array) Converts a simple array of strings into a single comma-separated string.
  • Syntax: ${variable_name}
  • Example Template: CC: ${cc_list}
  • How it Works: The code takes the array and joins its elements with ”, ”.
  • Example JSON:

Dokumentasi Bahasa Indonesia

Panduan Generator Faktur DOCX

Panduan ini menjelaskan cara menggunakan perintah Artisan Laravel untuk menghasilkan faktur .docx dari sebuah templat dan file data JSON.

1. Pengaturan dan Instalasi

  1. Prasyarat:
  • Proyek Laravel yang sudah berjalan.
  • Composer sudah terinstal.
  • Ekstensi PHP php-zip harus diaktifkan di server Anda.
  1. Instal PHPWord: Masuk ke direktori root proyek Anda dan jalankan:
  1. Struktur File:
  • Perintah (Command): app/Console/Commands/GenerateInvoiceFromTemplate.php (letakkan kode di atas di sini).
  • Templat: storage/app/templates/invoice_template.docx
  • Data: invoice_data.json (di root proyek, atau tentukan path lain).
  • Output: storage/app/invoices/ (folder akan dibuat secara otomatis).

2. Penggunaan Perintah

Struktur perintah (command signature) mendefinisikan cara Anda menjalankan perintah dan opsi apa saja yang tersedia. Struktur Perintah:
Opsi:
  • --data: Path ke file data JSON. Default: invoice_data.json.
  • --template: Path ke file templat .docx. Default: storage/app/templates/invoice_template.docx.
  • --output: Path tempat file .docx akhir akan disimpan. Default: storage/app/invoices/invoice_output.docx.
Contoh:
  • Jalankan dengan nilai default:
  • Tentukan path kustom:

3. Referensi Tag Templat

Gunakan tag-tag ini di dalam file templat .docx Anda. Penting: Semua format (tebal, warna, ukuran) yang diterapkan pada sebuah tag di templat akan diwarisi oleh data penggantinya. 3.1. Variabel Sederhana Mengganti sebuah tag dengan nilai string.
  • Sintaks: ${nama_variabel}
  • Contoh Templat: Nomor Faktur: ${invoice_number}
  • Contoh JSON:
3.2. Penyisipan Gambar Mengganti sebuah tag dengan gambar.
  • Sintaks: ${penanda_gambar}
  • Contoh Templat: Letakkan tag ${logo} di bagian atas dokumen Anda.
  • Penggunaan (dari URL): Sistem akan mengunduh gambar dan menempatkannya.
  • Penggunaan (dari File Lokal): Sistem akan menggunakan gambar dari filesystem server Anda. Gunakan path absolut.
3.3. Blok Kondisional Menampilkan atau menyembunyikan sebuah blok konten berdasarkan nilai boolean.
  • Sintaks:
  • Contoh Templat:
  • Contoh JSON: Jika bill_present bernilai true, blok akan ditampilkan dan tag di dalamnya akan diproses. Jika false atau tidak ada, seluruh blok akan dihapus.
3.4. Perulangan Baris Tabel Membuat baris tabel secara dinamis dari sebuah array objek.
  • Sintaks: Buat sebuah tabel di file .docx Anda dengan satu baris tunggal yang ditujukan sebagai templat. Letakkan tag seperti ${item.name} di dalam sel-selnya.
  • Contoh Baris Templat Tabel:
  • Cara Kerja: Kode akan menemukan baris yang berisi ${item.no} (atau variabel apa pun yang Anda pilih sebagai jangkar) dan menduplikasinya untuk setiap objek di dalam array item_list.
  • Contoh JSON:
3.5. Daftar Sederhana (dari Array) Mengubah sebuah array string sederhana menjadi satu string yang dipisahkan koma.
  • Sintaks: ${nama_variabel}
  • Contoh Templat: Tembusan (CC): ${cc_list}
  • Cara Kerja: Kode akan mengambil array dan menggabungkan elemen-elemennya dengan ”, ”.
  • Contoh JSON: