mnvx / eloquent-print-form
Eloquent的打印表单处理器
1.0.1
2020-11-10 10:36 UTC
Requires
- php: ^7.4
- illuminate/database: ^8.13
- nesbot/carbon: ^2.41
- phpoffice/phpword: ^0.17.0
README
轻松生成发票、合同和其他文件的docx打印表单。
用法
安装
composer require mnvx/eloquent-print-form
为Eloquent声明模型。
使用以下语法创建打印表单(docx文件)。
${field_name}
- 标准字段${entity.field_name}
- 链接实体的字段${entity.field_name|date}
- 将字段格式化为日期${entity.field_name|date|placeholder}
- 将字段格式化为日期,如果字段为空则替换为"____________________"${entity1.entity2.field_name}
- 通过一个表链接的实体的字段${entities.field_name}
- 用于表数据
如您所见,可以使用管道,例如: |date
、|date|placeholder
。支持以下管道:
placeholder
- 将空变量替换为"____________________"。date
- 格式化日期dateTime
- 格式化日期时间int
- 格式化整数值decimal
- 格式化十进制值
还可以指定自定义管道。
为指定实体生成打印表单
$entity = Contract::find($id); $printFormProcessor = new PrintFormProcessor(); $templateFile = resource_path('path_to_print_forms/your_print_form.docx'); $tempFileName = $printFormProcessor->process($templateFile, $entity);
示例
基本示例
例如,如果项目中有以下模型。
use Illuminate\Database\Eloquent\Model; /** * @property string $number * @property string $start_at * @property int $customer_id * @property Customer $customer * @property ContractAppendix[] $appendixes */ class Contract extends Model { public function customer() { return $this->belongsTo(Customer::class); } public function appendixes() { return $this->hasMany(ContractAppendix::class); } } /** * @property string $name * @property CustomerCategory $category */ class Customer extends Model { public function category() { return $this->belongsTo(CustomerCategory::class); } } /** * @property string $number * @property string $date * @property float $tax */ class ContractAppendix extends Model { public function items() { return $this->hasMany(ContractAppendixItem::class, 'appendix_id'); } public function getTaxAttribute() { $tax = 0; foreach ($this->items as $item) { $tax += $item->total_amount * (1 - 100 / (100+($item->taxStatus->vat_rate ?? 0))); } return $tax; } }
Laravel控制器动作的打印合同示例。
public function downloadPrintForm(FormRequest $request) { $id = $request->get('id'); $entity = Contract::find($id); $printFormProcessor = new PrintFormProcessor(); $templateFile = resource_path('path_to_print_forms/your_print_form.docx'); $tempFileName = $printFormProcessor->process($templateFile, $entity); $filename = 'contract_' . $id; return response() ->download($tempFileName, $filename . '.docx') ->deleteFileAfterSend(); }
docx模板示例
合同编号:${number|placeholder}
合同日期:${start_at|date|placeholder}
客户:${customer.name}
客户类别:${customer.category.name|placeholder}
附件
生成的文档示例
合同编号:F-123
合同日期:12.04.2012
客户:IBM
客户类别:____________________
附件
如何指定自定义管道
class CustomPipes extends \Mnvx\EloquentPrintForm\Pipes { // Override public function placeholder($value) { return $value ?: "NO DATA"; } // New pipe public function custom($value) { return "CUSTOM"; } } $entity = Contract::find($id); $pipes = new CustomPipes(); $printFormProcessor = new PrintFormProcessor($pipes); $templateFile = resource_path('path_to_print_forms/your_print_form.docx'); $tempFileName = $printFormProcessor->process($templateFile, $entity);
如何指定某些变量的自定义处理器
变量${custom}
的自定义处理器示例。
$tempFileName = $printFormProcessor->process($templateFile, $entity, [ 'custom' => function(TemplateProcessor $processor, string $variable, ?string $value) { $inline = new TextRun(); $inline->addText('by a red italic text', array('italic' => true, 'color' => 'red')); $processor->setComplexValue($variable, $inline); }, ]);