spirit-of-wars/docmvc
Composer 安装命令:
composer require spirit-of-wars/docmvc
包简介
DocMVC, MVC-library for generating files (excel, doc, pdf)
README 文档
README
The library is designed to work with files PDF, Excel, Word. This one is micro framework about mvc-model
General provisions:
- To get started you need create instance DocumentManager then download class-cartridge for him
- Working with data, customizing paths and required parameters must be done only in class-cartridge
- There are two ways to generate a document: render document from scratch (using view), render document from existed document (template)
- Document rendering logic must be written only in the view file (view)
- External dependencies are used to generate view-logic. The processors object of these libraries is available in
view file via a variable $driver
- Doc/Docx - phpoffice/phpword
- Excel - phpoffice/phpspreadsheet
- Pdf - dompdf/dompdf - Template is a blank document that does not yet contain the necessary data, but they will be substituted by the generation process. Such data is defined only in the model. (Example: To generate an excel document, we created a template.xlsx blank then inside in curly brackets we specify variables from where to take values)
To get started you need:
- Create a shared directory where everything will be stored. I am using a directory called 'document'
- Inside this one you need create one more directory (arbitrary name). Now each such directory will be responsible for its own document.
- In this directory you need to create classes-cartridges. For example, I need to make a financial report document in doc and pdf formats. For this i will create directory Report/ in the directory Document/ In the Document/ directory i will create two files: Doc.php (extended from SpiritOfWars\DocMVC\Cartridge\DocCartridge) and Pdf.php (SpiritOfWars\DocMVC\Cartridge\PdfCartridge)
- Each cartridge have 1 abstract method, that must be defined:
- setupView - must be return string, that contains name and path from view-file. CANNOT BE EMPTY! View file required must be created and the path to it must be specified Also each cartridge have 3 optional methods:
- setupModel - must be return array data. This data will be use in view-file or filling template variables
- setupTemplate - must be return string, that contains name and path from template-file. (To pdf-cartridge is not available)
- setupRequiredParams - must be return array of required params, that you want to pass to constructor. We specify only those parameters, without which the generation of the document is impossible. If there are none, do not touch this method
- Create directory view/ (in my example: Document/Report/view/)
- Create view-file with any name (in my example: i created Document/Report/view/view-pdf.php to pdf document)
- Set path to view-file in the cartridges method setupView (in my example: return 'view-pdf.php';). Caution! Path must be relative.
- If you need generating by template, just repeat steps 6,7,8 for template names. Directory for created must be named template/. Set path to template-document in the cartridges method setupTemplate. Caution! Path must be relative.
- The view file is intended for html layout, or its generation by $driver variable. Available by default two variables $driver - an object to work with the generation of document content, $model - an array with the data you specified in method setupModel
- In the right place in your code, we create an instance of our created cartridge, pass an array of parameters to the constructor, which will be available through the property $this->params (in my example $docObj = new(\Document\Report\Doc(['testKey' => 'testValue'])))
- Create instance of DocumentManager: new SpiritOfWars\DocMVC\DocumentManager($docObj) . Optional: logger object can be passed as second parameter
- Build document $docObj->build();
- Now the generated document available for download or saving to directory. Methods: saveAs(), download()
A few important notes:
- Templates are not available for pdf documents
- While downloading the document, make sure that nothing extra is displayed on the page. Because all this will also be generated document. Which can even lead to its unreadability
- By default, the name of the downloaded document will be of the form timestamp().{extension}. You need override method setupDocName() to change name. The name must not contain a file extension.
- Each class has its own default document extension. You can change it by overriding method setupFileExt. However, the extension you specify in it must be on the list of allowed (method allowedExt)
- The /sample/ folder contains examples of working with documents
- If you need to pass data from a method to a method, you can use the $this->commonData property
Code Examples: Generating docx-document for download
Class-cartridge Doc.php, is in the directory document/test/Doc.php
use SpiritOfWars\DocMVC\Cartridge\DocCartridge; class Doc extends DocCartridge { public function setupView() { return 'doc/view.php'; } public function setupModel() { $test = $this->params['test']; // required param $randParam = $this->params['test2']; return [ 'test' => $test, 'randParam' => $randParam ]; } public function setupRequiredParams() { return ['test']; } public function setupDocName() { return 'test-name'; } }
View-file view.php is in the directory document/test/view/view.php
$PHPWord = $driver; $data = $model; $PHPWord->addFontStyle('nStyle', array('size'=>10,'name'=>'Arial CYR')); $PHPWord->addParagraphStyle('pRight', array('align'=>'right','spaceAfter'=>0)); $PHPWord->addParagraphStyle('pRightT', array('align'=>'right','spaceBefore'=>400,'spaceAfter'=>0)); $PHPWord->addParagraphStyle('pRightB', array('align'=>'right','spaceAfter'=>400,'spaceAfter'=>0)); $PHPWord->addParagraphStyle('pCenter', array('align'=>'center','spaceAfter'=>0)); $PHPWord->addParagraphStyle('pLeft', array('align'=>'left','spaceAfter'=>0)); $PHPWord->addParagraphStyle('pLeftT', array('align'=>'left','spaceBefore'=>400,'spaceAfter'=>0)); $PHPWord->setDefaultFontName('Arial CYR'); $PHPWord->setDefaultFontSize(10); $section = $PHPWord->createSection(array('marginLeft'=>1100, 'marginRight'=>1100, 'marginTop'=>1100, 'marginBottom'=>1100)); $section->addText('Test text', 'nStyle', 'pRight'); $section->addText('Test text №2', 'nStyle', 'pRight'); $textrun = $section->createTextRun('pRight'); $textrun->addText($data['test']); $textrun->addText($data['randParam']);
Creating instance of class, passing params for it then download result document
$testDoc = new Doc([ 'test' => 'test content', 'randParam' => 'random param' ]); $documentManager = new DocumentManager($testDoc); $documentManager->build()->download();
spirit-of-wars/docmvc 适用场景与选型建议
spirit-of-wars/docmvc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 12 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「pdf」 「doc」 「files」 「download」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spirit-of-wars/docmvc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spirit-of-wars/docmvc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spirit-of-wars/docmvc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Yii2 export extension
PHP Excel Library
A tool for display docs or api docs to others
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-2.1-or-later
- 更新时间: 2018-12-25