Bỏ qua, đến nội dung

@digiforce-nc/plugin-action-template-print

Plugin cung cấp in theo template (DOCX/XLSX) - admin upload template Word/Excel chứa placeholder, hệ thống render dữ liệu bản ghi vào template qua Carbone engine, hỗ trợ QR code và hình ảnh.

Plugin này làm gì?

Hãy hình dung bạn cần in hóa đơn, hợp đồng, phiếu xuất kho - mỗi loại có mẫu riêng với logo, bảng dữ liệu, QR code. Thay vì code từng mẫu, admin chỉ cần upload file DOCX/XLSX chứa placeholder (VD: {d.customerName}, {d.items[i].price}). Khi user nhấn in, server lấy dữ liệu bản ghi → render vào template qua Carbone engine → trả file kết quả.

Ba nhiệm vụ chính

#Nhiệm vụChi tiết
1Quản lý templateCollection printingTemplates lưu trữ template với metadata (collection, data source, format)
2Render dữ liệuCarbone engine thay placeholder bằng dữ liệu thực, hỗ trợ loop, condition, format
3QR code & hình ảnhTự động tạo QR code và nhúng hình ảnh từ attachment field vào kết quả

Kiến trúc

Tổng quan

Plugin có cả server (collection template, render pipeline, Carbone engine) lẫn client (UI quản lý template, nút action in). Template file lưu trên filesystem tại storage/print-templates/.

TầngVai tròThành phần
Client UIQuản lý template + nút action inTemplateManager, RecordActionWidget
Server CollectionCRUD metadata templateprintingTemplates
Render PipelineChuẩn bị data, resolve attachments, QRRender pipeline
Carbone EngineRender data vào template DOCX/XLSXcarbone
File StorageLưu file template gốcstorage/print-templates/

API endpoints

EndpointMethodMô tả
{collection}:templatePrintPOSTRender dữ liệu bản ghi vào template, trả file kết quả
printingTemplates:uploadPOSTUpload template mới
printingTemplates:downloadGETTải file template gốc
printingTemplates CRUDGET/POST/PUT/DELETEQuản trị metadata template (admin)

templatePrint - Request

Tham sốKiểuMô tả
filterByTkstring/numberID bản ghi cần in
templateNamestringTên template sử dụng

ACL - Phân quyền

QuyềnLoạiMô tả
ui.templatePrintSnippetACL snippet cho phép truy cập UI template print
templatePrintActionCho phép user đã đăng nhập thực hiện in
On installStrategyTự động thêm vào admin và member role strategies

Database

Bảng printingTemplates

CộtKiểuMô tả
namestringTên định danh template (unique)
titlestringTên hiển thị
collectionNamestringCollection mà template áp dụng
rootDataTypestringLoại dữ liệu gốc (single record / list)
dataSourcestringData source sử dụng
filenamestringTên file template trên filesystem
enabledbooleanTemplate có đang hoạt động

File storage

Template file được lưu tại storage/print-templates/ trên server filesystem. Khi upload template mới, file được copy vào thư mục này với tên duy nhất.


Luồng hoạt động

Carbone placeholder

Template sử dụng cú pháp Carbone cho placeholder:

Cú phápMô tảVí dụ
{d.field}Field đơn giản{d.customerName}
{d.items[i].field}Loop qua mảng{d.items[i].productName}
{d.field:formatN(2)}Format số{d.total:formatN(2)}
{d.field:formatD(DD/MM/YYYY)}Format ngày{d.createdAt:formatD(DD/MM/YYYY)}
{d.field:ifEQ(value)}...{d.field:showEnd}Điều kiệnHiện/ẩn block theo giá trị

Thành phần client

Thành phầnMô tả
TemplatePrintActionProviderProvider cho nút action template print
TemplatePrintRecordActionWidgetWidget nút in cho row action (in một bản ghi)
TemplatePrintCollectionActionWidgetWidget nút in cho collection action (in nhiều bản ghi)
TemplateManagerUIUI quản lý template: upload, xem danh sách, xóa, tải về
TemplatePrintDesignerCấu hình action trong designer (chọn template, format)

Dependencies

PackageVai trò
@digiforce-nc/actionsAction context types (peer)
@digiforce-nc/databaseDatabase ORM (peer)
@digiforce-nc/serverServer framework (peer)
@digiforce-nc/clientClient framework (peer)
@digiforce-nc/plugin-file-managerQuản lý file upload/storage (peer)
carboneTemplate render engine - DOCX/XLSX (dev)
qrcodeTạo QR code nhúng vào template (dev)
jsdomDOM manipulation cho image handling (dev)

Mục lục chi tiết