stradaaccellog / phpjasper
PHP 报告生成器
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ^6.1
- squizlabs/php_codesniffer: ^3.1
README
PHPJasper
PHP 报告生成器
文档
关于
PHPJasper 是编译和处理 JasperReports (.jrxml & .jasper 文件) 的最佳解决方案,仅使用 PHP,简而言之:使用 PHP 生成报告。
注意
为什么选择 PHPJasper?
您是否曾需要为您的优秀网络应用创建具有大量字段的精美发票?
我需要,并且现有的解决方案并不完美。生成 HTML + CSS 来制作 PDF? 这没有任何意义! :)
然后我发现 JasperReports 是最好的开源报告解决方案。
我能用它做什么?
好吧,一切。JasperReports 是一个强大的报告和 BI 工具。
从他们的网站
JasperReports 库是全球最受欢迎的开源报告引擎。它完全用 Java 编写,能够使用来自任何类型数据源的数据,并生成像素完美的文档,可以在包括 HTML、PDF、Excel、OpenOffice 和 Word 在内的各种文档格式中查看、打印或导出。
建议使用 Jaspersoft Studio 来构建您的报告,将其连接到您的数据源(例如 MySQL、POSTGRES),遍历结果并将其输出到 PDF、XLS、DOC、RTF、ODF 等。
以下是一些您可以做的事情的示例
- 发票
- 报告
- 列表
需求
- PHP 7.0 或以上
- Java JDK 1.8
可选
- Mysql JDBC 驱动(如果您想使用数据库)
- PostgreSQL JDBC 驱动(如果您想使用数据库)
- Microsoft JDBC 驱动(如果您想使用数据库)
- Jaspersoft Studio(用于绘制和编译您的报告)
安装
如果您还没有安装,请安装 Composer。
composer require geekcom/phpjasper
或者,在您的文件 'composer.json' 中添加
{ "require": { "geekcom/phpjasper": "^3.0.0" } }
然后运行
composer install
就这么多。
PHPJasper 与 Docker
安装 Docker CE 和 docker-compose 后,只需运行
docker-compose up -d
docker exec -it phpjasper composer install
要执行测试
docker exec -it phpjasper ./vendor/bin/phpunit
或docker exec -it phpjasper ./vendor/bin/phpunit --testdox
要查看测试覆盖率,请执行文件: /tests/codeCoverage/html/dashboard.html
帮助我们编写新的测试,创建一个分支 :)
示例
《Hello World》示例。
前往存储库根目录中的示例目录(vendor/geekcom/phpjasper/examples
)。使用 Jaspersoft Studio 或您喜欢的文本编辑器打开 hello_world.jrxml
文件,查看源代码。
编译
首先我们需要将我们的 JRXML
文件编译成 JASPER
二进制文件。我们只需要做一次。
注意1:如果你使用的是 Jaspersoft Studio,则无需进行此步骤。你可以在程序内直接编译。
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world.jrxml'; //or input dir //$input = __DIR__ . '/vendor/geekcom/phpjasper/examples'; $jasper = new PHPJasper; $jasper->compile($input)->execute();
此命令将编译 hello_world.jrxml
源文件为 hello_world.jasper
文件。
处理中
现在让我们处理之前编译的报告
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world.jasper'; $output = __DIR__ . '/vendor/geekcom/phpjasper/examples'; $options = [ 'format' => ['pdf', 'rtf'] ]; $jasper = new PHPJasper; $jasper->process( $input, $output, $options )->execute();
现在查看示例文件夹! :) 很棒,对吧?你现在有 2 个文件,hello_world.pdf
和 hello_world.rtf
。
在 src/JasperPHP.php
中查看 methods compile
和 process
以获取更多详细信息
列出参数
查询 jasper 文件以检查给定 jasper 报告文件中可用的参数
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = __DIR__ . '/vendor/geekcom/phpjasper/examples/hello_world_params.jrxml'; $jasper = new PHPJasper; $output = $jasper->listParameters($input)->execute(); foreach($output as $parameter_description) print $parameter_description . '<pre>';
使用数据库生成报告
我们还可以指定连接数据库的参数
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = '/your_input_path/your_report.jasper'; $output = '/your_output_path'; $options = [ 'format' => ['pdf'], 'locale' => 'en', 'params' => [], 'db_connection' => [ 'driver' => 'postgres', //mysql, .... 'username' => 'DB_USERNAME', 'password' => 'DB_PASSWORD', 'host' => 'DB_HOST', 'database' => 'DB_DATABASE', 'port' => '5432' ] ]; $jasper = new PHPJasper; $jasper->process( $input, $output, $options )->execute();
注意2
有关完整地区列表,请参阅 支持地区
使用 MSSQL 数据库
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = '/your_input_path/your_report.jasper or .jrxml'; $output = '/your_output_path'; $jdbc_dir = __DIR__ . '/vendor/geekcom/phpjasper/bin/jaspertarter/jdbc'; $options = [ 'format' => ['pdf'], 'locale' => 'en', 'params' => [], 'db_connection' => [ 'driver' => 'generic', 'host' => '127.0.0.1', 'port' => '1433', 'database' => 'DataBaseName', 'username' => 'UserName', 'password' => 'password', 'jdbc_driver' => 'com.microsoft.sqlserver.jdbc.SQLServerDriver', 'jdbc_url' => 'jdbc:sqlserver://127.0.0.1:1433;databaseName=Teste', 'jdbc_dir' => $jdbc_dir ] ]; $jasper = new PHPJasper; $jasper->process( $input, $output, $options )->execute();
从 XML 生成报告
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = '/your_input_path/your_report.jasper'; $output = '/your_output_path'; $data_file = __DIR__ . '/your_data_files_path/your_xml_file.xml'; $options = [ 'format' => ['pdf'], 'params' => [], 'locale' => 'en', 'db_connection' => [ 'driver' => 'xml', 'data_file' => $data_file, 'xml_xpath' => '/your_xml_xpath' ] ]; $jasper = new PHPJasper; $jasper->process( $input, $output, $options )->execute();
从 JSON 生成报告
require __DIR__ . '/vendor/autoload.php'; use PHPJasper\PHPJasper; $input = '/your_input_path/your_report.jasper'; $output = '/your_output_path'; $data_file = __DIR__ . '/your_data_files_path/your_json_file.json'; $options = [ 'format' => ['pdf'], 'params' => [], 'locale' => 'en', 'db_connection' => [ 'driver' => 'json', 'data_file' => $data_file, 'json_query' => 'your_json_query' ] ]; $jasper = new PHPJasper; $jasper->process( $input, $output, $options )->execute();
MySQL
我们在 /src/JasperStarter/jdbc/
目录中提供 MySQL 连接器(v5.1.39)。
PostgreSQL
我们在 /src/JasperStarter/jdbc/
目录中提供 PostgreSQL(v9.4-1203)。
MSSQL
适用于 SQL Server 的 Microsoft JDBC 驱动程序 6.0、4.2、4.1 和 4.0.
性能
取决于复杂性、数据量和机器资源(告诉我你的用例)。
我有一个生成带有数据库连接、图片和多个页面的 发票 的报告,它大约需要 3/4 秒 处理时间。我建议你在后台使用工作进程生成报告。
谢谢
Cenote GmbH 为 JasperStarter 工具。
有问题吗?
打开一个新的 问题 或查找已关闭的问题
许可
MIT
贡献
为 PHP 社区做出贡献,进行分支操作!