PHP code example of mnvx / eloquent-print-form

1. Go to this page and download the library: Download mnvx/eloquent-print-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mnvx / eloquent-print-form example snippets


$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;
    }
}

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();
}

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);

$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);
    },
]);