kartik-v / yii2-report
一个用于生成使用 MS Word 模板在 PDF/DOC/DOCX 格式下美观格式的报告的 Yii2 组件
This package is auto-updated.
Last update: 2024-08-25 18:55:24 UTC
README
yii2-report
一个用于生成使用 Microsoft Word 文档模板在 PDF/DOC/DOCX 格式下美观格式的报告的 Yii2 组件。该组件使用 PHP reports library API 来生成报告。PHP-Reports 是一个基于云的、交互式报告引擎,它可以帮助从 Word / DOCX 模板生成格式良好的 PDF 报告。
如何通过 pull request 贡献?
请参阅此 贡献者 git 工作流程。
安装
安装此扩展的首选方法是使用 composer。
先决条件
注意:请检查此扩展的composer.json 文件中的要求和依赖项。在安装此扩展之前,您必须在您的应用程序根目录中的 composer.json 文件中将
minimum-stability
设置为dev
。或者,如果您将minimum-stability
设置为除dev
之外的任何值,请在您的 composer.json 文件的 require 部分设置以下内容
kartik-v/yii2-report: "@dev"
请参阅有关设置应用程序 composer.json 的 minimum-stability
设置的 网络提示 /wiki。
安装
运行以下命令之一:
$ php composer.phar require kartik-v/yii2-report "@dev"
或添加
"kartik-v/yii2-report": "@dev"
到您的 composer.json 文件的 require
部分。
用法
步骤 1: 获取 API 密钥
在 https://www.php-reports.com 上创建免费账户并获取您的免费 API 密钥。
步骤 2: 创建您的第一个模板
为了加速创建报告和应用的过程,PHP Reports 利用 Microsoft Word 的设计功能。只需创建一个 Microsoft Word 文件并设计您的报告。
步骤 3: 使用模板变量
您可以在您的 Microsoft Word 模板中使用模板变量。模板变量的名称来自它们双大括号内的内容,并且它们可以稍后用具体值替换。
模板字符串内的所有模板变量名称都必须是唯一的。模板变量名称不区分大小写。
步骤 4: 上传您的模板
在 https://www.php-reports.com 的 "模板管理器" 部分保存并上传您的模板。请记住模板标识符(templateId
)。
步骤 5: 设置 yii2-report
全局组件
在您的 Yii2 应用程序配置文件的 components
部分设置以下组件。
use kartik\report\Report; // ... 'components' => [ // setup Krajee Yii2 Report component 'report' => [ 'class' => Report::classname(), 'apiKey' => 'YOUR_PHP_REPORTS_API_KEY', // the following variables can be set to globally default your settings 'templateId' => 1, // optional: the numeric identifier for your default global template 'outputAction' => Report::ACTION_FORCE_DOWNLOAD, // or Report::ACTION_GET_DOWNLOAD_URL 'outputFileType' => Report::OUTPUT_PDF, // or Report::OUTPUT_DOCX 'outputFileName' => 'KrajeeReport.pdf', // a default file name if 'defaultTemplateVariables' => [ // any default data you desire to always default 'companyName' => 'Krajee.com' ] ] ]
步骤 6: 从组件生成报告
use kartik\report\Report; $report = Yii::$app->report; // set your template identifier (override global defaults) $report->templateId = 2; // If you want to override the output file name, uncomment line below // $report->outputFileName = 'My_Generated_Report.pdf'; // If you want to override the output file type, uncomment line below // $report->outputFileType = Report::OUTPUT_DOCX; // If you want to override the output file action, uncomment line below // $report->outputFileAction = Report::ACTION_GET_DOWNLOAD_URL; // Configure your report data. Each of the keys must match the template // variables set in your MS Word template and each value will be the // evaluated to replace the Word template variable. If the value is an // array, it will treated as tabular data. $report->templateVariables = [ 'client_name' => 'Murat Cileli', 'address' => 'Kadikoy, Istanbul / Turkey', 'date' => '10-Apr-2018', 'phone' => '+1-800-3399622', 'email' => 'admin@gmail.com', 'notes' => 'Thank you for your purchase.', 'quantities' => ['6', '3', '4'], 'products' => ['Apple iPhone 5S', 'Samsung Galaxy S5', 'Office 365 License'], 'prices' => ['490 USD', '399 USD', '199 USD'], ]; // lastly in your controller action download the generated report return $report->generateReport();
步骤 7: 检查您的输出报告文档
您的输出应类似于以下内容
许可
yii2-report 根据 BSD-3-Clause 许可证发布。有关详细信息,请参阅附带 LICENSE.md
文件。