{"id":23529885,"url":"https://github.com/mnvx/eloquent-print-form","last_synced_at":"2025-04-22T17:24:29.553Z","repository":{"id":57018546,"uuid":"311619762","full_name":"mnvx/eloquent-print-form","owner":"mnvx","description":null,"archived":false,"fork":false,"pushed_at":"2024-01-25T18:19:26.000Z","size":23,"stargazers_count":30,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T17:24:14.178Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mnvx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-10T10:14:39.000Z","updated_at":"2024-04-20T16:32:46.000Z","dependencies_parsed_at":"2024-12-25T21:14:18.141Z","dependency_job_id":"f0c89a7b-6f9c-4232-88b9-0cd5d4c7f03b","html_url":"https://github.com/mnvx/eloquent-print-form","commit_stats":{"total_commits":5,"total_committers":1,"mean_commits":5.0,"dds":0.0,"last_synced_commit":"b9e0bbf0d41acda578009654065399005c073da1"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnvx%2Feloquent-print-form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnvx%2Feloquent-print-form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnvx%2Feloquent-print-form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnvx%2Feloquent-print-form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mnvx","download_url":"https://codeload.github.com/mnvx/eloquent-print-form/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250285933,"owners_count":21405335,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-25T21:14:07.539Z","updated_at":"2025-04-22T17:24:29.505Z","avatar_url":"https://github.com/mnvx.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Print forms for Eloquent\n\nEasy way to generate docx print forms of invoices, contracts and other documents.\n\n## Usage\n\nInstallation\n\n```bash\ncomposer require mnvx/eloquent-print-form\n```\n\nDeclare models for Eloquent.\n\nCreate print form (docx file) using next syntax.\n\n- `${field_name}` - standard field\n- `${entity.field_name}` - field from linked entity\n- `${entity.field_name|date}` - format field as date\n- `${entity.field_name|date|placeholder}` - format field as date and replace to \"____________________\" if field is empty\n- `${entity1.entity2.field_name}` - field from linked entity through one table\n- `${entities.field_name}` - for table data\n\nAs you may see, it is possible to use pipes, for example: `|date`, `|date|placeholder`. Supported pipes are:\n- `placeholder` - replaces empty variable to \"____________________\".\n- `date` - format date\n- `dateTime` - format date time\n- `int` - format int value\n- `decimal` - format decimal value\n\nIt is possible also to specify custom pipes.\n\nGenerate print form for specified entity\n\n```php\n$entity = Contract::find($id);\n$printFormProcessor = new PrintFormProcessor();\n$templateFile = resource_path('path_to_print_forms/your_print_form.docx');\n$tempFileName = $printFormProcessor-\u003eprocess($templateFile, $entity);\n```\n\n## Examples \n\n### Basic example\n\nFor example, if there are next models in project.\n\n```php\nuse Illuminate\\Database\\Eloquent\\Model;\n\n/**\n * @property string $number\n * @property string $start_at\n * @property int $customer_id\n * @property Customer $customer\n * @property ContractAppendix[] $appendixes\n */\nclass Contract extends Model\n{\n\n    public function customer()\n    {\n        return $this-\u003ebelongsTo(Customer::class);\n    }\n \n    public function appendixes()\n    {\n        return $this-\u003ehasMany(ContractAppendix::class);\n    }\n}\n\n/**\n * @property string $name\n * @property CustomerCategory $category\n */\nclass Customer extends Model\n{\n    public function category()\n    {\n        return $this-\u003ebelongsTo(CustomerCategory::class);\n    }\n\n}\n\n/**\n * @property string $number\n * @property string $date\n * @property float $tax\n */\nclass ContractAppendix extends Model\n{\n\n    public function items()\n    {\n        return $this-\u003ehasMany(ContractAppendixItem::class, 'appendix_id');\n    }\n\n    public function getTaxAttribute()\n    {\n        $tax = 0;\n        foreach ($this-\u003eitems as $item) {\n            $tax += $item-\u003etotal_amount * (1 - 100 / (100+($item-\u003etaxStatus-\u003evat_rate ?? 0)));\n        }\n        return $tax;\n    }\n}\n```\n\nExample of Laravel's controller action for printing contract.\n\n```php\npublic function downloadPrintForm(FormRequest $request)\n{\n    $id = $request-\u003eget('id');\n    $entity = Contract::find($id);\n    $printFormProcessor = new PrintFormProcessor();\n    $templateFile = resource_path('path_to_print_forms/your_print_form.docx');\n    $tempFileName = $printFormProcessor-\u003eprocess($templateFile, $entity);\n    $filename = 'contract_' . $id;\n    return response()\n        -\u003edownload($tempFileName, $filename . '.docx')\n        -\u003edeleteFileAfterSend();\n}\n```\n\nExample of docx template\n\n---\n\nContract number: ${number|placeholder}  \nContract date: ${start_at|date|placeholder}    \nCustomer: ${customer.name}  \nCustomer category: ${customer.category.name|placeholder}\n\nAppendixes:\n\n| Row number                | Appendix number      | Appendix tax      |\n| ------------------------- | -------------------- | -----------------:|\n| ${appendixes.#row_number} | ${appendixes.number} | ${appendixes.tax} |\n\n---\n\nExample of generated document\n\n---\n\nContract number: F-123  \nContract date: 12.04.2012    \nCustomer: IBM  \nCustomer category: ____________________ \n\nAppendixes:\n\n| Row number                | Appendix number      | Appendix tax      |\n| ------------------------- | -------------------- | -----------------:|\n| 1                         | A-1                  | 1234              |\n| 2                         | A-1.1                | 0                 |\n| 3                         | B-1                  | 10                |\n\n---\n\n### How to specify custom pipes\n\n```php\nclass CustomPipes extends \\Mnvx\\EloquentPrintForm\\Pipes\n{\n    // Override\n    public function placeholder($value)\n    {\n        return $value ?: \"NO DATA\";\n    }\n\n    // New pipe\n    public function custom($value)\n    {\n        return \"CUSTOM\";\n    }\n}\n\n$entity = Contract::find($id);\n$pipes = new CustomPipes();\n$printFormProcessor = new PrintFormProcessor($pipes);\n$templateFile = resource_path('path_to_print_forms/your_print_form.docx');\n$tempFileName = $printFormProcessor-\u003eprocess($templateFile, $entity);\n```\n\n### How to specify custom processors for some variables\n\nExample of custom processor for variable `${custom}`.\n\n```php\n$tempFileName = $printFormProcessor-\u003eprocess($templateFile, $entity, [\n    'custom' =\u003e function(TemplateProcessor $processor, string $variable, ?string $value) {\n        $inline = new TextRun();\n        $inline-\u003eaddText('by a red italic text', array('italic' =\u003e true, 'color' =\u003e 'red'));\n        $processor-\u003esetComplexValue($variable, $inline);\n    },\n]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnvx%2Feloquent-print-form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmnvx%2Feloquent-print-form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnvx%2Feloquent-print-form/lists"}